Beruflich Dokumente
Kultur Dokumente
Homework #3
Due 14 Sep (Friday)
Instruction:
1. Solve all problems by using MATLAB.
2. Copy and paste your command window and MATLAB program into a single Word file
3. Upload your file to IVLE.
1. Solve A x = b
8
11
3
1
11
2
7
4
2
3
12
20
6
b
4
14
%%SolvePartA
ifdet(A)~=0;
x=A\b;
end
ifdet(A)~=0;%Printsolution
x
else
fprintf('MatrixAissingular,xeitherhasmanyornosolution\n');
end
%%SolvePartB
ifdet(A)<tol%checkdeterminant
fprintf('MatrixAisillconditioned\n');
end
%%SolvePartC
condition_number=norm(A,1)*norm(A^1,1)%checkconditionnumber
6.03
4.16
4.81
1.99
1.23
9.34
1
b 1
1
%%SolvePartA
ifdet(A)~=0;
x=A\b;
end
ifdet(A)~=0;%Printsolution
x
else
fprintf('MatrixAissingular,xeitherhasmanyornosolution\n');
end
%%SolvePartB
ifdet(A)<tol%checkdeterminant
fprintf('MatrixAisillconditioned\n');
end
%%SolvePartC
condition_number=norm(A,1)*norm(A^1,1)%checkconditionnumber
3. (a) Use MATLAB to get an exact solution S for the following linear system. (b) Use
Jacobi, Gauss-Siedel, and successive over relaxation (SOR) iteration methods ( = 1.5) to
solve the linear system. For all iterative methods, use P0 = [1 1 1 1 1 1 1 1 1]T as your
initial guess and perform 10 iterations. Show your results of P10. (c) Compare the speed
of convergence ||Pi- S||2 of these methods. Which method gives you the best answer after
10 iterations?
Solution:
exact_x =
0.5000
0.0000
1.5000
0.0000
2.5000
0.0000
1.5000
0.0000
0.5000
jacobi_x_p10 =
0.3535
0.4395
1.1094
0.7080
2.0117
0.7080
1.1094
0.4395
0.3535
gs_x_p10 =
0.3525
0.2644
1.1573
0.3796
2.1233
0.3387
1.2269
0.1884
0.4058
sor_x_p10 =
0.4796
0.0286
1.4694
0.0276
2.4770
0.0175
1.4885
0.0073
0.4967
convergence_test_jacobi =
1.4055
convergence_test_gs =
0.8537
convergence_test_sor =
0.0629
SOR gives best answer after 10 iterations
m file:
%%Preset
clear;clc;%clearworkspace,commandwindow
A=[210000000;121000000;012100000;00121
0000;000121000;000012100;000001210;000
000121;000000012];%setA
b=[1;2;3;4;5;4;3;2;1];%setb
jacobi_coeff=[01/20000000;1/201/2000000;01/20
1/200000;001/201/20000;0001/201/2000;000
01/201/200;000001/201/20;0000001/201/2;00
000001/20];%setcoeffmatrixforjacobi
%%SolvepartA
ifdet(A)~=0
exact_x=A\b
else
fprintf('MatrixAissingular,thereiseithernosolutionormany
solutions\n');
end
%%SolvepartB
%Jacobi
p0=ones(9,1);%setinitialguess
fori=1:10;
jacobi_x(:,i)=(1/2).*b+jacobi_coeff*p0;
p0=jacobi_x(:,i);
end
%Print
jacobi_x_p10=jacobi_x(:,10)%Printresultafter10iterations
%GS
p0_1=ones(9,1);%setinitialguess
forj=1:10;
gs_x(1,j)=(1/2).*b(1)+(1/2)*p0_1(2);
p0_1(1)=gs_x(1,j);
gs_x(2,j)=(1/2).*b(2)+(0.5)*p0_1(1)+(0.5)*p0_1(3);
p0_1(2)=gs_x(2,j);
gs_x(3,j)=(1/2).*b(3)+(0.5)*p0_1(2)+(0.5)*p0_1(4);
p0_1(3)=gs_x(3,j);
gs_x(4,j)=(1/2).*b(4)+(0.5)*p0_1(3)+(0.5)*p0_1(5);
p0_1(4)=gs_x(4,j);
gs_x(5,j)=(1/2).*b(5)+(0.5)*p0_1(4)+(0.5)*p0_1(6);
p0_1(5)=gs_x(5,j);
gs_x(6,j)=(1/2).*b(6)+(0.5)*p0_1(5)+(0.5)*p0_1(7);
p0_1(6)=gs_x(6,j);
gs_x(7,j)=(1/2).*b(7)+(0.5)*p0_1(6)+(0.5)*p0_1(8);
p0_1(7)=gs_x(7,j);
gs_x(8,j)=(1/2).*b(8)+(0.5)*p0_1(7)+(0.5)*p0_1(9);
p0_1(8)=gs_x(8,j);
gs_x(9,j)=(1/2).*b(9)+(0.5)*p0_1(8);
p0_1(9)=gs_x(9,j);
end
%print
gs_x_p10=gs_x(:,10)
%SOR
p0_2=ones(9,1);%setinitialguess
forj=1:10;
sor_x(1,j)=(1/2).*b(1)+(1/2)*p0_2(2);
p0_2(1)=(1.5)*sor_x(1,j)+(11.5)*p0_2(1);
sor_x(2,j)=(1/2).*b(2)+(0.5)*p0_2(1)+(0.5)*p0_2(3);
p0_2(2)=(1.5)*sor_x(2,j)+(0.5)*p0_2(2);
sor_x(3,j)=(1/2).*b(3)+(0.5)*p0_2(2)+(0.5)*p0_2(4);
p0_2(3)=(1.5)*sor_x(3,j)0.5*p0_2(3);
sor_x(4,j)=(1/2).*b(4)+(0.5)*p0_2(3)+(0.5)*p0_2(5);
p0_2(4)=(1.5)*sor_x(4,j)0.5*p0_2(4);
sor_x(5,j)=(1/2).*b(5)+(0.5)*p0_2(4)+(0.5)*p0_2(6);
p0_2(5)=(1.5)*sor_x(5,j)0.5*p0_2(5);
sor_x(6,j)=(1/2).*b(6)+(0.5)*p0_2(5)+(0.5)*p0_2(7);
p0_2(6)=(1.5)*sor_x(6,j)0.5*p0_2(6);
sor_x(7,j)=(1/2).*b(7)+(0.5)*p0_2(6)+(0.5)*p0_2(8);
p0_2(7)=(1.5)*sor_x(7,j)0.5*p0_2(7);
sor_x(8,j)=(1/2).*b(8)+(0.5)*p0_2(7)+(0.5)*p0_2(9);
p0_2(8)=(1.5)*sor_x(8,j)0.5*p0_2(8);
sor_x(9,j)=(1/2).*b(9)+(0.5)*p0_2(8);
p0_2(9)=(1.5)*sor_x(9,j)0.5*p0_2(9);
end
%print
sor_x_p10=sor_x(:,10)
%%SolvePartC
convergence_test_jacobi=norm(jacobi_x_p10exact_x,2)
convergence_test_gs=norm(gs_x_p10exact_x,2)
convergence_test_sor=norm(sor_x_p10exact_x,2)
ifconvergence_test_jacobi<convergence_test_sor||
convergence_test_jacobi<convergence_test_gs
fprintf('Jacobigivesbestanswerafter10iterations\n');
elseifconvergence_test_jacobi>convergence_test_sor||
convergence_test_sor<convergence_test_gs
fprintf('SORgivesbestanswerafter10iterations\n');
elseifconvergence_test_jacobi>convergence_test_gs||
convergence_test_sor>convergence_test_gs
else
fprintf('2ofthe3orall3areequallygood/badafter10
iterations\n');
end