现在时间是:
当前位置:首 页 >> 矩阵计算>> 教学区>> 文章列表

130213015刘婷婷矩阵计算第7次作业

作者:刘婷婷   发布时间:2015-06-23 16:44:54   浏览次数:83

题目:见课本75 上机习题(2)

 

 

1.1程序:(写出矩阵Ab)

Equ.m

function [A,b]=Equ(n)

B=-1*ones(n,n);

C=zeros(n,n);

for i=1:n-1

    C(i,n)=1;

end

A=eye(n)+tril(B,-1)+C; %build matrix A

x=rand(n,1); %build vector randomly

b=A*x;  %build vector x

 

 

2.2程序:(列主元Gauss消去法)

liexuan.m

function x3=liexuan(A,b)

n=length(b);

max=A(1,1);

p=1;

for k=1:n-1

    for i=k:n

            if A(i,k)>max

                max=A(i,k);

                p=i;

            end

    end

        A([p,k],:)=A([k,p],:);  %change the kth row and the pth row

       

        z=b(p);

        b(p)=b(k);

        b(k)=z;   %change vector b's kth and pth

    end

 

tol=10^(-16);

for k=1:n-1

    if abs(A(k,k))>tol

        A(k+1:n,k)=A(k+1:n,k)/A(k,k);

        A(k+1:n,k+1:n)=A(k+1:n,k+1:n)-A(k+1:n,k)*A(k,k+1:n);

    end

end

L=eye(n)+tril(A,-1);

U=triu(A,0);

 

b(1)=b(1)/L(1,1);

for j=2:n

    b(j)=b(j)-(L(j,1:j-1)*b(1:j-1))/L(j,j);

end

 

x3=zeros(n,1);

x3(n)=b(n)/U(n,n);

for i=n-1:-1:1

    x3(i)=(b(i)-(U(i,i+1:n)*x3(i+1:n)))/U(i,i);

end

 

 

3.1 程序:(解方程,计算解得精度,并与真实相对误差作比较)

err.m

clc;

%solve the equation b=A*x

%calculate error

for n=5:1:30

[A,b]=Equ(n);

x1=liexuan(A,b); %the solution of liexuan method

r=b-A*x1;

err2=cond(A,inf)*norm(r,inf)/norm(b,inf)%calculate the accuracy of the solution

err=(norm(b-A*x1,inf)/norm(b,inf));% the real relative error

wucha=norm(err2-err)%compare the difference between err and err2

end

 

 

3.2运行结果:

err2 =

 

  4.3601e-015

 

 

wucha =

 

  3.4881e-015

 

 

err2 =

 

  1.2377e-015

 

 

wucha =

 

  1.0314e-015

 

 

err2 =

 

  8.4833e-015

 

 

wucha =

 

  7.2714e-015

 

 

err2 =

 

  3.9187e-015

 

 

wucha =

 

  3.4288e-015

 

 

err2 =

 

  1.8716e-015

 

 

wucha =

 

  1.6637e-015

 

 

err2 =

 

  4.9773e-014

 

 

wucha =

 

  4.4796e-014

 

 

err2 =

 

  1.1467e-013

 

 

wucha =

 

  1.0425e-013

 

 

err2 =

 

  1.8887e-013

 

 

wucha =

 

  1.7313e-013

 

 

err2 =

 

  1.4989e-013

 

 

wucha =

 

  1.3836e-013

 

 

err2 =

 

  1.6431e-013

 

 

wucha =

 

  1.5257e-013

 

 

err2 =

 

  6.8655e-013

 

 

wucha =

 

  6.4078e-013

 

 

err2 =

 

  1.1628e-012

 

 

wucha =

 

  1.0902e-012

 

 

err2 =

 

  3.2602e-013

 

 

wucha =

 

  3.0684e-013

 

 

err2 =

 

  1.1630e-012

 

 

wucha =

 

  1.0984e-012

 

 

err2 =

 

  4.8545e-011

 

 

wucha =

 

  4.5990e-011

 

 

err2 =

 

  8.6946e-012

 

 

wucha =

 

  8.2599e-012

 

 

err2 =

 

  5.7868e-011

 

 

wucha =

 

  5.5112e-011

 

 

err2 =

 

  5.6520e-011

 

 

wucha =

 

  5.3950e-011

 

 

