In: Advanced Math
Use ten iterations of the appropriate MATLAB function, with x^(0)=[0,...,0]', to solve Ax=b (approximately).
A)use Jacobi iteration.
B) use Gauss-siedel iteration.
1) make sure to use SOR with w=1.25, w=1.5, w=1.75,w=1.9, and optimal value if given.
* A=[1,-2,0,0;-2,5,-1,0;0,-1,2,-0.5;0,0,-0.5,1.25]] , B=[-3;5;2;3.5]. , (optimal w is 1.5431.)




%%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=[1 -2 0 0;-2 5 -1 0;0 -1 2 -0.5;0 0 -0.5 1.25];
b_matrix=[-3;5;2;3.5];
ww=[1.25 1.5 1.75 1.9];
%total number of iterations
itr=100;
%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.5431,itr);
    %Printing the result
      fprintf('\n\tThe result using SOR
Method for w=1.5431 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 %%%%%%%%%%%%%%%%%%%%%%%