MATLAB learning 1: Getting started with drawing functions

This blog is a zero foundation and needs to use MATLAB to provide a quick way, only as an introduction or emergency use.

First, please understand the following basic syntax of MATLAB:

x = (0 : 0.1 : 8)

Connected by two colons, the first and last positions are the end positions of the graph being drawn, and the middle value is the sampling interval.

1、plot

For general drawing needs, plot can almost solve most problems.

1.1, drawing

x = (0:0.1:8)
y = 2*x
plot(x,y)

Insert picture description here

1.2. Draw multiple pictures in one coordinate system

Method: Each group of two in the plot function is enough. You can also use the color initials to identify the color

x = (0:0.1:8)
y = 2*x
z = 16*sin(x)
plot(x,y,x,z)

Insert picture description here

x = (0:0.1:8)
y = 2*x
z = 16*sin(x)
plot(x,-x,'g',x,y,'r',x,z,'b')

Insert picture description here

1.3. Draw multiple pictures in one interface

Use the subplot function to determine the drawing position, and then plot. The three parameters are (there are several rows, the total number of columns, and the number one).

x = (0:0.1:8)
y = 2*x
z = 16*sin(x)
subplot(1,2,1)
plot(x,y)
subplot(1,2,2)
plot(x,z)

Insert picture description here

1.4. Adjust the range of the coordinate system

Change the coordinate system range:

x = (0:0.1:8)
y = 2*x
z = 16*sin(x)

subplot(1,2,1)
plot(x,y)
axis([3,5,4,12])

subplot(1,2,2)
plot(x,z)
axis([2,6,-16,16])

Insert picture description here

1.5, add coordinate system annotation

You can add coordinate system information after plot, such as xlabel, ylabel, title, legend, all of which only need to use single quotes to enter the string; in addition, you can use grid on to display grid lines.

x = (0:0.1:8)
y = 2*x

plot(x,y)

xlabel('自变量')       %y轴注解
ylabel('函数')         %y轴注解
title('一次函数的图形') %图形标题
legend('y = 2 * x')    %图形注解
grid on                %显示格线

Insert picture description here

2、bar

The bar function is used as a histogram in the drawing. The basic usage is the same as before. Let's combine the above study to understand the bar function and use it as a review.

2.1, compare the figure of 1.5

x = (0:0.1:8)
y = 2*x

bar(x,y)

xlabel('自变量')
ylabel('函数')
title('一次函数的图形')
legend('y = 2 * x')
grid on

Insert picture description here

2.2. Comprehensive review

Except that it is impossible to draw multiple pictures in one coordinate system, other operations can be realized. As shown in the following code:

x = (0:0.5:8)
y = 2*x

subplot(2,2,1)
bar(x,y,'r')
axis([-1,9,-1,18])

xlabel('自变量')
ylabel('函数')
title('一次函数的图形')
legend('y = 2 * x')
grid on

subplot(2,2,4)
bar(x,-y,'b')
axis([-1,9,-18,1])

xlabel('自变量')
ylabel('函数')
title('一次函数的图形')
legend('y = -2 * x')
grid on

Insert picture description here

2.3, the bar3 function of three-dimensional graphics

For three-dimensional graphics, first use a matrix to determine the position of each value, and then the value of each position will be automatically reflected as the amplitude.

a = (1:0.5:3)
b = (3:-0.5:1)
c = a'*b

Here a'is the transposition of a. After calculation, the following content is obtained. Here you can see the c matrix.
Insert picture description here
Next, we will draw the c matrix. Since the initial perspective is not clear, we can use the view function to change the perspective.
Insert picture description here
After changing the perspective and finishing as follows:

a = (1:0.5:3)
b = (3:-0.5:1)
c = a'*b

bar3(c)
view(140,20)

xlabel('x')
ylabel('y')
zlabel('z')
title('3D视图')
grid on

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_42979679/article/details/103002008