绘制图形的几个函数用法
例:绘制函数图形 f = xsin(10* pi*x)+1,区间[0,2];
plot()
显示已知点的坐标:
plot(5,38.45,'rs','MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',5)
text(5,38.45,'(5,38.45)','EdgeColor','red','VerticalAlignment','bottom');
x=[0 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 30 31 32 33 34 35 36 37 38 39];
y2=[0 3.75 10.95 19.95 29.3 38.45 46.85 54.50 61.55 68.00 73.85 78.95 83.75 88.25 92.3 95.9 99.2 102.2 104.9 107.3 109.55 111.65 113.6 115.4 117.05 118.55 119.9 121.1 122.15 123.05 123.8 124.4 124.85 125.25 125.61 125.91 126.11 126.26 126.36 126.44];
plot(x,y2);
hold on;
y3=126.44;
plot(x,y3,'-');
% Set up fittype and options.
ft = 'linearinterp';
opts = fitoptions( ft );
opts.Normalize = 'on';
% Fit model to data.
fitresult = fit( x', y2', ft, opts );
xx1 = [6.321,11.15,21.15]; % x = [6.321,11.15,21.15]
yy1 = fitresult( xx1 ); % 与x对应的y值
% 画点 标注
for i = 1:length(xx1)
plot(xx1(i),yy1(i),'rs','MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',6)
text(xx1(i),yy1(i),['(',num2str(xx1(i),'%5.2f'),',',num2str(yy1(i),'%5.2f'),')'],'EdgeColor','red','BackgroundColor',[.7 .9 .7],'VerticalAlignment','bottom');
end
- plot()
x=linspace(0,2,10); %将区间[0,10]划分为1000小区间
y=x.*sin(10*pi*x)+1;
plot(x,y) %以x元素为横坐标值,y元素为纵坐标值绘制图形
x=linspace(0,2,1000); %将区间[0,10]划分为1000小区间
y=x.*sin(10*pi*x)+1;
plot(x,y) %显示图形
通过对比上下两张图可以发现,划分的区间越小,即划分越细,弧线越圆滑,图形拟合得越好。
- 含多个输入参数的plot函数还可以同时绘制多条曲线,如果输入是矩阵的话,配对的x,y分别是矩阵的列元素,曲线条数等于矩阵列数。
x=0:pi/20:2*pi; %x的区间是0-2*pi,pi/20是步长
y1=sin(x);
y2=cos(x);
y3=2*sin(x);
x=[x;x;x]';
y=[y1;y2;y3]';
plot(x,y)
grid on %给图像加上栅格
- 图形标注
%添加坐标轴信息
xlabel('变量 X')
ylabel('变量 Y1 & Y2')
title('正弦余弦波形') %添加图像标题
text(1.5,0.3,'cos(x)') %将cosx这个注解加到坐标中的某个位置
gtext('sin(x)') %用鼠标的光标定位,将sinx这个注解放在你鼠标点击的地方
legend('sinx','cosx') %添加图例
- 利用plot函数可以直接将矩阵的数据绘制在图形窗体中,此时plot函数将矩阵的每一列数据作为一条曲线绘制在窗体中。
A=pascal(5)
plot(A)
- 含选项的plot()函数
Matla