(1) Basic functions
function |
effect |
lift chestnuts |
clear |
Clear all variables in the workspace |
|
clc |
Clear all code from the command line |
|
help function name or doc function name |
Find function help |
|
sums |
define symbol variable |
syms x y ; |
sym('f') |
define a symbolic expression |
sym('x*y^2') |
pi |
Pi (matlab is case sensitive) |
Pi |
vpa(x,k) |
The value of x, retaining k digits of significant digits |
x=vpa(sin(1/3), 2) ⇨x=0.33 |
real(x) |
Find the real part of a complex number x |
x = real(1+2i) ⇨ x = 1 |
eval() |
string to value |
x = eval('5') ⇨ x = 5 |
sqrt() |
Square root |
x = sqrt(4) ⇨ x = 2 |
exp() |
Exponential function based on the natural constant e |
x = exp(2) ⇨ y = e² |
log() |
logarithmic function to base e |
x = log(exp(1)) ⇨ x = 1 |
log10() |
base 10 logarithmic function |
x = log(10) ⇨ x = 1 |
abs() |
take the absolute value |
x = abs(-1) ⇨ x = 1 |
mod(m,n) |
m takes the remainder of n and returns the remainder of m/n |
x = mod(3,2) ⇨ x = 1 |
Supplementary note: "clear variable name" can clear the specified variable in the workspace (if there are multiple variables, separate them with spaces)
% 例1
syms x;
x1=1.5; % x1变为double型
% 例2
x1=1.5; % x1变为double型
% 例3
syms x y;
f3 = x*y^2;
% 例4
f4=sym('x*y^2') % 例3<=>例4,作用相同
% 例5
f5=sym(A) % 将非符号对象(如,数字,表达式,变量等)A转换为符号对象,并存储在符号变量f5中
(2) Trigonometric function related
function |
effect |
Remark |
sin()、cos()、tan()、cot() |
Trigonometric functions |
x=sin(deg2rad(30)) ⇨x=0.5 |
rad2deg()、deg2rad() |
Radian to Angle, Angle to Radian |
x=deg2rad(30) ⇨ x=0.5236 |
asin()、acos()、 atan()、acot() |
Inverse trigonometric function, result in radians |
x = asin(0.5) ⇨ x = 0.5236 |
asind()、acosd()、atand()、acotd() |
Inverse trigonometric function, the result is an angle value |
x = asind(0.5) ⇨ x = 30.0° |
Supplementary explanation
atan(a/b): Calculate the corresponding angle according to the tangent value a/b, and the returned angle range is [-π/2, π/2]
atan2(a,b): According to the tangent value a/b and point Find the angle in the quadrant where (b, a) is located, and the returned angle range is [-π, π]
% 例子
theta = rad2deg(atan(sin(deg2rad(45))/cos(deg2rad(45))))
% 结果 theta = 45
theta = rad2deg(atan2(sin(deg2rad(45)),cos(deg2rad(45))))
% 结果 theta = 45
theta = rad2deg(atan(sin(deg2rad(135))/cos(deg2rad(135))))
% 结果 theta = -45.0000
theta = rad2deg(atan2(sin(deg2rad(135)),cos(deg2rad(135))))
% 结果 theta = 135
(3) Related to vector and matrix operations
function |
effect |
Remark |
size(A) |
Returns the number of rows and columns of a matrix |
|
size(C,1) / size(C,2) |
Returns the number of rows/columns of the matrix |
|
length(A) 或 max(size(A)) |
Returns the length of the dimension with the largest length |
|
name(s) |
Returns the total number of matrix elements |
|
sum(A) |
Returns the sum of all elements of a matrix |
x = sum([1,2]) ⇨ x = 3 |
inv(A) |
Returns the inverse of matrix A |
|
ndims(A) |
Returns the dimensions of matrix A |
Single values, vectors, and two-dimensional matrices all return 2 |
iscolumn(x) / isrow(x) |
Determine whether it is a column vector/row vector |
|
isvector() / ismatrix() |
Determine whether it is a vector/matrix |
|
isempty(x) / isscalar() |
Determine whether it is an empty vector/single value |
|
dot(a, b) |
vector a dot b, dot product |
|
cross(a, b) |
Vector a is cross multiplied by b, cross multiplication |
|
repmat(A,m,n) |
By copying A with m rows and n columns, return an m*n matrix |
|
A = [1]
U = repmat(A,2,2)
% 结果
U = [1 1
1 1]
(4) Solving equations and symbolic expressions
function |
effect |
Remark |
simplify(f) |
Simplify the formula f |
|
collect(f) |
Merger of similar items |
|
expand(f) |
Expand the formula |
|
horner(f) |
Nest the multiplication |
Numerical computing performance is better |
factor(f) |
factorization |
|
pretty(f) |
Display formulas relatively intuitively |
Complicated formulas do not work |
[n,d] = numden(f) |
Common division, return denominator n, numerator d |
Automatically simplify expressions before dividing |
- Solving Trigonometric Equations
% 解三角函数方程
clear;clc;
syms theta1
x=0;y=670;L1=260;L2=260;L3=150;
% 方法一:复杂
eqn = 2*x*(L1+L3)*cos(theta1)+2*y*(L1+L3)*sin(theta1) == x^2+y^2+(L1+L3)^2-L2^2;
theta1 = solve(eqn,theta1);
theta1 = real(theta1)
% 方法二:推荐,简单
eqn = 2*x*(L1+L3)*cos(theta1)+2*y*(L1+L3)*sin(theta1) == x^2+y^2+(L1+L3)^2-L2^2;
theta1 = solve(eqn,theta1,'Real',true)
(5) Drawing related
Use matlab's plot()/polarplot() to draw pictures
figure() |
create canvas |
clf() |
Clear the contents of the canvas |
set() |
Set graphics object properties, such as canvas name, position on the screen, etc. |
plot(X,Y) |
Create a 2-D line plot of the data in Y against the corresponding values in X |
plot3() |
draw 3D image |
xlabel()、ylabel() |
横轴、纵轴标签 |
xlim()、ylim() |
横轴、纵轴范围 |
text() |
标注图线名称 |
title() |
添加图题 |
hold on / hold off |
不刷新画布(在一张画布画多张图) / 刷新画布 |
legend() |
添加图例 |
grid on / grid off |
打开 / 关闭网格线 |
grid minor |
打开最小网格线 |
box on / box off |
打开 / 关闭 右、上边框线 |
save() |
保存工作区变量 |
saveas() / imwrite() |
保存图片 |
% 例子
clc;clear;close all;
x=0:0.1:pi; y=sin(x); z=cos(x);
h1 = figure(1); % 创建画布,画布编号为1
set(h1,'name','图1');
set(h1,'pos',[350 250 850 340]);
% 线宽、数据点标记形状、标记填充颜色、标记框线颜色、标记大小
p1 = plot(x,y,x,z,'linewidth',2,'Marker','s','MarkerFaceColor','w','MarkerEdgeColor','g','MarkerSize',10);
xlabel('X');ylabel('Y');
xlim([0,pi]);ylim([-2,2]);
set(get(gca,'XLabel'),'FontSize',8); % 设置X轴数字大小
title('足端轨迹');
text(pi,0,'正弦'); text(pi/2,0,'余弦');
[a,b] = max(y);
text(x(b),a,'正弦函数值最大点');
hold on;
p2 = plot(x,y+z,'r','linewidth',2,'Marker','s','MarkerFaceColor','w'); % 线颜色、线形
legend('sin x','cos x','Fontsize',12,'Location','southeast','fontname','Times');
legend('boxoff'); % 关闭图例外框
grid on;
set(gca,'GridLineStyle',':','GridColor','r','GridAlpha',1); % ':':网格线虚线;'-':网格线实线
grid minor;
box off;
save('a.mat','a','b')
saveas(h1,'三角函数.jpg'); % 保存图片
% clf(1); % 清空画布的内容