Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please include matlab code comments and comments on results Write a MATLAB scrip

ID: 3348551 • Letter: P

Question

Please include matlab code comments and comments on results

Write a MATLAB script to perform the Gauss-Seidel method for the following A and b: ?3.1-1 21 3 5.1 2 11 45.1 9.2 3.1 Use an initial guess of a [0, 0,0 with a stopping criterion of s 10-6, and perform Gauss-Seidel without relaxation, with SOR, and with under relaxation. Commen 5 bonus points: Modify your script to be able to implement the Jacobi method with relaxation based on a variable at the beginning of the script; that is, if the variable has one value, Ga if the variable has another value, Jacobi will be used. Do not have one entire section for Gauss-Seidel/Jacobi at the top and then another entire section for Jacobi/Gauss-Seidel at the bottom. For relaxation, t should t on your results. uss-Seidel will be used, anc oecur on the entire a vector at the end of the iteration.

Explanation / Answer

1st Function

%%Matlab function for Gauss Siedel Method
function [x1,x2,x3,imax]=Gauss_Siedel3(A_matrix,b_matrix,Relax_coeff,conv_es)

%A_matrix is the Coefficient Marix A,b_matrix is the Result Matrix b,
%Relax_coeff is the Relaxation Coefficient and conv_es is stopping
%criterion

it=1000;        %Maximum Iteration number
%%Gauss Siedel Method
x1=0;x2=0;x3=0;    %Initialization of x

%Iteration for Gauss Siedel
   for i=1:it
        x11=Relax_coeff*((b_matrix(1)-A_matrix(1,2)*x2-A_matrix(1,3)*x3)/A_matrix(1,1))+((1-Relax_coeff)*x1);
        cc1=(abs((x11-x1)/x1));
        x1=x11;
        x22=Relax_coeff*((b_matrix(2)-A_matrix(2,1)*x1-A_matrix(2,3)*x3)/A_matrix(2,2))+((1-Relax_coeff)*x2);
        cc2=(abs((x22-x2)/x2));
        x2=x22;
        x33=Relax_coeff*((b_matrix(3)-A_matrix(3,1)*x1-A_matrix(3,2)*x3)/A_matrix(3,3))+((1-Relax_coeff)*x3);
        cc3=(abs((x33-x3)/x3));
        x3=x33;
        cc=(cc1+cc2+cc3)/3;
        %If convergence criterion obtained then break the loop
        if cc<=conv_es
            break
        end
    end
%Printing the result
fprintf(' The result using Gauss Siedel for relaxation coefficient = %f is ',Relax_coeff);
fprintf(' x1=%f, x2=%f, x3=%f ',x1,x2,x3);
imax=i;

end

2nd Function

%%Matlab Function for Jacobi Method
function [x1,x2,x3,imax]=jacobi3(A_matrix,b_matrix,Relax_coeff,conv_es)

%A_matrix is the Coefficient Marix A,b_matrix is the Result Matrix b,
%Relax_coeff is the Relaxation Coefficient and conv_es is stopping
%criterion

it=1000;        %Maximum Iteration number
%%Gauss Siedel Method
x1=0;x2=0;x3=0;    %Initialization of x

%Iteration for Gauss Siedel
   for i=1:it
        x11=Relax_coeff*((b_matrix(1)-A_matrix(1,2)*x2-A_matrix(1,3)*x3)/A_matrix(1,1))+((1-Relax_coeff)*x1);
        cc1=(abs((x11-x1)/x1));
        x22=Relax_coeff*((b_matrix(2)-A_matrix(2,1)*x1-A_matrix(2,3)*x3)/A_matrix(2,2))+((1-Relax_coeff)*x2);
        cc2=(abs((x22-x2)/x2));
        x33=Relax_coeff*((b_matrix(3)-A_matrix(3,1)*x1-A_matrix(3,2)*x3)/A_matrix(3,3))+((1-Relax_coeff)*x3);
        cc3=(abs((x33-x3)/x3));
        cc=(cc1+cc2+cc3)/3;
        x1=x11;x2=x22;x3=x33;
        %If convergence criterion obtained then break the loop
        if cc<=conv_es
            break
        end
    end
%Printing the result
fprintf(' The result using Jacobi for relaxation coefficient = %f is ',Relax_coeff);
fprintf(' x1=%f, x2=%f, x3=%f ',x1,x2,x3);
imax=i;

end

3rd Function

function [x,y,z,i]=Gauss_Jacobi(A,b,relax_co1,stop_crit,v)
%This function will solve any equation either in Gauss Siedel or in Jacobi
%method for given A b relax_coefficien, stopping criterion and a variable v
%if v is 1 then it will solve using Gauss Siedel method if v is any value
%other than 1 then it will solve using Jacobi method
if v==1
    [x,y,z,i]=Gauss_Siedel3(A,b,relax_co1,stop_crit);
    fprintf(' Result for Gauss_Siedel and it coversge after %d iteration ',i)
else
    [x,y,z,i]=jacobi3(A,b,relax_co1,stop_crit);
    fprintf(' Result for Jacobi and it coversge after %d iteration ',i)
end

Matlab code and result.

%Matlab code for finding solution using Gauss Siedel method Jacobi method
%for running this code it will call three function Gauss_Siedel3.m,
%jacobi3.m and Gauss_Jacobi.m
clear all
close all
%The coefficient matrix A
A=[3.1 -1 2;-3 5.1 2;1 4 5.1];
%The result matrix B
b=[9.2 -9.1 3.1];
%Stopping Criterion
stop_crit=10^-6;
%Relaxation coefficients
relax_co1=1.1; %for SOR
relax_co2=.89; %for Under Relaxation
relax_co3=1;    %for without relaxation
%Result for diffrent relaxation
%It will call Gauss_Siedel3.m function which should be kept in same folder
%along with the code
[x1,y1,z1,i1]=Gauss_Siedel3(A,b,relax_co1,stop_crit);
fprintf(' Result for SOR and it coversge after %d iteration ',i1)
[x2,y2,z2,i2]=Gauss_Siedel3(A,b,relax_co2,stop_crit);
fprintf(' Result for Under relaxation and it coversge after %d iteration ',i2)
[x3,y3,z3,i3]=Gauss_Siedel3(A,b,relax_co3,stop_crit);
fprintf(' Result for without relaxation and it coversge after %d iteration ',i3)

%For answering second part
v=1;%variable value
Gauss_Jacobi(A,b,relax_co2,stop_crit,v);
v=2;%variable value
Gauss_Jacobi(A,b,relax_co2,stop_crit,v);

Result
          The result using Gauss Siedel for relaxation coefficient = 1.100000 is
          x1=2.935126,
          x2=-0.064874,
          x3=0.018118

          Result for SOR and it coversge after 105 iteration

          The result using Gauss Siedel for relaxation coefficient = 0.890000 is
          x1=2.935126,
          x2=-0.064874,
          x3=0.018118

          Result for Under relaxation and it coversge after 25 iteration

          The result using Gauss Siedel for relaxation coefficient = 1.000000 is
          x1=2.935126,
          x2=-0.064874,
          x3=0.018118

          Result for without relaxation and it coversge after 40 iteration

          The result using Gauss Siedel for relaxation coefficient = 0.890000 is
          x1=2.935126,
          x2=-0.064874,
          x3=0.018118

          Result for Gauss_Siedel and it coversge after 25 iteration

          The result using Jacobi for relaxation coefficient = 0.890000 is
          x1=2.935126,
          x2=-0.064874,
          x3=0.018118

          Result for Jacobi and it coversge after 51 iteration

Comment

From the results we can say that for Gauss_Siedel method it will converge faster than Jacobi Method, and for Gauss_Siedel method in under relaxation case it converge faster than SOR and without relaxation case. All three function and code should be kept in same folder.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote