Question
matlab
1. Given the system of equations 9 + x2 +x3 +x4 = 75 xi +8x2 x3x54 X1+X1 +7X3 + X4 = 43 xi+x2 +x6x434 Write a code to find th
0 0
Add a comment Improve this question Transcribed image text
Answer #1


Matlab code for solving clear all close all Matrix form of given linear equation b= [ 75:54:43,34]; displaying A and b matrixm-max (A1); for iri+1: ss A a#A(j,i)/m; end end Final A matrix after Gaussian elimination B-A; sz=size(A) ; x ( ss )=A( end,Eunction [x,it]-Gauss_method (A, b, x0,conv) Gauss Siedel method ea=1 ; it=0; while ea>=conv it=it+1 ; for i-1:length(b) s 0;Total number of iterations for Jacobi method is 35 Solution Matrix for Gauss Siedel is x- 4.99999999999999 Total number of it

%%Matlab code for solving
clear all
close all

%Matrix form of given linear equation
A=[9 1 1 1; 1 8 1 1; 1 1 7 1; 1 1 1 6];
b=[75;54;43;34];

%displaying A and b matrix
fprintf('Matrix A of given linear equation.\n')
disp(A)
fprintf('Matrix b vector.\n')
disp(b)
%initial guess
x0=[0;0;0;0];

%error convergence
conv=10^-12;

[x]=gauss_eliminationn(A,b);
fprintf('Solution Matrix for Gauss Elimination is x=\n ')
disp(x)

[x,it]=Jacobi_method(A,b,x0,conv);
fprintf('Solution Matrix for Jacobi method is x=\n ')
disp(x)
fprintf('Total number of iterations for Jacobi method is %d.\n',it)

[x,it]=Gauss_method(A,b,x0,conv);
fprintf('\nSolution Matrix for Gauss Siedel is x=\n ')
disp(x)
fprintf('Total number of iterations for Gauss Siedel method is %d.\n',it)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Function for Gauss Elimination
function [x]=gauss_eliminationn(A,b)
%A is the coefficient matrix and b is the result matrix for Ax=b
%x is the solution matrix
%example A=[15 6 8 11;6 6 5 3;8 5 7 6;11 3 6 9]; b=[40;20;26;29];
%so by x=gauss_eliminationn(A,b), we get x=[1 1 1 1]T;
%below is the algorithm for Gaussian elimination
    A=[A,b];%A is the matrix with A and b
    sz_A=size(A);
    ss_A=sz_A(1,1);
    %Loop for Gaussian elimination matrix formation for A matrix
    for i=1:ss_A-1
        A1=A(i:end,i);
        b=find(A1==max(A1));
        b=b+(i-1);
        A2=A(b,:);
        A(b,:)=A(i,:);
        A(i,:)=A2;
        m=max(A1);
        for j=i+1:ss_A      
            a=A(j,i)/m;
            A(j,:)=A(j,:)-a*A(i,:);
        end
    end
    %Final A matrix after Gaussian elimination
    B=A;
    sz=size(A);
    ss=sz(1,1);
    x(ss)=A(end,end)/A(end,end-1);
    %loop for backward substitution of Gaussian elimination matrix for finding x
    for ii=ss-1:-1:1
        sum=0;
        for jj=ii+1:ss
            sum=sum+A(ii,jj)*x(jj);
        end
        x(ii)=(A(ii,end)-sum)/A(ii,ii);
    end
    x=x';
end

%Function for Jacobi method
function [x,it]=Jacobi_method(A,b,x0,conv)
    %Jacobi method
    ea=1;it=0;
    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
             x1(i)=(b(i)-s)/A(i,i);
        end


        if it==1
            ea=10;
        else
            ea = norm(x1 - x0);
        end
        x0=x1;
    end
    x=x0';
end

%Function for Gauss Siedel method
function [x,it]=Gauss_method(A,b,x0,conv)
    %Gauss Siedel method
    ea=1;it=0;
    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

        if it==1
            ea=10;
        else
            ea = norm(x1 - x0);
        end
        x1=x0;

    end
    x=x0;
end


%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%%

Add a comment
Know the answer?
Add Answer to:
matlab 1. Given the system of equations 9 + x2 +x3 +x4 = 75 xi +8x2 x3x54 X1+X1 +7X3 + X4 = 43 xi+x2 +x6x434 Write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT