实验6Matlab绘图.docx
《实验6Matlab绘图.docx》由会员分享,可在线阅读,更多相关《实验6Matlab绘图.docx(14页珍藏版)》请在冰豆网上搜索。
实验6Matlab绘图
实验6Matlab绘图
一、实验目的
1、掌握绘制二维图形的常用函数。
2、掌握绘制三维图形的常用函数。
3、掌握图像读写函数和显示函数的使用方法。
二、实验内容
1、已知
,完成以下操作:
(1)在同一坐标系下,用不同的颜色和线型绘制三条曲线。
(2)在同一个图形窗口中,以子图的形式绘制三条曲线。
(3)在同一个图形窗口中,以子图的形式,分别用条形图、阶梯图、杆图和填充图绘制三条曲线。
解答:
(1)
x=-10:
:
10;
y1=x.^2;
y2=cos(2*x);
y3=y1.*y2;
plot(x,y1,x,y2,x,y3)
(2)x=-10:
:
10;
y1=x.^2;
y2=cos(2*x);
y3=y1.*y2;
subplot(1,3,1);
plot(x,y1);
subplot(1,3,2);
plot(x,y2);
subplot(1,3,3);
plot(x,y3);
(3)
x=-10:
:
10;
y1=x.^2;
y2=cos(2*x);
y3=y1.*y2;
subplot(2,2,1);
bar3(x,y1);holdon
bar3(x,y2);holdon
bar3(x,y3);holdon
view(90,0);
subplot(2,2,2);
stairs(x,y1);holdon
stairs(x,y2);holdon
stairs(x,y3);holdon
subplot(2,2,3);
stem(x,y1);holdon
stem(x,y2);holdon
stem(x,y3);holdon
subplot(2,2,4);
fill(x,y1,x,y2,x,y3);
2、设
绘制
和
曲线,并给图形添加图形标注(title(绘图曲线),xlabel(x轴),ylabel(y轴),text(在合适位置标注两曲线名),legend(y1,y2),axis(指定范围),显示网格,不显示边框)。
解答:
x=0:
:
2*pi;
y1=*log(x+sin(pi*x));
y2=+3*sin(x)./(1+x.^2)).*cos(x);
plot(x,y1,x,y2)
title('绘图曲线');
xlabel('x轴');
ylabel('y轴');
text(x(2000),y1(2000),'\leftarrowy1')
text(x(2000),y2(2000),'\leftarrowy2')
legend('y1','y2');
axis([02*pi-infinf]);
gridon;
boxoff;
3、绘制曲面图形,在同一窗口不同子图上分别采用faceted,flat,interp进行着色处理。
解答:
S=linspace(0,*pi,100);
T=linspace(0,*pi,100);
[s,t]=meshgrid(S,T);
x=cos(s).*cos(t);
y=cos(s).*sin(t);
z=sin(s);
surf(x,y,z);
shadingfaceted
faceted
flat
interp
4、绘制函数的曲面图和等高线。
其中
的21个值均匀分布在[-5,5]范围,
的31个值均匀分布在[0,10],要求使用subplot(1,2,1)和subplot(1,2,2)将生产的曲面图和等高线图画在同一窗口上。
绘制曲面时采用hot(5)色图,标注标题(曲面图),标注各个坐标轴,观察角度为(-20,20)。
等高线图标注标题(等高线图),标注各个坐标轴,观察角度为(0,90)。
解答:
X=linspace(-5,5,21);
Y=linspace(0,10,31);
[x,y]=meshgrid(X,Y);
z=5*cos(x).*cos(y).*exp(-sqrt(x.^2+y.^2)/4);
subplot(1,2,1);
surf(x,y,z);
colormap(hot(5));
title('曲面图');
xlabel('x轴');
ylabel('y轴');
zlabel('z轴');
view(-20,20);
subplot(1,2,2);
contour3(x,y,z);
title('等高线图');
xlabel('x轴');
ylabel('y轴');
zlabel('z轴');
view(0,90);
5、假设
的21个值均匀分布在[-5,5]范围,
的31个值均匀分布在[0,10],曲面方程
满足:
(1)绘制函数的曲面图,并且将它绕z轴旋转。
(2)绘制函数的曲面图,采用imwrite函数将绘制的曲面保存在c:
目录下,文件名为。
采用saveas函数将绘制的曲面保存在c:
目录下,文件名为。
检查c:
目录是否存在这两个文件。
(3)采用imread函数读取并分别在同一窗口不同子窗口用image函数、imshow显示图像。
解答:
(1)X=linspace(-5,5,21);
Y=linspace(0,10,31);
[x,y]=meshgrid(X,Y);
z=cos(x).*cos(y).*exp(-sqrt(x.^2+y.^2)/4);
surf(x,y,z)
view(150,0);
(2)
X=linspace(-5,5,21);
Y=linspace(0,10,31);
[x,y]=meshgrid(X,Y);
z=cos(x).*cos(y).*exp(-sqrt(x.^2+y.^2)/4);
surf(x,y,z)
view(150,0);
[data,map]=getframe;
imwrite(data,'c:
\','jpg');
使用MATLAB2012b,出现下面问题,改存在系统默认目录中
Errorusingimwrite(line459)
Unabletoopenfile"c:
\"forwriting.Youmaynothavewritepermission.
Errorinf(line8)
imwrite(data,'c:
\','jpg');
info=imfinfo('')
info=Filename:
'G:
\ProgramFiles\matlab2012b\R2012b\bin\'
FileModDate:
'21-Apr-201314:
55:
45'
FileSize:
31215
Format:
'jpg'
FormatVersion:
''
Width:
435
Height:
343
BitDepth:
24
ColorType:
'truecolor'
FormatSignature:
''
NumberOfSamples:
3
CodingMethod:
'Huffman'
CodingProcess:
'Sequential'
Comment:
{}
saveas(gcf,'','bmp')
imfinfo('')
ans=Filename:
'G:
\design\matlab\'
FileModDate:
'21-Apr-201315:
11:
36'
FileSize:
236278
Format:
'bmp'
FormatVersion:
'Version3(MicrosoftWindows'
Width:
560
Height:
420
BitDepth:
8
ColorType:
'indexed'
FormatSignature:
'BM'
NumColormapEntries:
256
Colormap:
[256x3double]
RedMask:
[]
GreenMask:
[]
BlueMask:
[]
ImageDataOffset:
1078
BitmapHeaderSize:
40
NumPlanes:
1
CompressionType:
'none'
BitmapSize:
235200
HorzResolution:
0
VertResolution:
0
NumColorsUsed:
0
NumImportantColors:
0
(3)
cdata=imread('');
subplot(1,2,1);
image(cdata);
subplot(1,2,2);
imshow(cdata);