matlab.docx

上传人:b****8 文档编号:29398378 上传时间:2023-07-23 格式:DOCX 页数:22 大小:89.16KB
下载 相关 举报
matlab.docx_第1页
第1页 / 共22页
matlab.docx_第2页
第2页 / 共22页
matlab.docx_第3页
第3页 / 共22页
matlab.docx_第4页
第4页 / 共22页
matlab.docx_第5页
第5页 / 共22页
点击查看更多>>
下载资源
资源描述

matlab.docx

《matlab.docx》由会员分享,可在线阅读,更多相关《matlab.docx(22页珍藏版)》请在冰豆网上搜索。

matlab.docx

matlab

第二章

1.用简单消去法解线性方程组。

matlab程序:

function[x]=ja211(A,b)

[m,m]=size(A);

l(m,m)=0;

x(m,1)=0;

forj=[1:

1:

m-1],

fori=[j+1:

1:

m],

l(i,j)=A(i,j)/A(j,j);

forh=[j:

1:

m],

A(i,h)=(A(i,h)-l(i,j)*A(j,h));

end

b(i,1)=b(i,1)-l(i,j)*b(j,1);

end

end

ifA(m,m)~=0,

x(m,1)=b(m,1)/A(m,m);

fort=[m-1:

-1:

1],

s=0;

fore=[t+1:

1:

m],

s=s+A(t,e)*x(e,1),

end

x(t,1)=(b(t,1)-s)/A(t,t),

end

end

题解

C=[2.373.06-4.28;1.46-0.783.75;-3.695.131.06;];

>>d=[1.76;4.69;5.74;];

>>ja211(C,d)

ans=

0.933798514091890

1.541054685145016

1.207647153023721

2.用主元消去法解线性方程组。

Matlab程序:

function[x]=ja221(A,b)

[m,m]=size(A);

D(1,m)=0;

l(m,m)=0;

x(m,1)=0;

forj=[1:

1:

m-1],

c=find(A(j:

1:

m,j)==max(A(j:

1:

m,j)));

D=A(j,:

);d=b(j,1);

A(j,:

)=A(c+j-1,:

);b(j,1)=b(c+j-1,1);

A(c+j-1,:

)=D;b(c+j-1,1)=d;

fori=[j+1:

1:

m],

l(i,j)=A(i,j)/A(j,j);

forh=[j:

1:

m],

A(i,h)=(A(i,h)-l(i,j)*A(j,h));

end

b(i,1)=b(i,1)-l(i,j)*b(j,1);

end

end

ifA(m,m)~=0,

x(m,1)=b(m,1)/A(m,m);

fort=[m-1:

-1:

1],

s=0;

fore=[t+1:

1:

m],

s=s+A(t,e)*x(e,1);

end

x(t,1)=(b(t,1)-s)/A(t,t);

end

end

题解

A=[10.83240.76750.9831;0.83240.69300.64000.8190;...

0.76750.64000.59110.7580;0.98310.81900.75800.0055;];

>>b=[8.8997;7.4144;6.8528;4.9171;];

>>ja221(A,b)

ans=

0.999999999999273

2.000000000000864

3.000000000000011

4.00000000000000

5.用LU分解法求线性方程组。

Matlab程序:

function[LUx]=ja51(A,b)

[n,n]=size(A);

L=eye(size(A));

U=zeros(size(A));

U(1,:

)=A(1,:

);

fori=[2:

1:

n],

forj=[1:

1:

n],

ifj

s1=0;

fore1=[1:

1:

j-1],

s1=s1+L(i,e1)*U(e1,j);

end

L(i,j)=(A(i,j)-s1)/U(j,j);

elseifj>=i,

s2=0;

fore2=[1:

1:

i-1],

s2=s2+L(i,e2)*U(e2,j);

end

U(i,j)=A(i,j)-s2;

end

end

end

y(n,1)=0;

x(n,1)=0;

forh=[1:

1:

n],

ifh==1,

y(h,1)=b(1,1);

else

s3=0;

fore3=[1:

1:

h-1],

s3=s3+L(h,e3)*y(e3,1);

end

y(h,1)=b(h,1)-s3;

end

end

fort=[n:

-1:

1],

ift==n,

x(t,1)=y(n,1)/U(n,n);

else

s4=0;

fore4=[t+1:

1:

n],

s4=s4+U(t,e4)*x(e4,1);

end

x(t,1)=(y(t,1)-s4)/U(t,t);

end

end

题解:

(1)

>>A=[201;-34-2;17-5;];

>>b=[4;-3;6;];

>>[LUx]=ja51(A,b)

L=

1.00000000000000000

-1.5000000000000001.0000000000000000

0.5000000000000001.7500000000000001.000000000000000

U=

2.00000000000000001.000000000000000

04.000000000000000-0.500000000000000

00-4.625000000000000

x=

1.864864864864865

0.783783783783784

0.270270270270270

(2)

AA=[10.17-0.250.54;0.4710.67-0.32;...

-0.110.351-0.74;0.550.430.361;];

