Re-read the introductory materials of MATLAB, and found many forgotten simple functions and techniques (2)-commonly used mathematical functions, control structures, 2D and 3D drawing

Preface

Immediately after the last learning content, continue to learn the introductory knowledge of MATLAB, and record the omissions on the way of learning and using.

Commonly used mathematical functions

Trigonometric functions and various commonly used functions such as minimum, maximum, summation, square root functions, etc., are naturally well understood. Here are some common functions that are forgotten in applications.

exp(x)——Exponential function

The exponential function is naturally faster to use this built-in function directly, and the return result is e to the power of x, and the value of e is about 2.71828. Of course, the default e of this value is not predefined, but there is in the function, so use it directly Enough.

log(x)-natural logarithm
log10(x)-base 10 logarithm

The above two logarithmic functions are also commonly used, but they have been useless for a long time and feel a little strange, so I can also use them.

sign(x)-sign function

When multiplying and dividing, it is very convenient to directly take the sign, no need to use the control structure statement to write.

Some usages of control structure

Needless to say, simple conditional branches, loops are also very simple, here to talk about the focus of the for loop.

for loop variable = expression 1: expression 2: expression 3

Loop statement
end

Very strong, I forgot expression 2 before, and only used expression 1 and expression 3. There is still a lot of time for extra processing.

此外,for语句之后还可以接矩阵或者向量

For a matrix or a vector, we always take out the elements according to the column, and then use the taken out elements to execute the loop statement.
————Attention, it must be taken according to the column.

MATLAB file processing

M files, M function files, picture files, and data files will not be unfamiliar if you think about it for a few years. If you are unfamiliar, it is probably the writing and reading of external files. If you have a problem, you can directly check the corresponding blog to learn, I have already studied, no problem, hhhhhhhh.

Drawing

Two-dimensional mapping

plot-plotting for a vector or matrix

This is the most commonly used and easy to use simple two-dimensional plotting function.

Several commonly used commands
xlabel,ylabel

Axis label function

title

Above the figure, add a title

grid on/grid off

At the beginning, turn off the grid-depending on the situation, sometimes adding a grid will show the data very clearly.

text-add text at the specified location

Here comes the point. I use the one-size title to indicate that I rarely added it before. I have forgotten this function a bit. I hereby remind myself.

axis-control the range of x and y coordinates

It is also rarely used, because in MATLAB, it will limit the appropriate range.

What should I do when it is inappropriate? Drag it to a suitable position, haha.

List it, and learn if you don’t know it. Sometimes it’s very convenient, such as realizing predictable drawing effects and wanting to get a certain style of image.

Attach a piece of practice code

x=linspace(-pi,pi,1000);
y=sin(x);
plot(x,y,'g-');
hold on
grid  on
xlabel('x');
ylabel('sin(x)');
title("y=sin(x)在[-pi,pi]上的图像");
text(0,0,'(0,0)');
legend("sin(x)");

The image made is as follows:
Insert picture description here

Here comes the important point. Some commonly used image signs (the most commonly used ones are skipped directly, and the more commonly used but easy to forget), many signs, not easy to remember, are listed here.
colour

g —— green
c —— cyan
m —— purple
k —— black (k is actually black)
w —— white

Dot notation

s —— square
d —— rhombus
v —— downward triangle
^ —— upward triangle
<—— left triangle
> —— right triangle
p —— five-pointed star
h —— hexagon

Linear notation

-—— Solid line
: —— Dotted line
-.
—— Dotted line – Dotted line

In addition to plot drawing, there are other two-dimensional drawing functions

loglog-drawing using logarithmic coordinate system
semilogx-the abscissa is a logarithmic coordinate system
semilogy——The ordinate is a logarithmic coordinate system
polar-Polar coordinate map
bar-histogram
errorbar-error bar graph
pie-pie chart
hist-statistical histogram

The above are some commonly used two-dimensional drawing commands. You can select different drawing commands and draw different images according to your needs. The two-dimensional drawing commands are simple. As long as you spend some time, there will be no big problems. If you don’t understand the commands listed above, you can study them to avoid rushing to use them.

In fact, it doesn't matter if you refuse to take the time to look at it. You only need to know what command is and what image, and then use help to view, or directly search for the corresponding function name, you can directly use it.

Three-dimensional mapping

plot3 plotting

One more dimension than plot, and many other things can inherit plot

figure
t=0:pi/100:10*pi;
x=sin(t);
y=cos(t);
z=t;
plot3(x,y,z,'k.-');

Insert picture description here
Insert picture description here

mesh、surf与meshgrid

Example of 3D grid

figure
grid off
[x,y]=meshgrid(-pi:0.1:pi);
z=sin(x).*sin(y);
surf(x,y,z)

Insert picture description here

Concluding remarks

I have finished reading the introductory readings here. The notes are as above. I have learned a lot. The biggest gain is that I have a good understanding of the overall framework of MATLAB. I have learned many functions that can be implemented flexibly. If I have the opportunity, I will continue to study MATLAB in depth, and I will re-learn and organize the other knowledge I have learned. If there are any shortcomings, I am grateful.

May you and me make progress together.

The entire code is as follows
clear
clc

format long

exp(1)
log(1)
log(2.71828)
% 厉害了
log(exp(1))

log10(10)

sign(-1)
sign(-10)
sign(0)
sign(4)

for i=1:2:10
    i
end

x=[1,2,3]
for k=x
    k
end

x=x'
for k=x
    k
end

x=[1,2;3,4]

for k=x
    k
end

x=linspace(-pi,pi,1000);
y=sin(x);
plot(x,y,'g-');
hold on
grid  on
xlabel('x');
ylabel('sin(x)');
title("y=sin(x)在[-pi,pi]上的图像");
text(0,0,'(0,0)');
legend("sin(x)");

figure
t=0:pi/100:10*pi;
x=sin(t);
y=cos(t);
z=t;
plot3(x,y,z,'k.-');
grid on

figure
grid off
[x,y]=meshgrid(-pi:0.1:pi);
z=sin(x).*sin(y);
surf(x,y,z)

Guess you like

Origin blog.csdn.net/qq_41563270/article/details/108357313