随学习进度持续更新。。。
参考 https://www.cnblogs.com/yuehq/p/6501547.html
代码
syms t m
x = sym('x(t,m)'); %定义x是关于t的一个表达式
y = sin(x);
df = diff(y,t) %得到表达式结果
latex(df) %转换为latex代码
df = subs(df,x,t^2*m) %将x表达式代入
latex(df) %转换为latex代码
结果如下:
df =
cos(x(t, m))*diff(x(t, m), t)
ans =
'\cos\left(x\left(t,m\right)\right)\,\frac{\partial }{\partial t} x\left(t,m\right)'
df =
2*m*t*cos(m*t^2)
ans =
'2\,m\,t\,\cos\left(m\,t^2\right)'
推导结果输入到markdown中效果如下:
cos ( x ( t , m ) ) ∂ ∂ t x ( t , m ) \cos\left(x\left(t,m\right)\right)\,\frac{\partial }{\partial t} x\left(t,m\right) cos(x(t,m))∂t∂x(t,m)
2 m t cos ( m t 2 ) 2\,m\,t\,\cos\left(m\,t^2\right) 2mtcos(mt2)
M = sym('A',[5,4]) %生成符号矩阵
latex(M)
result:
M =
[ A1_1, A1_2, A1_3, A1_4]
[ A2_1, A2_2, A2_3, A2_4]
[ A3_1, A3_2, A3_3, A3_4]
[ A4_1, A4_2, A4_3, A4_4]
[ A5_1, A5_2, A5_3, A5_4]
ans =
'\left(\begin{array}{cccc} A_{1,1} & A_{1,2} & A_{1,3} & A_{1,4}\\ A_{2,1} & A_{2,2} & A_{2,3} & A_{2,4}\\ A_{3,1} & A_{3,2} & A_{3,3} & A_{3,4}\\ A_{4,1} & A_{4,2} & A_{4,3} & A_{4,4}\\ A_{5,1} & A_{5,2} & A_{5,3} & A_{5,4} \end{array}\right)'
latex:
( A 1 , 1 A 1 , 2 A 1 , 3 A 1 , 4 A 2 , 1 A 2 , 2 A 2 , 3 A 2 , 4 A 3 , 1 A 3 , 2 A 3 , 3 A 3 , 4 A 4 , 1 A 4 , 2 A 4 , 3 A 4 , 4 A 5 , 1 A 5 , 2 A 5 , 3 A 5 , 4 ) \left(\begin{array}{cccc} A_{1,1} & A_{1,2} & A_{1,3} & A_{1,4}\\ A_{2,1} & A_{2,2} & A_{2,3} & A_{2,4}\\ A_{3,1} & A_{3,2} & A_{3,3} & A_{3,4}\\ A_{4,1} & A_{4,2} & A_{4,3} & A_{4,4}\\ A_{5,1} & A_{5,2} & A_{5,3} & A_{5,4} \end{array}\right) ⎝⎜⎜⎜⎜⎛A1,1A2,1A3,1A4,1A5,1A1,2A2,2A3,2A4,2A5,2A1,3A2,3A3,3A4,3A5,3A1,4A2,4A3,4A4,4A5,4⎠⎟⎟⎟⎟⎞
try:
syms m n
N = sym('x',[m,n])
failed:
错误使用 sym (line 224)
The second argument must be an assumption or a size vector.
版本问题
感谢@大家安静啊 的提醒,博主自己更新2019b后尝试操作确实有报错:
>> syms t m
>> x = sym('x(t,m')
错误使用 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);
原因是matlab更新了这个用法,在sym的文档中有说明,感觉syms更好用了。
测试:
>> syms x(m,t)
>> x
x(m, t) =
x(m, t)
>> sin(x)
ans(m, t) =
sin(x(m, t))
搞定!