cyfile / Matlab-miscellanies

各种Matlab代码
0 stars 0 forks source link

隐函数求导 #11

Open 213cy opened 8 years ago

213cy commented 8 years ago
%% 隐函数求导
syms x y y2(x)
%%%%%%%%%%%%%%%%
f=x*sin(x*y);% x,y都是变量,所以f是一个表达式
f1(x,y)=x*sin(x*y); % f1 是 x和y的函数
f2=x*sin(x*y2);% y2是函数,所以f2 是函数
%% 法1 标准方法
-diff(f, x)/diff(f, y)
-diff(f1, x)/diff(f1, y)
%% 法2 原理方法
eq=diff(subs(f,y,'y(x)')==0)% 替换表达式中的变量,新变量为表达式(变量矩阵y中的第x个表达式)
eq2=subs(eq,{'diff(y(x), x)' 'y(x)'},{'dydx' y});
solve(eq2,'dydx')
%
eq=diff(subs(f1,y,'y(x)')==0)% 替换函数中的变量,新变量为函数(变量x的函数y)
eq2=subs(eq,{'D(y)(x)' 'y(x)'},{'dydx' y});
solve(eq2,'dydx')
%
eq=diff(f2==0)
eq2=subs(eq,'D(y2)(x)','dydx');
solve(eq2,'dydx')
%% 法3 原始方法
f=x^2 + x*y + y^2-20
y_x=solve(f==0,y);%显示表达式很多情况下都解不出来
dydx=diff(y_x,x);
%pretty(simplify(dydx))
%
dydx_xy=-diff(f, x)/diff(f, y);
dydx_x=subs(dydx_xy,y,y_x);
%isequaln(dydx_x,[compose(dydx_xy,y_x(1),y,x);compose(dydx_xy,y_x(2),y,x)])
%pretty(simplify(dydx_x))
%
factor(dydx),factor(dydx_x)
isequaln(dydx,dydx_x)% isequaln这个函数的化简功能非常弱,要手动化简
isequaln(collect(dydx),collect(dydx_x))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 隐函数高阶导数
syms x y a
f(x,y)=x^3+y^3-3*a*x*y
%f(x,y)=x^2+y^2+a^2-4*y
dydx_f=-diff(f, x)/diff(f, y);
d2ydx2_f=diff(subs(dydx_f,y,'y(x)'),x);
d2ydx2_f2=subs(d2ydx2_f,{'D(y)(x)' 'y(x)'},{dydx_f 'y'});
pretty(simplify(d2ydx2_f2))
%%
F(x,y,a)=x^3+y^3-3*a*x*y
dydx_F=-diff(F, x)/diff(F, y);
dadx_F=-diff(F, x)/diff(F, a);
d2ydx2_F=diff(subs(dydx_F,{y a},{'y(x)' 'a(x)'}),x);
d2ydx2_F2=subs(d2ydx2_F,{'D(y)(x)' 'D(a)(x)' 'y(x)' 'a(x)'},{dydx_F dadx_F y a});
pretty(factor(d2ydx2_F2))
%pretty(simplifyFraction(d2ydx2_F2))
%%
syms x y z
% 书上的一道例题
F(x,y,z)=x^2+y^2+z^2-4*z
dzdx_F=-diff(F, x)/diff(F, z);
dydx_F=-diff(F, x)/diff(F, y);
d2ydx2_F=diff(subs(dzdx_F,{y z},{'y(x)' 'z(x)'}),x);
d2ydx2_F2=subs(d2ydx2_F,{'D(y)(x)' 'D(z)(x)' 'y(x)' 'z(x)'},{dydx_F dzdx_F y z});
pretty(factor(d2ydx2_F2))
%%