MATLAB 中将传递函数以Latex的格式输出


Letex形式的公式可以很方便地将公式插入到文档中。下面我以此传递函数为例演示如何将Matlab中的传递函数转化为Latex形式。

>> G=tf([1 2],[1 2 3])
G =
 
      s + 2
  -------------
  s^2 + 2 s + 3
 
Continuous-time transfer function.

Step1:将传递函数转换为符号表达式

[num,den] = tfdata(G);
syms s
G_sym = poly2sym(cell2mat(num),s)/poly2sym(cell2mat(den),s)

此时地结果:

G_sym =
 
(s + 2)/(s^2 + 2*s + 3)

Step2:将符号表达式转换为Latex格式

G_latex=latex(G_sym);

输出的结果:

G_latex =

    '\frac{s+2}{s^2+2\,s+3}'

这时我们就可以复制粘贴到支持Latex语法的文档编辑器中了,比如我粘贴到CSDN的Markdown编辑器:

$G(s)=\frac{s+2}{s^2+2\,s+3}$

显示效果:
G ( s ) = s + 2 s 2 + 2 s + 3 G(s)=\frac{s+2}{s^2+2\,s+3}

用.m函数实现

function G_latex=tf2latex(G)
[num,den] = tfdata(G);
syms s;
G_sym = poly2sym(cell2mat(num),s)/poly2sym(cell2mat(den),s);
G_latex=latex(G_sym);

使用效果:

l=tf2latex(G)

l =

    '\frac{s+2}{s^2+2\,s+3}'
发布了45 篇原创文章 · 获赞 1 · 访问量 2418

猜你喜欢

转载自blog.csdn.net/amnesiagreen/article/details/105728069