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
  • 3 Linear systems 18. Solve the linear system of equations using the Naive Gauss elimination metho...

    3 Linear systems 18. Solve the linear system of equations using the Naive Gauss elimination method x,+x: + x) = 1 +2x, +4x1 x 19. Solve the linear system of equations using the Gauss elimination method with partial pivoting 12x1 +10x2-7x3=15 6x, + 5x2 + 3x3 =14 24x,-x2 + 5x, = 28 20. Find the LU decomposition for the following system of linear equations 6x, +2x, +2, 2 21. Find an approximate solution for the following linear system of equations...

  • Please answer this MATLAB questions when able. Thanks. 4. Laboratory Problem Description In this laboratory you...

    Please answer this MATLAB questions when able. Thanks. 4. Laboratory Problem Description In this laboratory you are required to Find the solution of the following systems of linear equation: 1) xl + x2 + x3 3 4x1 - x2 x3-2 x1 2x2 x3-2 2) 2 -1 3 A 1 3 -2. B-2 Given the following system 4x1+3x2+7x3- 3 3x1+2x2+1x3 1 2x1+3x2+4x3- 2 Using MATLAB commands solve the following system using Gaussian elimination with partial pivoting. Find P, L, and U...

  • Consider the linear system 5x1 - 21 + X1 - 22 + x3 = 1 5.22...

    Consider the linear system 5x1 - 21 + X1 - 22 + x3 = 1 5.22 - 23 = 2 22 5 5x3 = 3 (a) Discuss the convergence of the iterative solutions of this system generated by the Jacobi and Gauss-Seidel methods, by considering their iterative matrices. (b) If both methods converge, which one of them converges faster to the exact solution of this system? (c) Starting with the initial approximation x(0) = [0,0,0], find the number of iterations...

  • Problem 2 (50 points): Estimate x1 and x2 from the following system of equations using 4 iteratio...

    Problem 2 (50 points): Estimate x1 and x2 from the following system of equations using 4 iterations of the Gauss-Seidel method with α,-1 and an initial guess of x,-1 and X2-1. x2-3x, +1.9 0 x,+x-3.0 = 0 Problem 2 (50 points): Estimate x1 and x2 from the following system of equations using 4 iterations of the Gauss-Seidel method with α,-1 and an initial guess of x,-1 and X2-1. x2-3x, +1.9 0 x,+x-3.0 = 0

  • 1. (45 pts) Given the following system of linear equations: Xi – X2 + x3 =...

    1. (45 pts) Given the following system of linear equations: Xi – X2 + x3 = X 1 + x2 + 6x3 = —2x1 + 8x2 + 4x3 = 3 (a) (3 pts) Write it in the form of Ax = b (b) (12 pts) Find all solutions of the system using Gauss elimination method. Write your answer as a column vector. (c) (4 pts) Use matrix multiplication to check your answer if possible. (d) (10 pts) Use Cramer's rule...

  • Test II. ITERATIVE SOLUTION OF SYSTEMS OF LINEAR EQUATIONS Solve the following linear system using Gauss-Seidel...

    Test II. ITERATIVE SOLUTION OF SYSTEMS OF LINEAR EQUATIONS Solve the following linear system using Gauss-Seidel iterative method. Use x = x; = x; =0 as initial guesses. Perform two iterations of the method to find xị, xį and xſ and fill the following table. Show all the calculation steps. 10x, + 2x2 - X3 = 27 -3x, - 6x2 + 2xz = -61.5 X1 + x2 + 5x3 = -21.5

  • Solve the given system of linear equations by Gauss-Jordan elimination: -X1 + x2 + x3 =...

    Solve the given system of linear equations by Gauss-Jordan elimination: -X1 + x2 + x3 = 5 5x + 3x, – x3 = 3 2x + 4x2 + x3 = 11 [6 marks]

  • Could someone explain these four promblems on matlab and if you do, could you write what...

    Could someone explain these four promblems on matlab and if you do, could you write what you wrote on matlab I.e. on the command window or script. Also if you have written anything by hand can you write neatly. Also an explain of how you did it would be greatly appreciated. 1] 5 points) Write the following set of equations in Matrix form and use Matlab to find the solution. 50 -5x3-6x2 2x2 + 7x3-30 x1-7x3-50-3x2 + 5x1 [2] (10...

  • Given the equations write a Matlab Function File (code) for 10x1 + 2x2 - x3 =...

    Given the equations write a Matlab Function File (code) for 10x1 + 2x2 - x3 = 27 -3x1 -5x2 +2x3 = -61.5 x1 +x2 +6x3 = -21.5 (A) Compute the determinant (B) Use Cramer's rule to solve for the x's (C) Solve by naive Gauss elimination. Show all steps of the computation.

  • Solve using Gauss Jordan 3) Given the following set of linear equations x, +2x2-x3 +x4=5 -xi-2x2-3x3...

    Solve using Gauss Jordan 3) Given the following set of linear equations x, +2x2-x3 +x4=5 -xi-2x2-3x3 + 2x4 = 7 x, +x2 + x3+x4=10

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