常用画图函数及其作用
plot(x,y,'str')
plot即画图,x是横轴,也就是自变量;y是纵轴,也就是图形的函数;str是指改变图形线条颜色、类型的语句。str的内容可见下图:
例程如下:
x = 0:0.5:4*pi;
y=sin(x);h=cos(x);w=1./(1+exp(-x));
g=(1/(2*pi*2)^0.5).*exp((-1.*(x-2*pi).^2)./(2*2^2));
plot(x,y,'bd-',x,h,'gp:',x,w,'ro-',x,g,'c^-');
结果如下:
legend('函数名',....):可依次标志每条的函数名是什么.
示例如下:
x = 0:0.5:4*pi;
y=sin(x);h=cos(x);w=1./(1+exp(-x));
g=(1/(2*pi*2)^0.5).*exp((-1.*(x-2*pi).^2)./(2*2^2));
plot(x,y,'bd-',x,h,'gp:',x,w,'ro-',x,g,'c^-');
legend('sin(x)','cos(x)','Sigmoid','Gauss function');
结果如下:
title('图形名称'):显示图形名字.
xlabel('x轴的含义'):显示x轴的含义
ylabel('y轴的含义'):显示y轴的含义.
示例如下:
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = exp(-x);
plot(x,y1,'--*',x,y2,':o');
xlabel('t= 0 to 2\pi');
ylabel('value of sin(t) and e^{x}');
title('function plots of sin(t) and e^{-x}');
legend('sin(t)','e^{-x}');
结果如下:
LaTex函数:显示积分函数.
text(显示内容):可以设置一个特定位置显示文本内容;
annotation():给函数加注释或者箭头
示例如下:
x = linspace(0,3);
y = x.^2.*sin(x);
plot(x,y);
line([2,2],[0,2^2*sin(2)]);
str = '$$ \int_{0}^{2} x^2\sin(x) dx $$';
text(0.25,2.5,str,'Interpreter','latex');
annotation('arrow','X',[0.32,0.5],'Y',[0.6,0.4]);
结果如下:
plotyy():共用一个x的坐标系;
get():返回某些对象属性的当前值;
set():改变句柄图形对象属性;
示例如下
x = 0:0.01:2*pi;
y1 = sin(x);
y2 = 2.^x;
[AX,Y1,Y2] = plotyy(x,y1,x,y2);
set(get(AX(1),'Ylabel'),'String','Sin(x)'); %定义y1的y轴的名称
set(get(AX(2),'Ylabel'),'String','2^x');%定义y2的y轴名称
xlabel('Time (s)');
结果如下: