用到的函数:
1、函数表达式已知
例:
y = x 2 y=x^2 y=x2 x = t 2 x=t^2 x=t2
求 y y y关于 t t t的导数,代码如下所示:
syms x tx = t^2;
y = x^2;
df = diff(y,t)
latex(df)
运行结果:
>> untitleddf =4*t^3ans ='4\,t^3'>>
Latex代码输入到markdown,结果如下所示:
4 t 3 4\,t^3 4t3
2、函数表达式未知
例:
y = x 2 y=x^2 y=x2 x = f ( t ) x=f(t) x=f(t)
求 y y y关于 t t t的偏导数,代码如下所示:
syms x t x = str2sym('x(t)');
y = x^2;
df = diff(y,t)
latex(df)
运行结果:
>> untitleddf =2*x(t)*diff(x(t), t)ans ='2\,x\left(t\right)\,\frac{\partial }{\partial t} x\left(t\right)'>>
Latex代码输入到markdown,结果如下所示:
2 x ( t ) ∂ ∂ t x ( t ) 2\,x\left(t\right)\,\frac{\partial }{\partial t} x\left(t\right) 2x(t)∂t∂x(t)
一处错误:
之前在2、函数表达式未知这里,用的是如下代码:
syms x t x = sym('x(t)');
y = x^2;
df = diff(y,t)
latex(df)
报错:
>> untitled
错误使用 sym>convertChar (line 1548)
Character vectors and strings in the first argument can only specify a variable or number. To
evaluate character vectors and strings representing symbolic expressions, use 'str2sym'.出错 sym>tomupad (line 1255)S = convertChar(x);出错 sym (line 222)S.s = tomupad(x);出错 untitled (line 3)
x = sym('x(t)');
解决办法:用str2sym代替sym