Implicit function and 3D drawing based on MATLAB (with drawings and codes)

1. Special two-dimensional graphics drawing statement

bar(x,y) 2D bar chart
compass(x,y) compass chart
feather(x,y) feathered figure
hist(y,n) histogram
polar(x,y) Polar plot
stairs(x,y) Ladder graphics
semilogx(x,y) x-semi-log plot
comet(x,y) comet track
errorbar(x,y,ym,yM) Limits of Error Graphics
fill(x,y,c) 2D filled graph
loglog(x,y) logarithmic graph
quiver(x,y) magnetic field diagram
stem(x,y) Matchstick Illustration
semilogy(x,y) y-semi-log plot

Example 1

Draw Polar Curve

\rho=5sin(\frac{4\theta}{3}),\quad \rho=5sin(\frac{\theta}{3})

untie:

MATLAB code:

clc;clear;

theta=0:0.01:6*pi; %周期可以利用试凑方法确定
rho=5*sin(4*theta/3);
polar(theta,rho)
figure,
rho=5*sin(theta/3);
polar(theta,rho)

operation result:

Example 2

Representing sinusoids with different curve-plotting functions

untie:

code:

clc;clear;

t=0:.2:2*pi;
y=sin(t); %生成绘图所用的数据

subplot(2,2,1),stairs(t,y) %分割窗口,在左上角绘制阶梯曲线

subplot(2,2,2),stem(t,y) %火柴杆曲线绘制

subplot(2,2,3),bar(t,y) %条形图

subplot(2,2,4),semilogx(t,y)  %横坐标为对数的曲线

 operation result:

example

code:


clc;clear;
t=(1/16:1/8:1)*2*pi;
x=exp(t).*sin(t);
y=t.*cos(t);
fill(x,y,'k'),  %黑色
grid on

 operation result:

Sometimes it may be necessary to view the effect of drawing curves in multiple graphics windows. At this time, you can use the figure command to create multiple windows.

Example 2

Plot the following functions in two windows:

y1=cos(5x)+0.1x^2,\quad y2=sin(3x)+0.1x

untie:

code:

clc;clear;
x=0:0.01:5;
y1=cos(5*x)+0.1*x.^2;
plot(x,y1)
y2=sin(3*x)+0.1*x;
figure(2),
plot(x,y2)

 operation result:

You can also add some annotations to the graph.

Example 3

Plot the following functions separately, and add some labels.

sin(x),sin(5x),sin(x)+sin(5x)

untie:

MATLAB code:

clc;clear;
x=0:0.04:20;
y1=sin(x);
y2=sin(5*x);
y3=y1+y2;
plot(x,y1,'*',x,y2,x,y3,'.') %y1用*表示,y2用默认的细线表示,y3用虚线点表示
legend('sin(x)','sin(5x)','sin(x)+sin(5x)') %分别对用上面表示给出图例
xlabel('x') %x轴标注
ylabel('y') %y轴标注
title('the function of sin') %标题
grid %加坐标格

 operation result:

2. Implicit function drawing

Implicit function form:

f(x,y)=0

Format:

ezplot(隐函数表达式)

 Example 4

Drawing:

f(x,y)=x^2sin(x+y^2)+y^2e^{x+y}+5cos(x^2+y)=0

untie:

MATLAB code:

clc;clear;

%x自选
ezplot('x^2*sin(x+y^2)+y^2*exp(x+y)+5*cos(x^2+y)')

%限定范围
figure,
ezplot('x^2*sin(x+y^2)+y^2*exp(x+y)+5*cos(x^2+y)',[-10 10])

operation result:

3. 3D image rendering

3.1 Three-dimensional curve

Format:

%格式1
plot3(x1,y1,z1)

%格式2
plot3(x1,y1,z1,选项1,x2,y2,选项2,···,xm,ym,zm,选项m)

Moreover:

stem3 : 3D matchstick curve

fill3 : 3D filled graphics

bar3 : 3D histogram

Example 5

Plot the parametric equation:

x(t)=t^3sin(3t)e^{-t},\quad y(t)=t^3cos(3t)e^{-t},\quad z=t^2

untie:

MATLAB code:

clc;clear;
t=0:.1:2*pi; %构造t向量,注意下面的点运算
x=t.^3.*sin(3*t).*exp(-t);
y=t.^3.*cos(3*t).*exp(-t);
z=t.^2;
plot3(x,y,z), %三维曲线绘制
grid
figure,
stem3(x,y,z);
hold on;
plot3(x,y,z),
grid

 operation result:

3.2 Three-dimensional surfaces

Format:

[x,y]=meshgrid(v1,v2)
z=x.*y

surf(x,y,z) %绘制表面图
mesh(x,y,z)%绘制网格图

surfl()  under light

surfc()  Contour

waterfall()  waterfall type

contour() contour3()  Contour

Example 6

