有没有办法在Matlab中做隐式微分 [英] is there a way to do implicit differentiation in matlab

查看:113
本文介绍了有没有办法在Matlab中做隐式微分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用matlab来帮助我解决数学问题.

I was using matlab a lot to help me with math problems.

现在,我正在寻找一种在matlab中进行隐式区分的方法. 例如,我想相对于dy/dx区分y^3*sin(x)+cos(y)*exp(x)=0.

Right now I am looking for a way to do implicit differentiation in matlab. For example, I would like to differentiate y^3*sin(x)+cos(y)*exp(x)=0 with respect to dy/dx.

我知道如何使用数学方法正常执行此操作,但是我一直在努力寻找使用matlab的简便方法.当我需要正态微分(与f(x)求微分)时,我正在使用符号数学工具箱,并执行以下操作:

I am aware how to do this normally using math methods, but I was struggling to find the easy way with matlab. When I need normal differentiation ( find differential from f(x) ), I was using symbolic math toolbox and did something like this:

syms x
y = myfunctionOf(x)
diff(y)

我浏览了doc diff并在符号工具箱中进行了快速查找,但是没有发现任何有助于上述情况的信息.但是我只是拒绝相信matlab没有这么简单的功能.

I looked through doc diff and also made a quick lookup in symbolic toolbox, but found nothing to help me with the above mentioned case. But I just refuse to believe that matlab does not have such a simple function.

推荐答案

以下代码可以满足您的要求,所有解释均在注释中,请注意,该代码假设您希望Matlab进行几乎所有的Mathematical在想你.

Here is some code that does what you want, all explanation are in the comments, note that this code assumes that you want Matlab to do almost all of the Mathematical thinking for you.

%// Firstly you need to define a function `f` in terms of `x` and `y`. 
syms x y;
f = y^3*sin(x)+cos(y)*exp(x);

%// Then you need to tell Matlab that y is a function of x,
%// you do this by replacing y with y(x)
yOfx = sym('y(x)');
f_yOfx = subs(f, y, yOfx);

%// Then you need to differentiate with respect to x
df = diff(f_yOfx, x);

%// df will have diff(y(x), x) terms in it, 
%// we want to solve for this term, 
%// to make it easier we should first replace it with a variable
%// and then solve
syms Dy;
df2 = subs(df, diff(yOfx, x), Dy);
dyOver_dx = solve(df2, Dy);

%// Finally if we do not want all of the y(x) terms, 
%// then replace them with y
dyOver_dx = subs(dyOver_dx, yOfx, y)

当然,如果我们不介意做一些书面工作,我们可以得到dy/dx = -(partial f/partail x)/(partial f/partial y),从中我们可以得到更短的代码

Of course if we do not mind do a bit of paper work, we can get dy/dx = -(partial f/partail x)/(partial f/partial y) from which we can get the much shorter code

%// Implicit differentiation identity
also_dyOver_dx = -diff(f, x)/diff(f, y);

在此检查两个答案是否相同.

Here is a check that the two answers are the same.

simplify(dyOver_dx - also_dyOver_dx) %// == 0

这篇关于有没有办法在Matlab中做隐式微分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