Comprehensive Algorithms for MATLAB Plotting

1. Bar Plot

  1. t = -10:1:10;

  2. subplot(2,2,1);

  3. bar(t, cos(t));

Copy code

This creates a vector t containing elements from -10 to 10. In the first subplot, the bar function is used to plot the bar graph of cos(t). The first parameter of the bar function is the x-axis coordinates, and the second parameter is the height or value corresponding to each x coordinate. This subplot shows the variation of cos(t) over the given range. Compass Plot

  1. subplot(2,2,2);

  2. compass(t, cos(t));

Copy code

In the second subplot, the compass function is used to create a polar plot. The compass function takes t as input and cos(t) as the magnitude for the polar coordinates. This plot displays the phase and magnitude information of cos(t). Rose Plot

  1. subplot(2,2,3);

  2. rose(t, cos(t));

Copy code

The third subplot uses the rose function to create a rose plot. The rose function takes the angle vector t and the corresponding values cos(t), then plots the frequency corresponding to the angles on the polar axis. This figure presents the distribution of cos(t) in the form of rose petals. Filled Plot

  1. subplot(2,2,4);

  2. fill(t, cos(t), ‘b’);

Copy code

In the fourth subplot, the fill function is used to create a filled plot. The first parameter of the fill function is the x-axis coordinates, and the second parameter is the corresponding y values for each x coordinate. Additionally, ‘b’ indicates that blue is used for filling. This figure shows the filled effect of cos(t) over the given range. Result Screenshot Below:

Comprehensive Algorithms for MATLAB Plotting2.1.clear: Clear all variables in the MATLAB workspace.2.clc: Clear the contents of the MATLAB command window.Then, after generating the time vector t, two signals y and Y are defined as sin(t) and sin(10*t) respectively. Next, these two signals are multiplied element-wise to obtain a new signal c.Finally, the plot function is used to draw the original signal y (represented by a red dashed line) and the multiplied signal c (represented by a blue solid line) on the same graph. This graph can be used to demonstrate the effect of signal multiplication.

  1. clear

  2. clc

  3. t=0:0.001:10;

  4. y=sin(t);

  5. % plot(t,y);

  6. Y=sin(10*t);

  7. c=y.*Y;

  8. plot(t,y,’r:’,t,c,’b’)

Copy code

Comprehensive Algorithms for MATLAB Plotting3.1.clear: Clear all variables in the MATLAB workspace.2.clc: Clear the contents of the MATLAB command window.Then, a vector x containing four data elements is defined. Next, a zero vector explode of the same size as x is created to set which slice needs to be highlighted.By using the min function, the minimum value c in vector x and the corresponding index offset are found. Then, the position in explode corresponding to the minimum value is set to the minimum value c.Finally, the pie function is used to create a pie chart, where the explode parameter highlights the minimum value. The size of each sector of the pie chart is determined by the elements in vector x.

  1. clear

  2. clc

  3. x=[11.4 23.5 35.4 15.6];

  4. explode=zeros(size(x));

  5. [c,offset]=min(x);

  6. explode(offset)=c;

  7. pie(x,explode)

Copy code

Comprehensive Algorithms for MATLAB Plotting4.1.clear: Clear all variables in the MATLAB workspace.2.clc: Clear the contents of the MATLAB command window.Then, a two-dimensional grid is generated using the meshgrid function, where both x and y are 401×401 matrices representing coordinates in two-dimensional space.Next, the distance r from each point to the center is calculated, and the value of the two-dimensional sinc function z is computed.Finally, the subplot function is used to create a figure window containing two subplots. In the first subplot, the mesh function is used to plot the three-dimensional mesh of the two-dimensional sinc function. In the second subplot, the surf function is used to plot the surface of the sinc function. This allows for a simultaneous comparison of the representations of the two-dimensional mesh and the surface plot.

  1. clear

  2. clc

  3. x=-2:0.01:2;

  4. [x,y]=meshgrid(x,x); %x and y are both 401×401 matrices

  5. r=sqrt(x.^2+x.^2)+eps;

  6. z=sinc(r);

  7. subplot(2,1,1);

  8. mesh(z);

  9. subplot(2,1,2);

  10. surf(x,y,z);

Copy code

Comprehensive Algorithms for MATLAB Plotting5.Using the peaks function to generate a typical mountain-shaped three-dimensional surface, multiple views and effects are displayed in subplots using different plotting functions.

  • meshz function (first subplot): Plots the surface and adds a skirt, showing both the surface and the zero plane.

  • waterfall function (second subplot): Produces a water flow effect in the x direction on the surface plot.

  • meshc function (third subplot): Simultaneously draws the mesh plot and contour lines.

  • surfc function (fourth subplot): Simultaneously draws the surface plot and contour lines.

  • surfl function (fifth subplot): Provides a color surface plot with lighting effects.

  • contourf function (sixth subplot): Draws a contour filled plot, which is a contour plot with color filling.