>>bb=[0.3;0.5;0.7;0.9;];

>>[LUx]=ja51(AA,bb)

L=

Columns1through3

1.00000000000000000

0.4700000000000001.0000000000000000

-0.1100000000000000.4007173133355071.000000000000000

0.5500000000000000.3657211172698620.318896973426907

Column4

0

0

0

1.000000000000000

U=

Columns1through3

1.0000000000000000.170000000000000-0.250000000000000

00.9201000000000000.787500000000000

000.656935115748288

000

Column4

0.540000000000000

-0.573800000000000

-0.450668405608086

.0565********

x=

0.440888550891832

-0.363030990136447

1.166********5979

0.393567223148812

6.设对称正定矩阵

能作分解

,其中

为下三角矩阵,

的转置矩阵,试推导

的元素

可由下列公式确定:

8.作矩阵的

分解(用五位小数计算)。

Matlab程序:

function[LD]=ja28(A,b)

[n,n]=size(A);

L=eye(size(A));

D=zeros(size(A));

fori=[2:

1:

n],

A(i,1)=A(i,1)/A(1,1);

forj=[2:

1:

n],

ifi>j;

s1=0;

fore1=[1:

1:

j-1],

s1=s1+A(e1,e1)*A(i,e1)*A(j,e1);

end

A(i,j)=(A(i,j)-s1)/A(j,j);

elseifi==j,

s2=0;

fore2=[1:

1:

i-1],

s2=s2+A(e2,e2)*A(i,e2)*A(i,e2);

end

A(i,j)=A(i,j)-s2;

end

end

end

forh=[1:

1:

n],

D(h,h)=A(h,h);

end

fork=[2:

1:

n],

fort=[1:

1:

k-1],

L(k,t)=A(k,t);

end

end

题解:

>>A=[10.83240.76750.9831;0.83240.69300.64000.8190;...

0.76750.64000.59110.7580;0.98310.81900.75800.0055;];

>>[LD]=ja28(A,b)

L=

Columns1through3

1.00000000000000000

0.8324000000000001.0000000000000000

0.76750000000000010.2775761973946751.000000000000000

0.9831000000000006.0555152394805460.353113136417216

Column4

0

0

0

1.000000000000000

D=

Columns1through3

1.00000000000000000

00.0001102400000000

00-0.009600743831649

000

Column4

0

0

0

-0.963830923689467

9.分别用

分解法和

分解法求解线性方程组。

Matlab

求解程序:

function[LDx]=ja2911(A,b)

[n,n]=size(A);

L=eye(size(A));

D=zeros(size(A));

fori=[2:

1:

n],

A(i,1)=A(i,1)/A(1,1);

forj=[2:

1:

n],

ifi>j;

s1=0;

fore1=[1:

1:

j-1],

s1=s1+A(e1,e1)*A(i,e1)*A(j,e1);

end

A(i,j)=(A(i,j)-s1)/A(j,j);

elseifi==j,

s2=0;

fore2=[1:

1:

i-1],

s2=s2+A(e2,e2)*A(i,e2)*A(i,e2);

end

A(i,j)=A(i,j)-s2;

end

end

end

forh=[1:

1:

n],

D(h,h)=A(h,h);

end

fork=[2:

1:

n],

fort=[1:

1:

k-1],

L(k,t)=A(k,t);

end

end

y(n,1)=0;

x(n,1)=0;

y(1,1)=b(1,1)/D(1,1);

fora=[2:

1:

n],

s3=0;

fore3=[1:

1:

a-1],

s3=s3+L(a,e3)*D(e3,e3)*y(e3,1);

end

y(a,1)=(b(a,1)-s3)/D(a,a);

end

x(n,1)=y(n,1);

forbb=[n-1:

-1:

1],

s4=0;

fore4=[bb+1:

1:

n],

s4=s4+L(e4,bb)*x(e4,1);

end

x(bb,1)=y(bb,1)-s4;

end

题解:

(1)

A=[4-2-4;-21710;-4109;];

>>b=[10;3;-7;];

>>[LDx]=ja2911(A,b)

L=

1.00000000000000000

-0.5000000000000001.0000000000000000

-1.0000000000000000.5000000000000001.000000000000000

D=

400

0160

001

x=

2

1

-1

(2)

>>A=[10.83240.76750.9831;0.83240.69300.64000.8190;...

0.76750.64000.59110.7580;0.98310.81900.75800.0055;];

>>b=[8.8997;7.4144;6.8528;4.9171;];

>>[LDx]=ja2911(A,b)

L=

Columns1through3

1.00000000000000000

0.8324000000000001.0000000000000000

0.76750000000000010.2775761973946751.000000000000000

0.9831000000000006.0555152394805460.353113136417216

Column4

0

0

0

1.000000000000000

D=

Columns1through3

1.00000000000000000

00.0001102400000000

00-0.009600743831649

000

Column4

0

0

0

-0.963830923689467

x=

0.999999999999501

2.000000000000462

3.000000000000151

