1、plot
基本语法:
plot(x)
x为向量时,以该元素的下标为横坐标、元素值为纵坐标绘出曲线
plot(x, y)
x、y为同维数组时,绘制以x、y元素为横纵坐标的曲线。
plot(x1, y1, x2, y2, …)
绘制以x1为横坐标、y1为纵坐标的曲线1,以x2为横坐标、y2为纵坐标的曲线2,等等。
实例:
>> x = 0:pi/100:2*pi;
>> y = 2*exp(-0.5*x).*sin(2*pi*x);
>> plot(x,y)
>> t = -pi:pi/200:pi;
>> x = t.*cos(3*t);
>> y = t.*(sin(t).^2);
>> plot(x,y)
绘制多条曲线实例:
>> y1 = sin(t);
>> y2 = sin(t).*sin(9*t);
>> t3 = pi*(0:9)/9;
>> y3 = sin(t3).*sin(9*t3);
>> plot(t,y1,'r:',t,y2,'b',t3,y3,'bo') % 绘制多条曲线,单引号里面的是设置曲线的格式
>> axis([0,pi,-1,1]) % 设置x轴的范围为[0,pi],设置y轴的范围为[-1,1]
2、hold on、hold off、subplot
hold on 不会覆盖上一图形,允许在上一图形的坐标轴上添加新的图形。
hold off 将会清除原来坐标轴上的图形,重新绘制新的图形。
subplot(m, n, k) :使(m*n)幅子图中第k个子图成为当前图。
subplot(‘postion’, [left, bottom, width, height]) :在指定的位置上开辟子图,并成为当前图。
title(‘string’)为对应的图添加标题,只需要写到绘制图的后面即可。
实例:
>>t=(pi*(0:1000)/1000)';
>>y1=sin(t);
>>y2=sin(10*t);
>>y12=sin(t).*sin(10*t);
>>subplot(2,2,1),plot(t,y1);axis([0,pi,-1,1]);title("y1")
>>subplot(2,2,2),plot(t,y2);axis([0,pi,-1,1]);title("y2")
>>subplot('position',[0.2,0.05,0.6,0.45])
>>plot(t,y12,'b-',t,[y1,-y1],'r:');title("y3")
// 高、宽的取值范围都是[0, 1],而左下角为(0,0)坐标 。