lzy820260594

0、小叙闲言 

通过上一节的学习,掌握了基本的matlab编程环境和一些重要的编程命令,本节可以说真正开始了基础编程的设计,希望可以踏踏实实一步一个脚印的学好。

1、思维导图 

很多例子、细节无法在导图中表现出来,会在之后的例子中详细写出来。

2、书中的例子 

例1:if 结构

b=5,a=3,c=2;
if (b^2 - 4*a*c) < 0 
 disp(\'This equation has two complex roots.\'); 
elseif (b^2 - 4*a*c) == 0 
 disp(\'This equation has two identical real roots.\'); 
else 
 disp(\'This equation has two distinct real roots.\'); 
end 

  本例结果如下:

This equation has two distinct real roots.

  if结构是最简单的选择结构,基本形式为if》》elseif》》elseif》》。。。》》else》》end;其中,elseif可以有多个,else只有一个;if和end是一一对应的,有几个if必须在结尾配几个end。其形式和c语言基本没有差别,所以比较简单。

例2:switch结构

switch (value) 
case {1, 3, 5, 7, 9}, 
 disp(\'The value is odd.\'); 
case {2, 4, 6, 8, 10}, 
 disp(\'The value is even.\'); 
otherwise, 
 disp(\'The value is out of range.\'); 
end 

  此处需要注意的是,在value的取值中,如果结果相同,那么一个case可以取多个值,此利中便是如此;当然,case后面也可以只有一个数。

例3:利用axis控制x,y轴的上下限

x=-2*pi:pi/20:2*pi;
y=sin(x);
plot(x,y);
title(\'plot of sin(x) vs x\');
axis([0 pi 0 1]);

得到的结果如下:

例4:在同一个坐标系中画出多个图像

x=-2*pi:pi/20:2*pi;
y1=sin(x);
y2=cos(x);
plot(x,y1,\'b-\');
hold on;
plot(x,y2,\'r--\');
hold off;
legend(\'sin x\',\'cos x\'); 

结果如下:

例5:子图象

创建子图象需要用到subplot命令,其形式为subplot(m,n,p),意义为在窗口创建mxn个子图象,按照m行n列来排布,p为子图象的序号。

figure(1);
subplot(2,2,1);
x=-pi:pi/20:pi;
y=sin(x);
plot(x,y);
title(\'a\');
subplot(2,2,2);
x=-pi:pi/20:pi;
y=cos(x);
plot(x,y);
title(\'b\');
subplot(2,2,3);
x=-pi:pi/20:pi;
y=sin(x);
plot(x,y);
title(\'c\');
subplot(2,2,4);
x=-pi:pi/20:pi;
y=cos(x);
plot(x,y);
title(\'d\');

  结果如下:

例6:极坐标图像

函数形式如下:polar(theta,r),其中theta代表一个弧度角数组,r代表一个距离数组。

g=0.5;
theta=0:pi/20:2*pi;
gain=2*g*(1+cos(theta));
polar(theta,gain,\'b-\');
title(\'tuxiang\');

  结果如下:

 3、感悟 

通过学习本部分的内容,熟悉了基本语句的用法,增强了绘制图像的能力。

分类:

技术点:

相关文章:

  • 2021-08-16
  • 2021-07-04
  • 2022-02-04
  • 2021-11-02
猜你喜欢
  • 2021-10-27
  • 2022-12-23
  • 2021-11-12
  • 2022-02-15
  • 2022-01-23
相关资源
相似解决方案