一:MATLAB中数组与矩阵基本操作。
1:生成一个54的魔方矩阵A
A=magic(5)
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
1):求该方阵的秩与行列式的值;
rank(A)
det(A)
ans =
5
ans =
5070000
2):取A方阵的上三角矩阵
triu(A)
ans =
17 24 1 8 15
0 5 7 14 16
0 0 13 20 22
0 0 0 21 3
0 0 0 0 9
3):将A方阵的第1、3行放入矩阵B中,第1、3列放入矩阵C中
B=A(1:2:3,:)
C=A(:,1:2:3)
B =
17 24 1 8 15
4 6 13 20 22
C =
17 1
23 7
4 13
10 19
11 25
4):求A方阵中大于7的元素的和(不用for循环)
s=d.*A;
S=sum(sum(s))
S =
297
2:生成一个 3*4 的复数矩阵 D,完成矩阵 D 的转置将结果存入 E 矩阵中
Dr=[1,2,3;4,5,6;7,8,9;10,11,12];
Di=[12,11,10;9,8,7;6,5,4;3,2,1];
D=Dr+Di*i
E=D'
D =
1 + 12i 2 + 11i 3 + 10i
4 + 9i 5 + 8i 6 + 7i
7 + 6i 8 + 5i 9 + 4i
10 + 3i 11 + 2i 12 + 1i
E =
1 - 12i 4 - 9i 7 - 6i 10 - 3i
2 - 11i 5 - 8i 8 - 5i 11 - 2i
3 - 10i 6 - 7i 9 - 4i 12 - 1i
二:求线性方程组的解或者通解
1):
A=[2,3,0
4,1,4
2,-1,2]
B=[10
18
3]
X=A^(-1)*B
A =
2 3 0
4 1 4
2 -1 2
B =
10
18
3
X =
-1
4
9/2
2):
A=[1 -1 -1 1;1 -1 1 -3;1 -1 -2 3];
B=[0; 1; -1/2];
x1=A\B
Y=null(A,'r')
x1 =
0
-1/4
0
-1/4
Y =
1 1
1 0
0 2
0 1
三:MATLAB 绘图命令的使用
1):
x=-2*pi:0.01*pi:2*pi;
y=sin(x).^2;
plot(x,y);
title('sin(x)^2');
xlabel('x');
ylabel('y');
x=-2*pi:0.01*pi:2*pi;
y1=sin(x).^2;
y2=sin(x).*cos(x);
plot(x,y1);
hold on;
plot(x,y2);
axis([-1,1,-3,3]);
x=-2*pi:0.01*pi:2*pi;
y1=sin(x).^2;
y2=sin(x).*cos(x);
plot(x,y1,'b',x,y2,'r-.');
legend('sin(x)^2','sin(x)*cos(x)');
2):
x=-2*pi:0.01*pi:2*pi;
y1=sin(x).^2;
y2=sin(x).*cos(x);
subplot(2,1,1);
plot(x,y1);
subplot(2,1,2);
plot(x,y2);
3):
x=0:0.005*pi:2*pi;
y1=sin(6*x);
polar(x,y1,'b.');
4):
u=0:0.005*pi:2*pi;
v=0:0.005*pi:2*pi;
[X,Y]=meshgrid(u,v);
x=(1+cos(X)).*cos(Y);
y=(1+cos(X)).*sin(Y);
z=sin(X);
mesh(x,y,z);
四:matlab 符号运算
1):
syms x
f=x^10-1;
factor(f)
ans =
(x - 1)*(x + 1)*(x^4 + x^3 + x^2 + x + 1)*(x^4 - x^3 + x^2 - x + 1)
2):
syms x
limit('((1+x)^(1/x)-exp(1))/x',x,0)
ans =
-exp(1)/2
3):
syms x
int(sin(x^2),0,inf)
ans =
(2^(1/2)*pi^(1/2))/4
4):
syms x y z
z=7*y^3-x^4*y^3;
Dfx2=diff(z,x,2)
Dfy2=diff(z,y,2)
Dfx=diff(z,x);
Dfy=diff(z,x);
Dfxy=diff(Dfx,y)
Dfx2 =
-12*x^2*y^3
Dfy2 =
- 6*y*x^4 + 42*y
Dfxy =
-12*x^3*y^2
5):
syms x y f
f=sin(x)+exp(x)-x^2*y;
Dfx=diff(f,x);
Dfy=diff(f,y);
Dy_Dx=-(Dfx/Dfy)
Dy_Dx =
(cos(x) + exp(x) - 2*x*y)/x^2
6):
syms x y f g
dsolve('Df==g+f+x','Dg==g-f','f(0)==1','g(0)==2')
ans =
g: [1x1 sym]
f: [1x1 sym]
五:数值积分与数值微分
1):
a=0;
b=1;
n=10;
h=(b-a)/n;
x=a:h:b;
y=4./(1+x.^2);
Y=myfun(x,y)
function [Y]=myfun(x,y)
n=length(x);
s=(x(2:n)-x(1:n-1)).*(y(1:n-1)+y(2:n))/2;
Y=s*ones(n-1,1);
end
Y =
1683/536
2):
xs=0;xe=1;x=[xs,xe];
y0_1=0;y0_2=0;y=[2];
%% 1.预估校正法:
[ X,Y ] = yu_gu_jiao_zheng( h,x,y );
plot(X,Y,'k+');hold on;
%% 3.系统
[T11,Y11]=ode15s('myfun',x,y);
plot(T11,Y11,'b');
legend('预估校正法解:','解析法解:');
function [ X,Y ] = yu_gu_jiao_zheng( h,x,y )
n=floor((x(2)-x(1))/h);
x=x(1);
Y(:,1)=y;
X(1)=x;
for m=1:n
k1=myfun(x,y)';
aa2=y+h*k1;
k2=myfun(x+h,aa2)';
y=y+h/2*(k1+k2);
Y(:,m+1)=y;
x=x+h;
X(m+1)=x;
end
end
function [ y_1 ] = myfun( x,y )
y_1=x*exp(-x^2)-6*x*y;
end