这里有一些代码可以做你想做的事,所有的解释都在注释中,注意这个代码假设你希望 Matlab 为你做几乎所有的数学思考。
%// 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)
得到更短的代码
%// Implicit differentiation identity
also_dyOver_dx = -diff(f, x)/diff(f, y);
这是检查两个答案是否相同。
simplify(dyOver_dx - also_dyOver_dx) %// == 0