matlab中的sub函数

来源:互联网 发布:python清屏 编辑:程序博客网 时间:2024/06/06 02:42

matlab subs函数应用,运行命令help subs

函数解释如下

subs-Symbolic substitution 符号替换

This Matlab function returns a copy of s replacing all occurrences of old with new, and then evaluating s.

example:

        subs(s, old, new)

subs(s, new)

subs(s)

也就是说函数subs(s, old, new)的功能就是将s符号表达式中的所有old符号变量替换成新的new的值,然后计算s并返回。

举个例子

例子:

syms x1 x2; %创建符号变量和函数的快捷方式f=(x1-1)^2+2*(x2-2)^2+x1;   %f为目标函数(两变量x1和x2)x=[3;0];    %x为初始点,x(1)=3,x(2)=0d=-[diff(f,x1);diff(f,x2)];     %分别求x1和x2的偏导数,即下降的方向. d=1-2*x1   8-4*x2d_temp=subs(d,x1,x(1)) %符号替换函数,将d表达式中所有的x1的值替换为x(1)的值,计算d并返回。

输出结果:

d_temp =        -5 8 - 4*x2