(转)matlab各类数学公式
matlab矩阵应用
clear
%建立矩阵的两种方式
A1 = [1 2 3 4 5; 6 7 8 9 10];
A2 = [
1 2 3 4 5
6 7 8 9 10
];
%一种是换行用引号,一种是自然写法
clear
A = [5 4 3 2 1; 6 7 8 9 10;1 2 3 4 5;24 24 24 24 24;25 25 25 25 25];
B = [5 4 3 2 1; 6 7 8 9 10;1 2 3 4 5;24 24 24 24 24;25 25 25 25 25];
A_sqare = [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];
k = 5;
X1 = A’; %A取转置
X2 = A + B; %求A和B矩阵的和
X3 = A – B; %求A和B矩阵的差
X4 = k * A; %数K乘以A矩阵
X5 = det(A_sqare); %求矩阵A的行列式(注A_sqare必须为方阵)
X6 = rank(A); %求矩阵A的秩
X7 = inv(A_sqare); %求矩阵A的逆(注A_sqare必须为方阵)
X8 = B / A; X8 = B * inv(A_sqare); %A右除B = B右乘A的逆
X9 = B / A; X9 = inv(A_sqare) * B; %A左除B = B左乘A的逆
A.*B % .* 是A的每个元素和B相乘,非矩阵相乘,同理 ./ .^
A(2,:) % 取A矩阵的第2行 然后 A(2,:) = [5 5 5 5 5]就对该行进行赋值
A(:,2) % 取A矩阵的第2列 然后 A(:,2) = [5 5 5 5 5]就对该列进行赋值
A(2:1:4,3:1:4) %取A矩阵中的一块,其语法为A(起始行:步长:终止行,起始列:步长:终止列)
zeros(5) %生成n阶零矩阵
eye(5) %生成n阶单位矩阵
eig(A) %矩阵A的特征值
[X,D] = eig(A) %矩阵A的 特征向量矩阵X 特征值组成的对角阵
A([1,2],:) %1,2行互换
A(:,[2,3]) %1,2列互换
A(2,:) = 5 * A(2,:) %第2行乘以5,列上的操作以此类推
K = [A B;B A] %由几个小矩阵合成一个大矩阵
orth(A) %非奇异矩阵正交化
a1 = A(2,:);
a2 = A(3,:);
a1*a2′ %两个向量内积
rref(A) %A的极大无关向量组
matlab画图应用
%while循环
clear
sum = 0;k = 1;
while k<101
sum = sum+k;
k = k + 1;
end
sum
%for循环
clear
sum = 0;n = 1;
for n = 1:100
sum = n + sum;
end
sum
%plot绘图
x = -10:0.1:10;
y = 3*x.^4+x.^2-1;
figure %开启新绘图页面
plot(x,y)
%fplot绘图
clear
figure
fplot(@f1, [-10 10]) %调用f1.m里面的那个function
%ezplot绘图
clear
syms x
figure
y = 3*x^4 + x^2 – 1;
ezplot(y)
%螺旋线绘图 题目:x = cost , y = sint , z = t t属于[0,6pi]
clear
t = 0:0.1:6*pi;
x = cos(t);
y = sin(t);
z = t;
figure
plot3(x,y,z)
%空间曲线绘图 题目: z = sqrt(1 – x^2 – y^2) , (x – 1/2)^2 + y^2 = (1/2)^2
clear
t = 0:0.1:6;
x = 0.5*cos(t)+0.5;
y = 0.5*sin(t);
z = sqrt(1-x.^2-y.^2);
figure
plot3(x,y,z)
%二次曲面绘图 题目: x^2 + y^2 = z
clear
s = -10:1:10;
t = -10:1:10;
[x,y] = meshgrid(s,t);
z = x.^2 + y.^2;
figure
mesh(x,y,z);
%旋转曲面绘图 题目: y = 1/x 围绕y轴旋转
clear
s = -10:0.1:10;
t = -10:0.1:10;
[x,y] = meshgrid(s,t);
r = 1./x;
[x,y,z] = cylinder(r);
figure
mesh(x,y,z)
%输入数
clear
K = input(‘请输入数’);
matlab解方程应用
%常微分方程求解 详细参考PPT(7)
clear
y1 = dsolve(‘Dy=8-3*y’,'y(0)=2′)
y2 = dsolve(‘D2y=2*x*Dy/(1+x^2)’,'y(0)=1,Dy(0)=3′)
%微分方程组求解
clear
[X,Y] = dsolve(’2*Dx+4*x+Dy-y=exp(t),Dx+3*x+y=0′,’x(0)=1.5,y(0)=0′)
%线性方程组全部解
clear
format rat
A=[1 1 3 -1;0 1 -1 1;1 1 2 2;1 -1 1 -1];
B=[-2;1;4;0];
X = A/B
%方程求解
clear
X = solve(‘x-exp(-x)=0′,’x')
%区间里方程求解
%x=0:0.1:10;
%X = solve(’5*x^2*sin(x)-exp(-x)’,'x’)
%求微分方程的特解并且做出函数曲线
y0 = [1,0];
[t,x] = ode45(@vdp,[0,30],y0); %从vdp.m这个文件里面读函数
y = x(:,1);
dy=x(:,2);
figure
plot(t,y,t,dy);
%解微分方程
fun=inline(‘-2*y+2*x*x+2*x’);
[x,y]=ode23(fun,[0,0.5],1)
matlab微积分应用
%函数求导
clear
syms x y; %将x y设为变量
f = cos(x)^3-cos(3*x); %需要求导的式子
dy = diff(f,x); %用diff 函数进行求导
%函数求极限
clear
syms x
f = x*log(1+x)/sin(x*x)
limit(f,’x',0,’left’) %语法为 limit(求极限的式子,求极限的变量,需要逼近的数字,从左还是从右逼近)
%函数求积分
clear
syms x
f = sym(‘x*exp(x)/(1+x)^2′);
int(f) %语法为 int(求极限的式子,下限,上限) 如果不加上限下限,就是函数式。
int(f,0,1)
pretty(f) %以自然函数形式呈现
%例-求z = x^2 + y^2 , z = 1 , z =2 围成的曲面
clear
syms x y z
z = x^2+y^2;
f = z;
I = int( int( f, y, sqrt(1-x^2), sqrt(2-x^2) ), x, 1, sqrt(2) )
%级数求和
clear
syms n
f = (n+1)/n*2^n;
j = symsum(f, n, 1, inf) %级数求和,下限为1,上限为无穷大
%泰勒展开
clear
syms x
f = cos(x)
taylor(f, 10, x, pi/3) %语法为taylor(待展开函数,取前几项, 变量名, 展开中心)
%求傅里叶系数
clear
syms x n
f = x^3+x^2;
n = 5;
a0 = int(f,x,-pi,pi)/pi
a1 = int(f*cos(1*x),x,-pi,pi)/pi
a2 = int(f*cos(2*x),x,-pi,pi)/pi
a3 = int(f*cos(3*x),x,-pi,pi)/pi
a4 = int(f*cos(4*x),x,-pi,pi)/pi
a5 = int(f*cos(5*x),x,-pi,pi)/pi
b1 = int(f*sin(1*x),x,-pi,pi)/pi
b2 = int(f*sin(2*x),x,-pi,pi)/pi
b3 = int(f*sin(3*x),x,-pi,pi)/pi
b4 = int(f*sin(4*x),x,-pi,pi)/pi
b5 = int(f*sin(5*x),x,-pi,pi)/pi