matlab 绘图的三种⽅法:已知函数表达式
matlab经常要使⽤函数进⾏绘图,举个栗⼦,要绘制下⾯的函数应该怎么绘制呢?
总结有以下三种⽅法:
1.for 循环
这是最最原始的⽅法了,不推荐
2.直接计算
matlab有强⼤的矩阵运算能⼒,使得问题⾮常的⽅便~但是需要把运算符号加上点号 ,改成.*,把^ 改成.^。3.使⽤subs 函数
另外还可以使⽤subs函数,可以提前规定⼀个函数,利⽤subs函数进⾏赋值。
对⽐
用subplot函数对三种⽅法进⾏了运⾏时间的测试:
⽅法2最优(直接计算),⽅法1次之,⽅法3最慢。
f (x )=sin (x )∗(1−x )
2clear t =linspace (0,20,501);fi =zeros (1,501);for  ii =1:length (t ) f1(ii )=sin (t (ii ))*(1-t (ii )^2);end plot (t ,f1,'-')
1
2
3
4
5
6
7⋅∗clear t =linspace (0,20,501);f1=sin (t ).*(1-t .^2);plot (t ,f1,'-')
1
2
3
4tic clear syms x f =sin (x )*(1-x ^2);t =linspace (0,20,501);f1=subs (f ,x ,t ); plot (t ,f1,'-')toc
1
2
3
4
5
6
7
8
附上测试的代码clear t =linspace (0,20,501);% 第⼀种⽅法tic ;fi =zeros (1,501);for  ii =1:length (t ) f1
(ii )=sin (t (ii ))*(1-t (ii )^2);end t1=toc ;% 第⼆种⽅法tic ;f2=sin (t ).*(1-t .^2);t2=toc ;% 第三种⽅法tic ;syms x f =sin (x )*(1-x ^2);f3=subs (f ,x ,t ); t3=toc ;%plot (t ,f1,'-')subplot (1,3,1)plot (t ,f1)title (['⽅法1⽤时',num2str (t1),'s'])subplot (1,3,2)plot (t ,f2)title (['⽅法2⽤时',num2str (t2),'s'])subplot (1,3,3)plot (t ,f3)title (['⽅法3⽤时',num2str (t3),'s'])1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。