Each subplot uses axis([-inf inf -inf inf -inf inf]) to set the display range of the axes.

  1. clear

  2. clc

  3. [x,y,z] =peaks;

  4. subplot(2,3,1);

  5. meshz(x,y,z); %Surface with a skirt, showing the surface and zero plane

  6. axis([-inf inf -inf inf -inf inf]);

  7. subplot(2,3,2);

  8. waterfall(x,y,z); %Produces a water flow effect in the x direction

  9. axis([-inf inf -inf inf -inf inf]);

  10. subplot(2,3,3);

  11. meshc(x,y,z); %Simultaneously draws the mesh plot and contour lines

  12. axis([-inf inf -inf inf -inf inf]);

  13. subplot(2,3,4);

  14. surfc(x,y,z); %Simultaneously draws the surface plot and contour lines

  15. axis([-inf inf -inf inf -inf inf]);

  16. subplot(2,3,5)

  17. surfl(x,y,z); %Provides a color surface plot with lighting effects

  18. axis([-inf inf -inf inf -inf inf]);

  19. subplot(2,3,6)

  20. contourf(x,y,z);

  21. axis([-inf inf -inf inf -inf inf]);

Copy code

Comprehensive Algorithms for MATLAB Plotting6.

  1. clear

  2. clc

  3. [X0,Y0,Z0]=sphere(30); %Generate three-dimensional coordinates of the unit sphere

  4. X=2*X0;Y=2*Y0;Z=2*Z0; %Generate three-dimensional coordinates of the sphere with radius 2

  5. clf

  6. subplot(1,2,1);

  7. surf(X0,Y0,Z0); %Draw the unit sphere

  8. shading interp %Use interpolated shading

  9. hold on,mesh(X,Y,Z),colormap(hot),hold off %Use hot colormap

  10. hidden off %Generate perspective effect

  11. axis equal,axis off %Do not display axes

  12. title(‘Perspective View’)

  13. subplot(1,2,2);

  14. surf(X0,Y0,Z0); %Draw the unit sphere

  15. shading interp %Use interpolated shading

  16. hold on,mesh(X,Y,Z),colormap(hot),hold off %Use hot colormap

  17. hidden on %Generate hidden effect

  18. axis equal,axis off %Do not display axes

  19. title(‘Hidden View’)

Copy code

Comprehensive Algorithms for MATLAB Plotting7.

  1. clear

  2. clc

  3. subplot(2,2,1), fplot(@humps, [0 1])

  4. subplot(2,2,2), fplot(@(x) abs(exp(-1i*x*(0:9))*ones(10,1)), [0 2*pi])

  5. % % Vectorize the function for subplot(2,2,3)

  6. % vec_func = @(x) [tan(x),sin(x),cos(x)];

  7. % x_range = linspace(2*pi*(-1), 2*pi*(1), 1000); % Adjust the number of points as needed

  8. % subplot(2,2,3), fplot(vec_func, x_range)

  9. subplot(2,2,4), fplot(@(x) sin(1 ./ x), [0.01 0.1], 1e-3)

Copy code

Comprehensive Algorithms for MATLAB Plotting8.

  1. clear

  2. clc

  3. subplot(3,3,1)

  4. ezplot(‘cos(x)’)

  5. subplot(3,3,2)

  6. ezplot(‘cos(x)’, [0, pi])

  7. subplot(3,3,3)

  8. ezplot(‘1/y-log(y)+log(-1+y)+x – 1’)

  9. subplot(3,3,4)

  10. ezplot(‘x^2 – y^2 – 1’)

  11. subplot(3,3,5)

  12. ezplot(‘x^2 + y^2 – 1’,[-1.25,1.25]);

  13. axis equal

  14. subplot(3,3,6)

  15. ezplot(‘x^3 + y^3 – 5*x*y + 1/5’,[-3,3])

  16. subplot(3,3,7)

  17. ezplot(‘x^3 + 2*x^2 – 3*x + 5 – y^2’)

  18. subplot(3,3,8)

Copy code

Comprehensive Algorithms for MATLAB Plotting9.

  1. clear

  2. clc

  3. t=(0:0.02:2)*pi;

  4. x=sin(t);

  5. y=cos(t);

  6. z=cos(2*t);

  7. plot3(x,y,z,’b-‘,x,y,z,’bd’)

  8. view([-82,58]);

  9. box on

  10. legend(‘Chain’,’Gem’);

Copy code

Comprehensive Algorithms for MATLAB Plotting10.

  1. clear

  2. clc

  3. subplot(2,2,1)

  4. contour3(peaks,50); %Draw contour lines of the surface in three-dimensional space

  5. axis([-inf inf -inf inf -inf inf]);

  6. subplot(2,2,2)

  7. contour(peaks, 50); %Draw the projection of the surface contour lines on the XY plane

  8. subplot(2,2,3)

  9. t=linspace(0,20*pi, 501);

  10. plot3(t.*sin(t), t.*cos(t), t);% Draw curve in three-dimensional space

  11. subplot(2,2,4)

  12. plot3(t.*sin(t), t.*cos(t), t, t.*sin(t), t.*cos(t), -t);% Simultaneously draw two curves in three-dimensional space

Copy code

Comprehensive Algorithms for MATLAB Plotting11.

  1. clear

  2. clc

  3. subplot(1,2,1);

  4. t = 0:0.01:2*pi;

  5. x = cos(2*t).*(cos(t).^2);

  6. y = sin(2*t).*(sin(t).^2);

  7. comet(x,y)

  8. subplot(1,2,2);

  9. t = -10*pi:pi/250:10*pi;

  10. comet3((cos(2*t).^2).*sin(t),(sin(2*t).^2).*cos(t),t)

Copy code

Comprehensive Algorithms for MATLAB Plotting

Edited by / Garvey

Reviewed by / Fan RuiQiang

Checked by / Garvey

Click below

Follow us

Leave a Comment