本文最后更新于 2025年8月14日 星期四 14:40
0
基础
0.1 精度问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 uint64(36028797018963968 ) x = [4 /3 1.2345e-6 ] format short format short e format short g format long format long e format long g format bank format rat format hex
0.2 内置常量
eps
浮点相对精度 \(\varepsilon=2^{-52}\)
realmin
最小浮点数 \(2^{-1022}\)
realmax
最大浮点数 \((2-\varepsilon)2^{1023}\)
Inf
infinity
NaN
非数字
0.3 工作区变量的读写与查看
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 whos save myfile.mat a b c save myfile.mat writematrix(a, 'myfile.txt' ) warning('off' ) writematrix(a,'data.xlsx' ,'Sheet' ,2 ,'Range' ,'B2' ) a = readmatrix('dataA_32.xlsx' ,'Sheet' ,1 ,'Range' ,'C3:F6' ) a = load('myfile.mat' ); clc clear close all
常量可以被重新赋值,可以使用clear <常量名>恢复原始值。
0.4 查看文档
1 2 3 4 5 6 doc mean help mean lookfor mean nargin mean nargout mean
... 用于续行。
1 数组/矩阵/向量
1.1 基础
1.1.1 创建矩阵
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 a = [1 2 3 4 ] a = [1 3 5 ; 2 4 6 ; 7 8 10 ] eye (n) zeros (5 ,1 ) ones (5 ,2 ) randi([0 ,10 ],3 ) rand (5 ,3 ) randn (5 ,3 ) normrnd(mu,sigma,m,n) exprnd(mu,m,n) poissrnd(mu,m,n) unifrnd(a,b,m,n) fix (10 *rand (1 ,10 )) linspace (n1,n2,n) logspace (n1,n2,n) randperm(n) perms ([1 :n])
1.1.2 基础运算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 a + 10 sin (a) a' inv(a) a.*a a.^3 A = [a,a] A = [a;a] A(4 ,5 ) = 17 B = 0 :10 :100 magic (3 ) sum(A) sum(A,2 ) diag (A) fliplr (A) diag (fliplr (A)) flipud (A) A(:,[1 3 2 4 ]) rref(a)
方程组的 \(\begin{cases}x_1+2x_2+2x_3+x_4=0,
\\2x_1+x_2-2x_3-2x_4=0, \\x_1-x_2-4x_3-3x_4=0,\end{cases}\)
的基础解系为 \([2,-2,1,0]',[5/3,-4/3,0,1]'\) ,即
\(b=\left(\begin{array}{cc} 2 &
\frac{5}{3}\\ -2 & -\frac{4}{3}\\ 1 & 0\\ 0 & 1
\end{array}\right)\) 。
1 2 a = sym([1 ,2 ,2 ,1 ;2 ,1 ,-2 ,-2 ;1 ,-1 ,-4 ,-3 ]); b = null(a)
求超定方程组 \(\begin{cases}2x_1+4x_2=11,\\3x_1-5x_2=3,\\x_1+2x_2=6,\\2x_1+x_2=7,\end{cases}\)
的数值解。
解超定方程组的“\”可以用伪逆命令 pinv
代替,且 pinv
的使用范围比“\”更加广泛,pinv
也给出最小二乘解或最小范数解。只有 a 列满秩时,才能使用命令
a\b,否则只能使用命令 pinv(a)*b。
1 2 3 4 a = [2 ,4 ;3 ,-5 ;1 ,2 ;2 ,1 ]; b = [11 ;3 ;6 ;7 ]; s = a \ b pinv(a)*b
求一个正交变换 \(x=Py\) ,把二次型
\(f=2x_1 x_2+2x_1 x_3-2x_1 x_4-2x_2 x_3+2x_2
x_4+2x_3 x_4\) 化为标准形。
1 2 3 A = sym([0 ,1 ,1 ,-1 ;1 ,0 ,-1 ,1 ;1 ,-1 ,0 ,1 ;-1 ,1 ,1 ,0 ]); [P1,D] = eig(A) P2 = orth(P1)
1.2 元素引用
1 2 3 4 5 6 7 8 9 10 11 A(4 ,2 ) A(8 ) A(1 :3 ,2 ) A(3 ,:) sum(A(:,end )) k = find (isprime (A))' A(k) A(k) = NaN
1.3 构建表
1 2 3 4 5 6 7 n = (0 :9 )' pows = [n n.^2 2. ^n] format short g x = (1 :0.1 :2 )'; logs = [x log10 (x)]
1.4 删除元素
1 2 3 4 5 6 7 8 A(:,2 ) = [] A(2 :2 :10 ) = [] x = x(isfinite (x)) x = x(abs (x-mean (x)) <= 3 *std(x)) A(~isprime (A)) = 0
1.5 多维数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 p = perms (1 :4 ); A = magic (4 ); M = zeros (4 ,4 ,24 );for k = 1 :24 M(:,:,k) = A(:,p(k,:));end size (M) sum(M,1 ) sum(M,2 )
1.6 元胞数组
1 2 3 4 5 6 7 8 9 10 C = {A sum(A) prod(prod(A))} M = cell(8 ,1 );for n = 1 :8 M{n} = magic (n);end M{3 }
2 字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 strlength(A) seq = 'GCTAGAATCC' seq(4 ) seq2 = [seq 'ATTAGAAACC' ] disp ("hello world" ) double(s) char(s) F = reshape (32 :127 ,16 ,6 )' char(F) char(F+128 ) S = char('A' ,'rolling' ,'stone' ,'gathers' ,'momentum.' ) C = {'A' ;'rolling' ;'stone' ;'gathers' ;'momentum.' } C = cellstr(S) S = char(C)
3 结构体
3.1 结构体基础
1 2 3 4 S(1 ) = struct('name' ,'Jerry Garcia' ,'score' ,70 ,'grade' ,'C' ) S(2 ) = struct('name' ,'John Doe' ,'score' ,100 ,'grade' ,'A' ) [N1 N2] = S.name
3.2 动态字段名称示例
算平均分。
初始化:
1 testscores.Ann_Lane.week(1 :5 ) = [95 89 76 82 79 ];
使用 edit avgscore 新建脚本
avgscore.m 。
1 2 3 4 5 function avg = avgscore (testscores, student, first, last) for k = first:last scores(k) = testscores.(student).week(k);end avg = sum(scores)/(last - first + 1 );
然后运行:
1 avgscore(testscores, 'Ann_Lane' , 2 , 5 )
3.2 集合
1 2 3 4 5 6 A = [1 3 5 ] B = [3 6 9 ]max (A) union(A,B) [minA,maxA] = bounds(A)
4 绘图
gtext('str hear') 在光标位置添加文字。
4.1 二维绘图 plot 函数
绘制 \([0,2\pi]\)
上的线性间距向量上的正弦函数:
1 2 3 4 5 6 7 8 9 x = linspace (0 ,2 *pi ); y = sin (x); xlabel("x" ) ylabel("sin(x)" )plot (x,y,"r--" ) title("Plot of the Sine Function" )legend ("sin" )hold on
4.2 三维绘图 surf/mesh 函数
对于给定的行向量和列向量 \(x\) 和
\(y\) ,每个向量包含 \([-2,2]\) 范围内的 20 个点,计算 \(z=x\mathrm{e}^{-x^2-y^2}\) :
1 2 3 4 5 x = linspace (-2 ,2 ,20 ); y = x'; z = x .* exp (-x.^2 - y.^2 ); surf(x,y,z) mesh(x,y,z)
创建一个球。
1 2 3 4 [x,y,z] = sphere; r = 2 ; surf(x*r,y*r,z*r) axis equal
4.3 多个绘图
4.3.1 subplot 函数
绘制二元函数 \(z=\sin(xy)/xy\)
的三维网格图。
1 2 3 4 5 6 7 8 9 10 11 x = -5 :0.2 :5 ; [x,y] = meshgrid (x); z = (sin (x.*y)+eps )./(x.*y+eps ); subplot(221 ); mesh(x,y,z); subplot(222 ); fmesh(@(x,y) sin (x.*y)./(x.*y)); subplot(223 ); surf(x,y,z); subplot(224 ); fsurf(@(x,y) sin (x.*y)./(x.*y));
4.3.2 多个绘图 tiledlayout 函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 t = tiledlayout(2 ,2 ); title(t,"Trigonometric Functions" ) x = linspace (0 ,30 ); nexttileplot (x,sin (x)) title("Sine" ) nexttileplot (x,cos (x)) title("Cosine" ) nexttileplot (x,tan (x)) title("Tangent" ) nexttileplot (x,sec (x)) title("Secant" )
4.4 隐函数
1 fimplicit(@(x,y) x.^2 +y.^2 /4 -1 ,[-2 ,2 ,-2 ,2 ])
在区间 \([0,10\pi]\) 画出参数曲线
\(x=\sin(t)\) ,\(y=\cos(t)\) ,\(z=t\) 。
1 2 3 4 5 t = 0 :pi /50 :10 *pi ; subplot(121 );plot3 (sin (t),cos (t),t); subplot(122 ); fplot3(@(t) sin (t),@(t) cos (t),@(t) t,[0 ,10 *pi ]);
画 \(x^2+(y-5)^2=16\) 绕 \(x\) 轴旋转一周所形成的旋转曲面为 \(x^2+(\sqrt{y^2+z^2}-5)^2=16\) ,先把旋转曲面化成参数方程
\(x=4\cosu\) ,\(y=(5+4\sin u)\cos v\) ,\(z=(5+4\sin u)\sin v\) ,其中 \(\alpha,\beta\in[0,2\pi]\) 。
1 2 3 4 5 6 7 8 f = @(x,y,z) x.^2 + (sqrt (y.^2 + z.^2 )-5 ).^2 -16 ; subplot(121 ); fimplicit3(f,[-4 ,4 ,-9 ,9 ,-9 ,9 ]); x=@(u,v) 4 *cos (u); y=@(u,v) (5 +4 *sin (u)).*cos (v); z=@(u,v) (5 +4 *sin (u)).*sin (v); subplot(122 ); fsurf(x,y,z);
4.5 更多与四维数据可视化
椭球面 \(x^2/9+y^2/4+z^2/6=1\) 。
1 ellipsoid(0 ,0 ,0 ,3 ,2 ,sqrt (6 ));
1 2 3 4 5 6 7 8 9 [x,y,z,v] = flow; isosurface(x,y,z,v); x = 1 :20 ; y = 1 :10 ; z = -10 :10 ; [x,y,z] = meshgrid (x,y,z); v = x.^2. *y.*(z+1 ); isosurface(x,y,z,v);
5 数值计算
5.1 积分
\(D=\{(x,y)|x^2+y^2\leq
x\}\) ,\(\int_D\sqrt{1-x^2-y^2}\mathrm{d}\sigma=\int_0^1\mathrm{d}x\int_{-\sqrt{x-x^2}}^{\sqrt{x-x^2}}\sqrt{1-x^2-y^2}\mathrm{d}y=0.6028\)
\(\Omega=\{(x,y,z)|0\leq
z\leq\sqrt{1-x^2-y^2}\}\) ,
\[
\begin{align}
&\int_\Omega\frac{z^2\ln( x^2+y^2+z^2+1)}{x^2+y^2+z^2+1}\mathrm{d}v
\\
=&\int_{-1}^1\mathrm{d}x\int_{-\sqrt{1-x^2}}^{\sqrt{1-x^2}}\mathrm{d}y\int_0^{\sqrt{1-x^2-y^2}}\frac{z^2\ln(x^2+y^2+z^2+1)}{x^2+y^2+z^2+1}\mathrm{d}z
\\
=&0.1273
\end{align}
\]
1 2 3 4 5 6 7 8 9 10 f=@(x,y) sqrt (1 -x.^2 -y.^2 ); ymax=@(x) sqrt (x-x.^2 ); ymin=@(x) -ymax(x); integral2(f,0 ,1 ,ymin,ymax) f=@(x,y,z) z.^2. *log (x.^2 +y.^2 +z.^2 +1 )./(x.^2 +y.^2 +z.^2 +1 ); ymax=@(x) sqrt (1 -x.^2 ); ymin=@(x) -ymax(x); zmax=@(x,y) sqrt (1 -x.^2 -y.^2 ); integral3(f,-1 ,1 ,ymin,ymax,0 ,zmax)
5.2 级数求和
1 2 syms n symsum(1 /n^2 ,n,1 ,inf )