This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.

1. Draw polar coordinates

Description: Polarplot function was used to draw polar coordinates, and each group of data represented a closed curve, with a total of 20 curves constituting 20 closed concentric curves.

T = linspace (0, 2 * PI, 500); 20 * y = 1 + 0.3 * sin (t) + 0.1 + 0.1 * * sin (30 * t), sin (40 * t); Polarplot (t, y, t, 1.1 * y, t, 1.2 * y, t, 1.3 * y, t, 1.4 * y, t, 1.5 * y, t, 1.6 * y, t, 1.7 * y, t, 1.8 * y, t, 1.9 x, y... T, 2.0 * y, t, 2.1 * y, t, 2.2 * y, t, 2.3 * y, t, 2.4 * y, t, 2.5 * y, t, 2.6 * y, t, 2.7 * y, t, 2.8 * y, t, 2.9 * y, 'our linewidth, 1.5);Copy the code

2. Draw a piecewise function for a single curve (inverse proportional function y=1/x)

Description: When x is close to 0, the inverse proportional function tends to singularity (1/0 tends to infinity), using nan non-number hollow out the graph, you can achieve a curve drawing y=1/x of the whole domain.

X = linspace (- 3,3,500); f = @(x) 1./x; y = f(x); For ii = 1:length(x) if abs(x(ii))<0.03 x(ii) = nan; y(ii) = nan; end end plot(x,y) legend('y=1/x')Copy the code

3. Draw randomly distributed color slices inside the cube

Note: Plot3 was used to draw 12 edges of cube, and fill3 function was used to draw color slices. The position of color slices and filling color were randomly generated.

a = 20; b = 20; c = 20; A1 = [0 0 0 a 0 0 a b 0 0 b 0 0 0 0]; A2 = [0 0 c a 0 c a b c 0 b c 0 0 c]; A3 = [0 0 0 0 0 c 0 b c 0 b 0 0 0 0]; A4 = [a 0 0 a 0 c a b c a b 0 a 0 0]; % drawing figure hold on plot3 (A1 (:, 1), A1 (:, 2), A1 (:, 3), 'k'); plot3(A2(:,1),A2(:,2),A2(:,3),'k'); plot3(A3(:,1),A3(:,2),A3(:,3),'k'); plot3(A4(:,1),A4(:,2),A4(:,3),'k'); view(3) XYZ0 = [0 0 0 1 0 0 1 0 1 0 0 1]; for ii = 1:100 p = [rand*(a-2) rand*(b-2) rand*(c-2)]+1; % center X2= p(1) + XYZ0(:,1); Y2 = p(2) + XYZ0(:,2); Z2 = p(3) + XYZ0(:,3); ColorSpec = rand (1, 3); fill3( X2,Y2,Z2,ColorSpec ) endCopy the code

4. Randomly generate several disjoint smaller circles within the larger circle

Note: draw a large circle first, randomly generate the center of a small circle in the large circle, judge the distance between the center of a small circle and other small circles, if the distance of the center of a small circle is less than the diameter, it means that the small circle intersects, remove the small circle.

tic X = []; Y = []; n = 0; while n < 1000 r = rand * (750-5); theta = rand * 2*pi; x0 = r*cos(theta); y0 = r*sin(theta); s = min( (x0-X).^2 + (y0-Y).^2 ); If s < 10^2 continue; else X = [X;x0]; % Center coordinate set Y = [Y;y0]; End end toc alpha = linspace(0,2* PI,100); x = 5*cos(alpha); y = 5*sin(alpha); figure plot( 150*x,150*y,'r' ) hold on for ii = 1:n x0 = X(ii); y0 = Y(ii); plot(x0+x,y0+y,'k') endCopy the code

5. Generate a random number of spheres in rectangular bodies

Description: The patch function draws the surface of cuboid and sphere and can be filled with color.

x = 100; y = 80; z = 50; Theta = linspace (0, 2 * PI, 50); Phi = linspace (0, 2 * PI, 50); [theta,phi] = meshgrid(theta,phi); r = 2; X0 = r*cos(phi).*cos(theta); Y0 = r*cos(phi).*sin(theta); Z0 = r*sin(phi); % plot X = [0 X X 0 0 X X 0 0 0 0 X X X 0 0 0 X X X 0 X X 0]'; Y = [0 0 y y 0 0 y y 0 y y 0 0 y y 0 y y y y]'; Z = [0 0 0 0 z z z z 0 0 z z 0 0 z z 0 0 z z ]'; figure patch(X,Y,Z,'r'); view(3) hold on for ii = 1:50 p = [rand*(100-4*r) rand*(80-4*r) rand*(50-4*r)]+2*r; % center X2= p(1) + X0; Y2 = p(2) + Y0; Z2 = p(3) + Z0; patch( X2,Y2,Z2,'y' ) endCopy the code

6. Draw the intersection between the cylinder and the surface of the sphere, and draw the intersection curve

Description: Mesh function to draw the surface diagram, solve the equation to get the intersection line parameter equation, plot3 function to draw three-dimensional intersection line.

T = linspace(0,2* PI,200); S = linspace (0, 2 * PI, 200); [t,s] = meshgrid(t,s); x = 2*cos(t); y = 2*sin(t).*cos(s); z = 2*sin(t).*sin(s); Figure mesh(x,y,z) %% 2 t2 = linspace(0,2* PI,200); Z2 = linspace (- 3,3,200); [t2,z2] = meshgrid(t2,z2); x2 = 1 + cos(t2); y2 = sin(t2); Hold on mesh(x2,y2,z2) % t3 = linspace(0,2* PI,200); y3 = sin(t3); x3 = 1 + cos(t3); z3 = sqrt(4-2*x3); plot3(x3,y3,z3,'r','linewidth',5) hold on plot3(x3,y3,-z3,'r','linewidth',5)Copy the code

8. Draw three-dimensional parabolic surfaces

Use nan non – number to hollow out the graph

X = - 20:0. 1:20; Y = - 20:0. 1:20; [X,Y] = meshgrid(x,y); P = 0.2; Q = 0.1; Z = X.^2/(2*p) + Y.^2/(2*q); Z = (Z<=500) .* Z + ((Z>500)-1) ./ ((Z>500)-1); % Figure hollow mesh(X,Y,Z)Copy the code

9. The parabolic surface is animated with the change of parameters

Description: Each step size dynamic update drawing three-dimensional surface, the formation of animation effect.

X = linspace,1,20 (- 1); Y = linspace,1,20 (- 1); [X,Y] = meshgrid(x,y); figure a = 1; Z = a.*X.^2 + Y.^2; h = surf(X,Y,Z); Zlim ([0]) for a = 1-0. 1:10 Z = a. * X ^ 2 + y. ^ 2; set(h,'zdata',Z); Drawnow pause (0.1) in the endCopy the code

10. Square waves are synthesized using sinusoidal waves of different frequencies

Fourier series, the use of different frequencies of sine wave synthesis of square wave, trigonometric function item number, the synthesis of square wave more accurate.

0-0 at t =. 000001:1; f1=6*sin(10*pi*t)/pi; f2=6*sin(10*pi*t)/pi+2*sin(30*pi*t)/pi; f3=6*sin(10*pi*t)/pi+2*sin(30*pi*t)/pi+6*sin(50*pi*t)/(5*pi); %% loop segment N = 10; % count f4 = 0; % the initial value for ii = 1: N f4 = f4 + 3 * 2 * sin (2 * 2-1) * (10 * PI * t)/PI/(2 * 2-1); Subplot (2,2,2),plot(t,f2) subplot(2,2,3),plot(t,f3) subplot(2,2,4),plot(t,f4)Copy the code