For the Butterworth filter, u_0and v_0take 16 as the center of the region. assumption D_0=200,n=2.

H(u,v)=\frac{1}{1+D^{2n}(u,v)/D_0}

D(u,v)=\sqrt{(u-u_0)^2+(v-v_0)^2}

untie:

The MATLAB code is as follows:

clc;clear;
[x,y]=meshgrid(0:31);
n=2;
D0=200;
D=sqrt((x-16).^2+(y-16).^2); %求距离
z=1./(1+D.^(2*n)/D0); %计算
mesh(x,y,z)  %绘制滤波器
axis([0 31 0 31 0 1]) %重新设置坐标系,增大可读性
figure,
surf(x,y,z) %绘制三维表面图
figure,
contour3(x,y,z,30)  %三维等高线图,30条等高线

operation result:

Example 7

try to plot a binary function

z=f(x,y)=\frac{1}{\sqrt{(1+x)^2+y^2}}+\frac{1}{\sqrt{(1-x)^2+y^2}}

untie:

code show as below:

clc;clear;
xx=[-2:.1:-1.2,-1.1:0.02:-0.9,-0.8:0.1:0.8,0.9:0.02:1.1,1.2:0.1:2];
yy=[-1:0.1:-0.2,-0.1:0.02:0.1,0.2:0.1:1];
[x,y]=meshgrid(xx,yy);
z=1./(sqrt((1-x).^2+y.^2))+1./(sqrt((1+x).^2+y.^2));
surf(x,y,z),
shading faceted;
set(gca,'zlim',[0,20]) %设置当前坐标轴对象的句柄的值

 operation result:

Example 8

Draw three views of the Butterworth filter.

untie:

code show as below:

clc;clear;
[x,y]=meshgrid(0:31);
n=2;
D0=200;
D=sqrt((x-16).^2+(y-16).^2); %求距离
z=1./(1+D.^(2*n)/D0); %计算

subplot(221),surf(x,y,z),axis([0 31 0 31 0 1]);
view(0,90); %俯视图

subplot(222),surf(x,y,z),axis([0 31 0 31 0 1]);
view(90,0); %侧视图

subplot(223),surf(x,y,z),axis([0 31 0 31 0 1]);
view(0,0); %正视图

subplot(224),surf(x,y,z),axis([0 31 0 31 0 1]);
 %三维图

operation result:

view(\alpha,\beta)

The first angle is used to indicate the horizontal direction, that is, the included angle in the negative direction of the y-axis, and the default value is -37.5 degrees;

The second angle is used to indicate the vertical direction, and the default value is 36 degrees.

4. Visualization of symbolic operations

In addition to drawing images for numerical results, MATLAB also provides a wealth of symbolic drawing functions. Symbolic drawing commands usually start with the letter ez .

Example 9

The deformed helix is ​​drawn using the symbolic drawing method. The parametric equations are as follows:

x=t,y=sint,z=2cost,\quad t\in [0,20]

untie:

code show as below:

clc;clear;
syms t;
x=t;
y=sin(t);
z=2*cos(t);
ezplot3(x,y,z,[0,20])
grid on

operation result:

Example 10

Use the mesh drawing method to draw the ellipsoid, and the ellipsoid parameter equation is as follows:

x=sint_1cost_2,y=16sin(t_1)sin(t_2),z=2cost_1

untie:

MATLAB code:

clc;clear;
syms t1 t2;
x=sin(t1)*cos(t2);
y=16*sin(t1)*sin(t2);
z=2*cos(t1);
ezmesh(x,y,z,[0,pi,0,2*pi])
hidden off %透视效果
%注意hidden off效果对ezsurf无效

 operation result:

Example 11

Use the ezsurf drawing method to draw the tire-shaped torus, and the parameter equation is as follows:

x=cost(3+cosu),y=sint(3+cosu),z=sinu,\quad t\in[0,2\pi],u\in [0,2\pi]

untie:

The MATLAB code is as follows:

clc;clear;
syms t u;
x=cos(t)*(3+cos(u));
y=sin(t)*(3+cos(u));
z=sin(u);
ezsurf(x,y,z,[0,2*pi,0,2*pi])
axis off %不显示坐标轴

operation result:

5. Complex number drawing

There are two main forms for plotting complex functions.

One is the Cartesian coordinate diagram ( plot ), that is, the representation diagram of the complex number is made with the real part and the imaginary part of the complex number as coordinates;

The other is the polar coordinate diagram ( polar ), that is, the coordinates of the modulus and argument of the complex number are plotted.

example

code:

clc;clear;
t=0:0.1:2*pi;
y=t+i*t.*sin(t);
r=abs(y);
theta=angle(y);

subplot(121),plot(y),
title('直角坐标图')

subplot(122),polar(theta,r),
title('极坐标图')

operation result:

Guess you like

Origin blog.csdn.net/forest_LL/article/details/124568275