3.999999999999999

编程求解程序:

function[Lx]=ll(A,b)

[n,m]=size(A);

L=zeros(size(A));

L(1,1)=(A(1,1))^(1/2);

fori=[2:

1:

n],

forj=[1:

1:

i],

ifj

s1=0;

fore1=[1:

1:

j-1],

s1=s1+L(i,e1)*L(j,e1);

end

L(i,j)=(A(i,j)-s1)/L(j,j);

elsej>=i,

s2=0;

fore2=[1:

1:

i-1],

s2=s2+L(i,e2)*L(i,e2);

end

L(i,j)=(A(i,i)-s2)^(1/2);

end

end

end

y(n,1)=0;

x(n,1)=0;

fork=[1:

1:

n],

s3=0;

fore3=[1:

1:

k-1],

s3=s3+L(k,e3)*y(e3,1);

end

y(k,1)=(b(k,1)-s3)/L(k,k);

end

x(n,1)=y(n,1);

forh=[n-1:

-1:

1],

s4=0;

fore4=[h+1:

1:

n],

s4=s4+L(e4,h)*x(e4,1);

end

x(h,1)=(y(h,1)-s4)/L(h,h);

end

题解:

(1)

A=[4-2-4;-21710;-4109;];

b=[10;3;-7;];

[Lx]=ll(A,b)

L=

200

-140

-221

x=

2

1

-1

(2)

A=[10.83240.76750.9831;0.83240.69300.64000.8190;...

0.76750.64000.59110.7580;0.98310.81900.75800.0055;];

>>b=[8.8997;7.4144;6.8528;4.9171;];

>>[Lx]=ll(A,b)

L=

Column1

1.000000000000000

0.832400000000000

0.767500000000000

0.983100000000000

Column2

0

0.010499523798722

0.107909655877723

0.063580026370449

Column3

0

0

0.000000000000000+0.097983385487789i

-0.000000000000000+0.034599220566370i

Column4

0

0

0

0.000000000000000+0.981748910714683i

x=

-4.230492466831677+5.135030281812863i

11.705472294606809-9.528336853201315i

4.412452545669016-1.386673748146589i

-0.000000000000000+3.926995642858731i

10.用简单迭代法解下列线性方程组。

(按三位小数进行计算)

Matlab程序:

functionx1=jacobi(A,b,x0,N,E)

[m,n]=size(A);

x(m,2)=0;

x(:

2)=x0;

fori=[1:

1:

N],

ifmax(abs(x(:

1)-x(:

2)))

break;

else

x(:

1)=x(:

2);

forj=[1:

1:

n],

s1=0;

fore1=[1:

1:

n],

s1=s1+A(j,e1)*x(e1,1);

end

x(j,2)=(b(j,1)-s1+A(j,j)*x(j,1))/A(j,j);

end

end

end

x1=x(:

2);

题解:

(1)

AA=[276-1;6152;11-2;];

>>bb=[85;72;110;];

>>x0=[1;1;1;];

>>jacobi(AA,bb,x0,100,0.0001)

ans=

-1.351465849800266

11.966245114666920

-49.692619548641581

(2)

>>A=[81-2;3101;5-220;];

>>b=[9;19;72;];

>>x0=[1;1;1;];

>>jacobi(A,b,x0,100,0.0001)

ans=

1.808733564833984

1.032266759707031

3.251038359500000

13.用简单迭代法、塞德尔迭代法解线性方程组(个迭代五次)。

并与精确解

比较。

Matlab程序:

functionx1=GS(A,b,x0,N,E)

[m,n]=size(A);

x(m,2)=0;

x(:

2)=x0;

fori=[1:

1:

N],

ifmax(abs(x(:

1)-x(:

2)))

break;

else

x(:

1)=x(:

2);

forj=[1:

1:

n],

ifj==1,

s1=0;

fore1=[2:

1:

m],

s1=s1+A(1,e1)*x(e1,1);

end

x(j,2)=(b(1,1)-s1)/A(1,1);

elseifj==m,

s2=0;

fore2=[1:

1:

m-1],

s2=s2+A(m,e2)*x(e2,2);

end

x(j,2)=(b(m,1)-s2)/A(m,m);

else

s3=0;

fore3=[1:

1:

j-1],

s3=s3+A(j,e3)*x(e3,2);

end

fore4=[j+1:

1:

m],

s3=s3+A(j,e4)*x(e4,1);

end

x(j,2)=(b(j,1)-s3)/A(j,j);

end

end

end

end

x1=x(:

2);

题解:

A=[5-1-1-1;-110-1-1;-1-15-1;-1-1-110;];

b=[-4;12;8;34;];

>>x0=[1;1;1;1];

>>jacobi(A,b,x0,5,0.000001)

ans=

0.968000000000000

1.981270000000000

2.968640000000000

3.981290000000000

>>GS(A,b,x0,5,0.000001)

ans=

0.996552514803221

1.998599188612620

2.998420412137192

3.999357211555304

与真实值比较最大误差是0.004

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1