matlab学习笔记2
MATLAB 的数组的建立和保存
%创建等差数列
a=0:0.5:10
a = 1×21
0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000 ⋯
x=linspace(0,1,75)
x = 1×75
0 0.0135 0.0270 0.0405 0.0541 0.0676 0.0811
%从原来的数组创建新的数组
a=1:4;
b=1:2:7;
c=[b,a];
d=[a(1:2:4),4 0.2 8];
c,d
c = 1×8
1 3 5 7 1 2 3 4d = 1×5
1.0000 3.0000 4.0000 0.2000 8.0000
%利用函数 logspace 创建等比数列
logspace(0,2,11)
ans = 1×11
1.0000 1.5849 2.5119 3.9811 6.3096 10.0000 15.8489
MATLAB 的矩阵运算和数组运算
%矩阵相加运算
A=[1, 1, 1; 1, 2, 3; 1, 3, 6];
B=[8, 1, 6; 3, 5, 7; 4, 9, 2];
C=A+B;
D=A-B;
C,D
C = 3×3
9 2 7
4 7 10
5 12 8D = 3×3
-7 0 -5
-2 -3 -4
-3 -6 4
%矩阵乘法运算
X= [2 3 4 5;1 2 2 1];
Y=[0 1 1;1 1 0;0 0 1;1 0 0];
Z=X*Y;
A=2*X;
Z,A
Z = 2×3
8 5 6
3 3 3A = 2×4
4 6 8 10
2 4 4 2
%矩阵的点积运算
X=[-1 0 2];
Y=[-2 -1 1];
Z=dot(X, Y)
Z = 4
%另一种点积运算
sum(X.*Y)
ans = 4
%向量的叉乘a=[1 2 3];b=[4 5 6];c=cross(a,b)
c = 1×3
-3 6 -3
%混合积a=[1 2 3];b=[4 5 6];c=[-3 6 -3];x=dot(a, cross(b, c))
x = 54
%展开多项式(s^2 + 2s + 2)(s + 4)(s +1)w=conv([1,2,2],conv([1,4],[1,1]));P=poly2str(w,'s');w,P
A1 = 3×3 complex
1.4908 - 0.0000i 0.2551 + 0.0000i 0.6711 + 0.0000i
2.1879 - 0.0000i 0.5145 + 0.0000i -0.6590 + 0.0000i
-1.4515 - 0.0000i 1.3756 + 0.0000i 2.2378 + 0.0000iA2 = 3×3
1.5518 2.0477 2.1779
2.4082 1.3195 1.9037
1.0000 2.0477 1.5518A3 = 3×3
0.0640 0.0041 0.0016
0.0003 0.1600 0.0102
0.4000 0.0041 0.0640
矩阵运算的应用——线性方程组的求解
%例1.2.25A=[5 6 0 0 0 1 5 6 0 0 0 1 5 6 0 0 0 1 5 6 0 0 0 1 5];B=[1,0,0,0,1]';R_A=rank(A);X=A\B;R_A,X
R_A = 5X = 5×1
2.2662
-1.7218
1.0571
-0.5940
0.3188
%另一种解法A=[5 6 0 0 0 1 5 6 0 0 0 1 5 6 0 0 0 1 5 6 0 0 0 1 5];B=[1,0,0,0,1]';C=[A,B]; %构造增广矩阵R=rref(C); %将C化成最简行R
R = 5×6
1.0000 0 0 0 0 2.26620 1.0000 0 0 0 -1.7218 0 0 1.0000 0 0 1.0571 0 0 0 1.0000 0 -0.5940 0 0 0 0 1.0000 0.3188
%例1.2.26A=[1 1 -3 -1 3 -1 -3 4 1 5 -9 -8];B=[1 4 0]';X =A\B %近似值
X = 4×1
0 0
-0.5333
0.6000
%用rref求解A=[1 1 -3 -1 3 -1 -3 4 1 5 -9 -8];B=[1 4 0]';C=[A,B];R=rref(C)
R = 3×5
1.0000 0 -1.5000 0.7500 1.25000 1.0000 -1.5000 -1.7500 -0.2500 0 0 0 0 0