数据可视化四:Matlab数据可视化(三)

数据可视化四:Matlab数据可视化(三)

目录:

1、绘制二维图形的常用函数。

2、绘制三维图形的常用函数。

3、绘制图形的辅助操作。


1、设 ,在 x=0~2π区间取 101 点,绘制函数的曲线。

示例代码如下:

close all;

x=0 :pi / 50 : 2*pi;

y = (0.5 + 3 * sin(x)./ (1 + x.^2)).* cos(x);

plot(x,y);

运行结果如下:

 

图1.1 运行结果

 

2、已知,完成下列操作:

(1) 在同一坐标系下用不同的颜色和线型绘制三条曲线。

示例代码如下:

close all;

x=linspace(0,2*pi,101);

y1=x.^2;

y2=cos(2*x);

y3=y1.*y2;

plot(x,y1, 'g*');

hold on;

plot(x,y2,'ro');

hold on;

plot(x,y3,'b+');

运行结果:

 

图2.1 运行结果1

(2) 以子图形式绘制三条曲线。

示例代码如下:

close all;

x=linspace(0,2*pi,101);

subplot(3,1,1);

y1=x.^2;

plot(x,y1);

legend('y1=x.^2');

title('y1');

subplot(3,1,2);

y2=cos(2*x);

plot(x,y2);

legend('y2=cos(2*x)');

title('y2');

subplot(3,1,3);

y3=y1.*y2;

plot(x,y3);

legend('y3=y1.*y2');

title('y3');

运行结果:

 

图2.2 运行结果2

(3) 分别用条形图、阶梯图、杆图和填充图绘制三条曲线。

①条形图代码示例:

close all;

x=linspace(0,2*pi,101);

subplot(3,1,1);

y1=x.^2;

bar(x,y1);

legend('y1=x.^2');

title('y1');

subplot(3,1,2);

y2=cos(2*x);

bar(x,y2);

legend('y2=cos(2*x)');

title('y2');

subplot(3,1,3);

y3=y1.*y2;

bar(x,y3);

legend('y3=y1.*y2');

title('y3');

运行结果:

 

图2.3 运行结果3

②阶梯图代码示例:

close all;

x=linspace(0,2*pi,101);

subplot(3,1,1);

y1=x.^2;

stairs(x,y1);

legend('y1=x.^2');

title('y1');

subplot(3,1,2);

y2=cos(2*x);

stairs(x,y2);

legend('y2=cos(2*x)');

title('y2');

subplot(3,1,3);

y3=y1.*y2;

stairs(x,y3);

legend('y3=y1.*y2');

title('y3');

运行结果:

 

图2.4 运行结果4

③杆图示例代码:

close all;

x=linspace(0,2*pi,101);

subplot(3,1,1);

y1=x.^2;

stem(x,y1);

legend('y1=x.^2');

title('y1');

subplot(3,1,2);

y2=cos(2*x);

stem(x,y2);

legend('y2=cos(2*x)');

title('y2');

subplot(3,1,3);

y3=y1.*y2;

stem(x,y3);

legend('y3=y1.*y2');

title('y3');

运行结果:

 

图2.5 运行结果5

④填充图代码示例:

close all;

x=linspace(0,2*pi,101);

subplot(3,1,1);

y1=x.^2;

fill(x,y1,'r');

legend('y1=x.^2');

title('y1');

subplot(3,1,2);

y2=cos(2*x);

fill(x,y2,'r');

legend('y2=cos(2*x)');

title('y2');

subplot(3,1,3);

y3=y1.*y2;

fill(x,y3,'r');

legend('y3=y1.*y2');

title('y3');

运行结果:

 

图2.6 运行结果6

 

3、已知:

 

在-5≤x≤5 区间绘制函数曲线。

答:示例代码如下:

close all;

x=-5:0.1:5;

y=(x+ sqrt(pi))/(exp(2)).*(x<=0)+1/2*log(x+sqrt(1+x.^2)).*(x>0);

plot(x,y);

运行结果:

 

图3.1 运行结果

 

4、绘制极坐标曲线ρ=asin(b+nθ),并分析参数 a、b、n 对曲线形状的影响。

答:示例代码如下:

close all;

x=-2*pi:pi/100:2*pi;

subplot(2,2,1);

a=input('input a:');

b=input('input b:');

n=input('input n:');

y=a*sin(b+n*x);

polar(x,y);

title(sprintf('a=%d,b=%d,n=%d',a,b,n));

subplot(2,2,2);

a=input('input a:');

b=input('input b:');

n=input('input n:');

y=a*sin(b+n*x);

polar(x,y);

title(sprintf('a=%d,b=%d,n=%d',a,b,n));

subplot(2,2,3);

a=input('input a:');

b=input('input b:');

n=input('input n:');

y=a*sin(b+n*x);

polar(x,y);

title(sprintf('a=%d,b=%d,n=%d',a,b,n));

subplot(2,2,4);

a=input('input a:');

b=input('input b:');

n=input('input n:');

y=a*sin(b+n*x);

polar(x,y);

title(sprintf('a=%d,b=%d,n=%d',a,b,n));

①当b、n的值不变,只改变a的值时,

运行结果:

 

图4.1 运行结果1

②当a、n的值不变,只改变b的值时,

运行结果:

 

图4.2 运行结果2

③当a、b的值不变,只改变n的值时,

 

图4.3 运行结果3

当b、n的值固定时,a决定图形的大小,当a为整数时,图形半径大小就是a;

当a、n的值固定时,b决定图形的旋转角度,图形的形状及大小不变;

当a、b的值固定时,n决定图形的扇叶数,当n为奇数时,扇叶数为n,当n为偶数时,扇叶数为2n。

 

5、绘制函数的曲面图和等高线。

 

其中 x 的 21 个值均匀分布[-5,5]范围,y 的 31 个值均匀分布在[0,10],要求使用

subplot(2,1,1)和 subplot(2,1,2)将产生的曲面图和等高线图画在同一个窗口上。

答:示例代码如下:

close all;

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);

colormap jet;

subplot(2,1,1);

surf(x,y,z);

title('曲面图');

subplot(2,1,2);

surfc(x,y,z);

title('等高线图');

运行结果:

 

图5.1 运行结果

 

6、绘制曲面图形:

 

答:示例代码如下:

close all;

s=linspace(0,pi/2,100);

t=linspace(0,3*pi/2,100);

[s,t]=meshgrid(s,t);

x=cos(s).*cos(t);

y=cos(s).*sin(t);

z=sin(s);

colormap jet;

surf(x,y,z);

运行结果:

 

图6.1 运行结果

 

  • 12
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值