2.5 基本绘图方法
2.5.1 直角坐标中的两维曲线
1,plot(y)
y=5*rand(1,10)-0.5
plot(y)
title('my first plot')
xlabel('x')
ylabel('y')
plot(t,y)的用法
t=0:0.5:4*pi;
>> y=exp(-0.1*t).*sin(t);% 元素群运算
>> plot(t,y)
>> y1=exp(-0.1*t).*sin(t+1);
>> hold on
>> plot(t,y1,':r')
2.5.2线型、点型和颜色
见 P29 表 2.12
例子:
plot(t,y1,'--r')
2.5.3 多条曲线的绘制
1、用 plot(t,[y;y1])
t=0:0.5:4*pi;
>> y=exp(-0.1*t).*sin(t);
>> y1=exp(-0.1*t).*sin(t+1);
>> plot(t,[y; y1])
0 2 4 6 8 10 12 14
- 0,8
- 0,6
- 0,4
- 0,2
0
0,2
0,4
0,6
0,8
1
Hold 命令
t=0:0.5:4*pi;
>> y=exp(-0.1*t).*sin(t);
plot(t,y)
>> %hold on,plot(t,y1,'g')
>> t2=0:0.2:2*pi;
y2=exp(-0.5*t2).*sin(5*t2+1);
>> hold on??
plot(t2,y2,'--r')
4,plotyy命令
t=0:0.5:4*pi;
y=exp(-0.1*t).*sin(t);
t2=0:0.2:2*pi;
y2=exp(-0.5*t2).*sin(5*t2+1);
y3=5*y2;
plotyy(t,y,t2,y3);
grid,gtext('t,t2')
gtext('y'),gtext('y3‘)
练习
请在同一张图上画出:
Y1=sin(t),’+’红色 ;
y2=cos(2t),’ *’绿色表示;
y3=1+e-0.1tsin(3t),’长划线’黄色表示。
t=0:01:pi/4
其它几个主要的二维绘图命令
1,stem命令,绘制脉冲图;
t=0:0.1:pi
y=sin(t)
stem(t,y)
title(‘stem(t,y)’)
Stairs命令
绘制梯图
t=0:0.1:pi
y=sin(t)
stairs(t,y)
title('stairs(t,y)')
Bar命令
绘制条形图
t=0:0.1:pi
y=sin(t)
bar(t,y,'r')
title('bar(t,y)')
fill命令
在曲线和坐标轴之间的封闭区之间填以指定颜色
t=0:0.1:4*pi
y=sin(t)
fill(t,y,'--b')
title('fill(t,y)')
Subplot命令
格式,subplot(n,m,p),将图形窗口分为 n× m个子图,在第 p个子图上绘制图形
例子
clc
t=0:0.1:2*pi;
y=sin(t);
figure
subplot(2,2,1),stem(t,y)
title('stem(t,y)'),pause
subplot(2,2,2),stairs(t,y,'m'),
title('stairs(t,y)'),pause
subplot(2,2,3),bar(t,y)
title('bar(t,y)'),pause
subplot(2,2,4),fill(t,y,'--b'),title('fill(t,y)')
3、虚数绘图
当 plot(z)中的 z为复数时,matlab把复数的 实部作为 X坐标,虚部作为 Y坐标进行绘图,相当于 plot(real(z),imag(z) );
而 plot(t,z),是以 t为 x轴,z的实部为 y轴,
虚部丢弃。
练习
已知 z=e((-0.1+i) t);请用 plot命令画出 z的图形以及
plot(t,z)
练习
clc
close all
pause off
figure
t=0:0.5:4*pi;
z=exp((-0.1+i)*t);
subplot(2,2,1)
plot(z),pause on
title('复数绘图 plot(z)')
pause
subplot(2,2,2)
plot(t,z),pause
title('复数绘图 plot(t,z)')
subplot(2,2,3)
polar(angle(z),abs(z)),pause
title('polar(angle(z),abs(z))')
subplot(2,2,4)
semilogx(t,z),pause
title('semilogx(t,z)')
图形编辑
背景色变化
2.5.5三维曲线和曲面
1、空间曲线绘制 — plot3
格式,plot3(x,y,z,’r’)
例子?clc
clear all
close all
z=0:0.1:4*pi;x=cos(z);y=sin(z);
figure
plot3(x,y,z,'or')
-1
- 0,5
0
0,5
1
-1
- 0,5
0
0,5
1
0
5
10
15
2、三维曲面的绘制
三维图形的形成原理为:
首先,对 x-y平面用与 x,y坐标轴平行的直线分成若干小矩形;
然后计算矩形网格点上的函数值,得到相应三维曲面上的数据点;
最后将相邻的数据点分别用空间曲线(或小平面)连接,得到三维曲面图形函数 mesh和 surf用来绘制三维曲面。
例 画 z=sinc(r)=sin(r)/r (r是 X- Y平面上的向径 )的图程序
clc
clear all; close all
x=-8:0.5:8;y=x';
X1=ones(size(y))*x;
Y1=y*ones(size(x));
R=sqrt(X1.*X1+Y1.*Y1);
z=sin(R)./R;
mesh(X1,Y1,z); title('mesh(z)')
figure
surf(X1,Y1,z); title('surf(z)')
- 1 0
-5
0
5
10
- 1 0
-5
0
5
10
- 0,5
0
0,5
1
s u r f ( z )
- 1 0
-5
0
5
10
- 1 0
-5
0
5
10
- 0,5
0
0,5
1
m e s h ( z )
Mesh利用直线连接相邻的点
surf利用小平面连接相邻的点
figure
plot(X1,x)
xlabel('X1'),ylabel('
x')
-8 -6 -4 -2 0 2 4 6 8
-8
-6
-4
-2
0
2
4
6
8
X1
x
-8 -6 -4 -2 0 2 4 6 8
-8
-6
-4
-2
0
2
4
6
8
y
Y1
figure
plot(y',Y1')
xlabel('y'),ylabel('Y1')
clc
clear all
close all
x=-8:0.5:8;y=x';
X1=ones(size(y))*x;Y1=y*ones(size(x));
R=sqrt(X1.*X1+Y1.*Y1);
z=sin(R)./R;
mesh(X1,Y1,z)
figure
plot(X1,x)
xlabel('X1'),ylabel('x')
figure
plot(y',Y1')
xlabel('y'),ylabel('Y1')
其实以下三行语句
x=-8:0.5:8;y=x';
X1=ones(size(y))*x;
Y1=y*ones(size(x));
可以用如下一个语句代替
[X1,Y1]=meshgrid(-8:0.5:8; -8:0.5:8)
Matlab动画命令
getframe命令
moviein命令
movie命令练习
Clc
Clear all
Close all
axis equal
M=moviein(16)
for j=1:16
plot(fft(eye(j+16)));
M(:,j)=getframe;
end
movie(M,30)
练习 2? clc
clear all
t=0:0.1:2*pi;
for j=1:10
if rem(j,2)==0
y1=sin(t);
plot(t,y1,'+r');
else
y2=cos(t);
plot(t,y2,'*b');
end
M(:,j)=getframe;
end
pause
movie(M,5)
2.5.1 直角坐标中的两维曲线
1,plot(y)
y=5*rand(1,10)-0.5
plot(y)
title('my first plot')
xlabel('x')
ylabel('y')
plot(t,y)的用法
t=0:0.5:4*pi;
>> y=exp(-0.1*t).*sin(t);% 元素群运算
>> plot(t,y)
>> y1=exp(-0.1*t).*sin(t+1);
>> hold on
>> plot(t,y1,':r')
2.5.2线型、点型和颜色
见 P29 表 2.12
例子:
plot(t,y1,'--r')
2.5.3 多条曲线的绘制
1、用 plot(t,[y;y1])
t=0:0.5:4*pi;
>> y=exp(-0.1*t).*sin(t);
>> y1=exp(-0.1*t).*sin(t+1);
>> plot(t,[y; y1])
0 2 4 6 8 10 12 14
- 0,8
- 0,6
- 0,4
- 0,2
0
0,2
0,4
0,6
0,8
1
Hold 命令
t=0:0.5:4*pi;
>> y=exp(-0.1*t).*sin(t);
plot(t,y)
>> %hold on,plot(t,y1,'g')
>> t2=0:0.2:2*pi;
y2=exp(-0.5*t2).*sin(5*t2+1);
>> hold on??
plot(t2,y2,'--r')
4,plotyy命令
t=0:0.5:4*pi;
y=exp(-0.1*t).*sin(t);
t2=0:0.2:2*pi;
y2=exp(-0.5*t2).*sin(5*t2+1);
y3=5*y2;
plotyy(t,y,t2,y3);
grid,gtext('t,t2')
gtext('y'),gtext('y3‘)
练习
请在同一张图上画出:
Y1=sin(t),’+’红色 ;
y2=cos(2t),’ *’绿色表示;
y3=1+e-0.1tsin(3t),’长划线’黄色表示。
t=0:01:pi/4
其它几个主要的二维绘图命令
1,stem命令,绘制脉冲图;
t=0:0.1:pi
y=sin(t)
stem(t,y)
title(‘stem(t,y)’)
Stairs命令
绘制梯图
t=0:0.1:pi
y=sin(t)
stairs(t,y)
title('stairs(t,y)')
Bar命令
绘制条形图
t=0:0.1:pi
y=sin(t)
bar(t,y,'r')
title('bar(t,y)')
fill命令
在曲线和坐标轴之间的封闭区之间填以指定颜色
t=0:0.1:4*pi
y=sin(t)
fill(t,y,'--b')
title('fill(t,y)')
Subplot命令
格式,subplot(n,m,p),将图形窗口分为 n× m个子图,在第 p个子图上绘制图形
例子
clc
t=0:0.1:2*pi;
y=sin(t);
figure
subplot(2,2,1),stem(t,y)
title('stem(t,y)'),pause
subplot(2,2,2),stairs(t,y,'m'),
title('stairs(t,y)'),pause
subplot(2,2,3),bar(t,y)
title('bar(t,y)'),pause
subplot(2,2,4),fill(t,y,'--b'),title('fill(t,y)')
3、虚数绘图
当 plot(z)中的 z为复数时,matlab把复数的 实部作为 X坐标,虚部作为 Y坐标进行绘图,相当于 plot(real(z),imag(z) );
而 plot(t,z),是以 t为 x轴,z的实部为 y轴,
虚部丢弃。
练习
已知 z=e((-0.1+i) t);请用 plot命令画出 z的图形以及
plot(t,z)
练习
clc
close all
pause off
figure
t=0:0.5:4*pi;
z=exp((-0.1+i)*t);
subplot(2,2,1)
plot(z),pause on
title('复数绘图 plot(z)')
pause
subplot(2,2,2)
plot(t,z),pause
title('复数绘图 plot(t,z)')
subplot(2,2,3)
polar(angle(z),abs(z)),pause
title('polar(angle(z),abs(z))')
subplot(2,2,4)
semilogx(t,z),pause
title('semilogx(t,z)')
图形编辑
背景色变化
2.5.5三维曲线和曲面
1、空间曲线绘制 — plot3
格式,plot3(x,y,z,’r’)
例子?clc
clear all
close all
z=0:0.1:4*pi;x=cos(z);y=sin(z);
figure
plot3(x,y,z,'or')
-1
- 0,5
0
0,5
1
-1
- 0,5
0
0,5
1
0
5
10
15
2、三维曲面的绘制
三维图形的形成原理为:
首先,对 x-y平面用与 x,y坐标轴平行的直线分成若干小矩形;
然后计算矩形网格点上的函数值,得到相应三维曲面上的数据点;
最后将相邻的数据点分别用空间曲线(或小平面)连接,得到三维曲面图形函数 mesh和 surf用来绘制三维曲面。
例 画 z=sinc(r)=sin(r)/r (r是 X- Y平面上的向径 )的图程序
clc
clear all; close all
x=-8:0.5:8;y=x';
X1=ones(size(y))*x;
Y1=y*ones(size(x));
R=sqrt(X1.*X1+Y1.*Y1);
z=sin(R)./R;
mesh(X1,Y1,z); title('mesh(z)')
figure
surf(X1,Y1,z); title('surf(z)')
- 1 0
-5
0
5
10
- 1 0
-5
0
5
10
- 0,5
0
0,5
1
s u r f ( z )
- 1 0
-5
0
5
10
- 1 0
-5
0
5
10
- 0,5
0
0,5
1
m e s h ( z )
Mesh利用直线连接相邻的点
surf利用小平面连接相邻的点
figure
plot(X1,x)
xlabel('X1'),ylabel('
x')
-8 -6 -4 -2 0 2 4 6 8
-8
-6
-4
-2
0
2
4
6
8
X1
x
-8 -6 -4 -2 0 2 4 6 8
-8
-6
-4
-2
0
2
4
6
8
y
Y1
figure
plot(y',Y1')
xlabel('y'),ylabel('Y1')
clc
clear all
close all
x=-8:0.5:8;y=x';
X1=ones(size(y))*x;Y1=y*ones(size(x));
R=sqrt(X1.*X1+Y1.*Y1);
z=sin(R)./R;
mesh(X1,Y1,z)
figure
plot(X1,x)
xlabel('X1'),ylabel('x')
figure
plot(y',Y1')
xlabel('y'),ylabel('Y1')
其实以下三行语句
x=-8:0.5:8;y=x';
X1=ones(size(y))*x;
Y1=y*ones(size(x));
可以用如下一个语句代替
[X1,Y1]=meshgrid(-8:0.5:8; -8:0.5:8)
Matlab动画命令
getframe命令
moviein命令
movie命令练习
Clc
Clear all
Close all
axis equal
M=moviein(16)
for j=1:16
plot(fft(eye(j+16)));
M(:,j)=getframe;
end
movie(M,30)
练习 2? clc
clear all
t=0:0.1:2*pi;
for j=1:10
if rem(j,2)==0
y1=sin(t);
plot(t,y1,'+r');
else
y2=cos(t);
plot(t,y2,'*b');
end
M(:,j)=getframe;
end
pause
movie(M,5)