err2 =

 

  2.1138e-011

 

 

wucha =

 

  2.0219e-011

 

 

err2 =

 

  8.2160e-011

 

 

wucha =

 

  7.8736e-011

 

 

err2 =

 

  1.7191e-010

 

 

wucha =

 

  1.6504e-010

 

 

err2 =

 

  1.0096e-009

 

 

wucha =

 

  9.7073e-010

 

 

err2 =

 

  2.2194e-009

 

 

wucha =

 

  2.1372e-009

 

 

err2 =

 

  3.6218e-009

 

 

wucha =

 

  3.4925e-009

 

 

err2 =

 

  3.3061e-008

 

 

wucha =

 

  3.1921e-008

 

 

err2 =

 

  1.1404e-009

 

 

wucha =

 

  1.1024e-009

 

 

 

3.3分析:

随着矩阵A的维数的增加,列主元Gauss消去法求解方程组的误差越来越大。

 

 

4.1程序:(迭代法改进计算精度)

improve.m

clc;

for n=5:1:30

[A,b]=Equ(n);

x1=liexuan(A,b); %the solution of liexuan method

x=inv(A)*b; %the real solution

while (norm((x-x1),inf)/norm(x,inf))>eps

r=b-A*x1;%step 1

z=liexuan(A,r);%step2:solve the equation A*z=r

z=single(z);%save as single type

x=x1+z;%step3

x1=x;%stsp4

end

     'the solution has reach the eps!'

     x1

end

 

 

 

