MATLAB drawing exercise (2)

Posted by fat creative on Sat, 15 Feb 2020 16:03:01 +0100

Multi graphic display

1. Graph Segmentation

subplot(m,n,p) divides the current window into m*n view areas, and specifies the p view as the current view.

Plot ('position ', [left right width height]): the location of the new sub region generated is specified by the user, and the following four tuples are the specific parameters of the region. The value range of width and height is [0,1].

>> subplot(2,1,1)
>> subplot(2,1,2)

Display 4 * 4 graphic segmentation.

>> t1=(0:11)/11*pi;
>> t2=(0:400)/400*pi;
>> t3=(0:50)/50*pi;
>> y1=sin(t1).*sin(9*t1);
>> y2=sin(t2).*sin(9*t2);
>> y3=sin(t3).*sin(9*t3);
>> subplot(2,2,1),plot(t1,y1,'r.')
>> axis([0,pi,-1,1]),title('(1)Discrete graph with too few points')
>> subplot(2,2,2),plot(t1,y1,t1,y1,'r.')
>> axis([0,pi,-1,1]),title('(2)Continuous graph with too few points')
>> subplot(2,2,3),plot(t2,y2,'r.')
>> axis([0,pi,-1,1]),title('(3)Point dense discrete graph')
>> subplot(2,2,4),plot(t3,y3)
>> axis([0,pi,-1,1]),title('(4)Point enough continuous graph')

2. Graphic overlay

The hold on/off command is used to control the hold or not hold of the original graph

>> N=9;
>> t=0:2*pi/N:2*pi;
>> x=sin(t);y=cos(t);
>> tt=reshape(t,2,(N+1)/2);
>> tt=flipud(tt);
>> tt=tt(:);
>> xx=sin(tt);yy=cot(tt);
>> plot(x,y)
>> hold on
>> plot(xx,y)
>> hold off
>> plot(xx,y)



The fplot command is a special command for drawing images. The image made with it is smoother and more accurate than that made with plot command.

Make the image of function y=sinx, y=(sinx)^3, x ∈ [1,4].

>> subplot(2,1,1),fplot(@(x)sin(x),[1,4]);
>> subplot(2,1,2),fplot(@(x)sin(x).^3,[1,4]);

The image of function y=sin(1/x),x ∈ [0.01,0.02] is obtained.

>> x=linspace(0.01,0.02,50);
>> y=sin(1./x);
>> subplot(2,1,1),plot(x,y)
>> subplot(2,1,2),fplot(@(x)sin(1./x),[0.01,0.02])

The ezplot command is used to graph a symbolic function.

Draw the image of implicit function f1(x)=e^(2x)sin(2x),x ∈ (- π, π).

>> syms x
>> f1 = exp(2*x)*sin(2*x);
>> subplot(2,2,1),ezplot(exp(2*x),[-pi,pi])
>> subplot(2,2,2),ezplot(sin(2*x))
>> subplot(2,2,3),ezplot(exp(2*x)+sin(2*x),[-pi,pi,0,2*pi])
>> subplot(2,2,4),ezplot(f1,[-4*pi,4*pi])

Reference: MATLAB2016 advanced application and simulation, edited by Li Jin, Liu Tao, etc., China Machine Press