In: Advanced Math
Use ten iterations of the appropriate MATLAB function, with x^(0)=[0,...,0]', to solve Ax=b (approximately).
B) use Gauss-siedel iteration.
C)use SOR with w=1.25, w=1.5, w=1.75,w=1.9, and optimal value if given.
* A=[4,8,0,0;8,18,2,0;0,2,5,1.5;0,0,1.5,1.75] , B=[8;18;0.5;-1.75]. , (optimal w is 1.634.)
%%Matlab code for Gauss Siedel Method and Jacobi method
clear all
close all
%A_matrix is the Coefficient Marix A,b_matrix is the Result
Matrix b,
A_matrix=[4,8,0,0;8,18,2,0;0,2,5,1.5;0,0,1.5,1.75];
b_matrix=[8;18;0.5;-1.75];
ww=[1.25 1.5 1.75 1.9];
%total number of iterations
itr=10;
%exact solution
x0=[0 0 0 0];
%displaying the matrix
fprintf('The A matrix is \n')
disp(A_matrix)
fprintf('The b matrix is \n')
disp(b_matrix)
%result for Jacobi method
[x_j]=Jacobi_method(A_matrix,b_matrix,x0,itr);
%result for Gauss Siedel method
[x_g]=Gauss_method(A_matrix,b_matrix,x0,itr);
%Printing the result
fprintf('\n\tThe result using Gauss Siedel Method after 10
iterations is\n');
disp(x_j)
%Printing the result
fprintf('\n\tThe result using Jacobi Method after 10 iterations
is\n');
disp(x_g)
for i=1:length(ww)
%result for Gauss SOR method
[x_sor]=SOR(A_matrix,b_matrix,x0,ww(i),itr);
%Printing the result
fprintf('\n\tThe result using SOR
Method for w=%f after 10 iterations is\n',ww(i));
disp(x_sor')
end
%result for Gauss SOR method
[x_sor]=SOR(A_matrix,b_matrix,x0,1.634,itr);
%Printing the result
fprintf('\n\tThe result using SOR
Method for w=1.634 after 10 iterations is\n');
disp(x_sor')
%Function for Jacobi method
function [x]=Jacobi_method(A,b,x0,itr)
%Jacobi method
it=0;
for nn=1:itr
%while ea>=conv
it=it+1;
for i=1:length(b)
s=0;
for j=1:length(b)
if i~=j
s=s+A(i,j)*x0(j);
end
end
x2(i)=(b(i)-s)/A(i,i);
end
x0=x2;
end
x=x0';
end
%Function for Gauss Siedel method
function [x]=Gauss_method(A,b,x0,itr)
%Jacobi method
it=0;
for nn=1:itr
%while ea>=conv
it=it+1;
for i=1:length(b)
s=0;
for j=1:length(b)
if i~=j
s=s+A(i,j)*x0(j);
end
end
x0(i)=(b(i)-s)/A(i,i);
end
%x1=x0;
end
x=x0';
end
%%Matlab function for Gauss Siedel SOR Method
function [x]=SOR(A,b,x0,lambda,itr)
%inputs are A=matrix A; b=result vector, x0=initial guess
%maxit=maximum iteration;conv=error convergence
%outputs are x=solution matrix, it=maximum iteration; lambda=w for
sor
%Gauss Siedel method
k=0;
for nn=1:itr
%while ea>=conv
k=k+1;
for i=1:length(b)
s=0;
for j=1:length(b)
if i~=j
s=s+A(i,j)*x0(j);
end
end
x11=((lambda*(b(i)-s))/A(i,i))+(1-lambda)*x0(i);
x0(i)=x11;
end
end
x=x0;
end
%%%%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%%%%%%%