4.2运行结果:

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.3508

    0.6855

    0.2941

    0.5306

    0.8324

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.5975

    0.3353

    0.2992

    0.4526

    0.4226

    0.3596

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.5583

    0.7425

    0.4243

    0.4294

    0.1249

    0.0244

    0.2902

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.3175

    0.6537

    0.9569

    0.9357

    0.4579

    0.2405

    0.7639

    0.7593

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.7406

    0.7437

    0.1059

    0.6816

    0.4633

    0.2122

    0.0985

    0.8236

    0.1750

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.1636

    0.6660

    0.8944

    0.5166

    0.7027

    0.1536

    0.9535

    0.5409

    0.6797

    0.0366

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.8092

    0.7486

    0.1202

    0.5250

    0.3258

    0.5464

    0.3989

    0.4151

    0.1807

    0.2554

    0.0205

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.9237

    0.6537

    0.9326

    0.1635

    0.9211

    0.7947

    0.5774

    0.4400

    0.2576

    0.7519

    0.2287

    0.0642

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.7673

    0.6712

    0.7152

    0.6421

    0.4190

    0.3908

    0.8161

    0.3174

    0.8145

    0.7891

    0.8523

    0.5056

    0.6357

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.9509

    0.4440

    0.0600

    0.8667

    0.6312

    0.3551

    0.9970

    0.2242

    0.6525

    0.6050

    0.3872

    0.1422

    0.0251

    0.4211

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.1841

    0.7258

    0.3704

    0.8416

    0.7342

    0.5710

    0.1769

    0.9574

    0.2653

    0.9246

    0.2238

    0.3736

    0.0875

    0.6401

    0.1806

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.0451

    0.7232

    0.3474

    0.6606

    0.3839

    0.6273

    0.0216

    0.9106

    0.8006

    0.7458

    0.8131

    0.3833

    0.6173

    0.5755

    0.5301

    0.2751

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.2486

    0.4516

    0.2277

    0.8044

    0.9861

    0.0300

    0.5357

    0.0871

    0.8021

    0.9891

    0.0669

    0.9394

    0.0182

    0.6838

    0.7837

    0.5341

    0.8854

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.8990

    0.6259

    0.1379

    0.2178

    0.1821

    0.0418

    0.1069

    0.6164

    0.9397

    0.3545

    0.4106

    0.9843

    0.9456

    0.6766

    0.9883

    0.7668

    0.3367

    0.6624

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.2442

    0.2955

    0.6802

    0.5278

    0.4116

    0.6026

    0.7505

    0.5835

    0.5518

    0.5836

    0.5118

    0.0826

    0.7196

    0.9962

    0.3545

    0.9713

    0.3464

    0.8865

    0.4547

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.4134

    0.2177

    0.1257

    0.3089

    0.7261

    0.7829

    0.6938

    0.0098

    0.8432

    0.9223

    0.7710

    0.0427

    0.3782

    0.7043

    0.7295

    0.2243

    0.2691

    0.6730

    0.4775

    0.6237

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.2364

    0.1771

    0.8296

    0.7669

    0.9345

    0.1079

    0.1822

    0.0991

    0.4898

    0.1932

    0.8959

    0.0991

    0.0442

    0.5573

    0.7725

    0.3119

    0.1790

    0.3390

    0.2101

    0.5102

    0.9064

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.6289

    0.1015

    0.3909

    0.0546

    0.5013

    0.4317

    0.9976

    0.8116

    0.4857

    0.8944

    0.1375

    0.3900

    0.9274

    0.9175

    0.7136

    0.6183

    0.3433

    0.9360

    0.1248

    0.7306

    0.6465

    0.8332

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.3983

    0.7498

    0.8352

    0.3225

    0.5523

    0.9791

    0.5493

    0.3304

    0.6195

    0.3606

    0.7565

    0.4139

    0.4923

    0.6947

    0.9727

    0.3278

    0.8378

    0.7391

    0.9542

    0.0319

    0.3569

    0.6627

    0.2815

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.2304

    0.7111

    0.6246

    0.5906

    0.6604

    0.0476

    0.3488

    0.4513

    0.2409

    0.7150

    0.8562

    0.2815

    0.7311

    0.1378

    0.8367

    0.1386

    0.5882

    0.3662

    0.8068

    0.5038

    0.4896

    0.8770

    0.3531

    0.4494

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.9635

    0.0423

    0.9730

    0.1892

    0.6671

    0.5864

    0.6751

    0.3610

    0.6203

    0.8112

    0.0193

    0.0839

    0.9748

    0.6513

    0.2312

    0.4035

    0.1220

    0.2684

    0.2578

    0.3317

    0.1522

    0.3480

    0.1217

    0.8842

    0.0943

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.9300

    0.3990

    0.0474

    0.3424

    0.7360

    0.7947

    0.5449

    0.6862

    0.8936

    0.0548

    0.3037

    0.0462

    0.1955

    0.7202

    0.7218

    0.8778

    0.5824

    0.0707

    0.9227

    0.8004

    0.2859

    0.5437

    0.9848

    0.7157

    0.8390

    0.4333

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.4706

    0.5607

    0.2691

    0.7490

    0.5039

    0.6468

    0.3077

    0.1387

    0.4756

    0.3625

    0.7881

    0.7803

    0.6685

    0.1335

    0.0216

    0.5598

    0.3008

    0.9394

    0.9809

    0.2866

    0.8008

    0.8961

    0.5975

    0.8840

    0.9437

    0.5492

    0.7284

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.5768

    0.0259

    0.4465

    0.6463

    0.5212

    0.3723

    0.9371

    0.8295

    0.8491

    0.3725

    0.5932

    0.8726

    0.9335

    0.6685

    0.2068

    0.6539

    0.0721

    0.4067

    0.6669

    0.9337

    0.8110

    0.4845

    0.7567

    0.4170

    0.9718

    0.9880

    0.8641

    0.3889

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.4547

    0.2467

    0.7844

    0.8828

    0.9137

    0.5583

    0.5989

    0.1489

    0.8997

    0.4504

    0.2057

    0.8997

    0.7626

    0.8825

    0.2850

    0.6732

    0.6643

    0.1228

    0.4073

    0.2753

    0.7167

    0.2834

    0.8962

    0.8266

    0.3900

    0.4979

    0.6948

    0.8344

    0.6096

 

 

ans =

 

the solution has reach the eps!

 

 

x1 =

 

    0.5747

    0.3260

    0.4564

    0.7138

    0.8844

    0.7209

    0.0186

    0.6748

    0.4385

    0.4378

    0.1170

    0.8147

    0.3249

    0.2462

    0.3427

    0.3757

    0.5466

    0.5619

    0.3958

    0.3981

    0.5154

    0.6575

    0.9509

    0.7223

    0.4001

    0.8319

    0.1343

    0.0605

    0.0842

    0.1639


 







上一篇:没有了    下一篇:没有了

Copyright ©2018    计算数学达人 All Right Reserved.

技术支持:自助建站 | 领地网站建设 |短信接口 版权所有 © 2005-2018 lingw.net.粤ICP备16125321号 -5