Question

Problem 2 [25 points] (Coding, pen and paper) Write the code to perform Jacobi and Gauss- Seidel methods for solving the line

0 0
Add a comment Improve this question Transcribed image text
Answer #1

%%%%%%%%%% Note: Problem 1 you have not given so I have taken one example problem


function xrt=gaus_jocob_Sidel()
clc;
clear all;

% 10x1 + x2 - x3 = 18
%
% x + 15x2 + 2x3 = -12
%
% -x1 + x2 + 20x3 = 17

A = [6 -1 2 1 ; 1 6 1 -1;0 1 3 1;1 -2 1 5];
b = [3; 2; -6;1];
n=length(b);
% error tolerance

disp('Exact solution by Matirx inverse')
y=inv(A)*b %%%Exact


%initial guess:
x0 = ones(n,1);
tol=1e-6; % Stoping (tolerence)
iter=1;

disp(' Solution, number of iteration and error by Gauss Jacobi method')
[x_jacobian, iter, err1]=gausjocobi(A,b,x0,tol)
disp(' Solution, number of iteration and error by Gauss Seidel method')
[x_gauss, iter, err1]=gausssidel(A,b,x0,tol)

% [x_Sor, iter]=Sor(A,b,x0,tol)
function [x_jacobian, iter, err1]=gausjocobi(A,b,x0,tol)

% Jacobi method
%---------------

%A is matrx
%b is right side matrix
%x0 is inital vector
%tol is tolerence


xnew=x0;
iter=1;
error=1;
while (error>tol )
xold=xnew;

for i=1:length(xnew)
off_diag = [1:i-1 i+1:length(xnew)];
xnew(i) = 1/A(i,i)*( b(i)-sum(A(i,off_diag)*xold(off_diag)) );
end

error=norm(xnew-xold);
err1(iter)=error;
iter = iter+1;
end
x_jacobian=xnew;

end


function [x_gauss, iter, err1]=gausssidel(A,b,x0,tol)
xnew=x0;
iter=1;
err=1;
while (err>tol)
lambda=1;
xold=xnew;
for i=1:n
I = [1:i-1 i+1:n];
xnew(i) = (1-lambda)*xnew(i)+lambda/A(i,i)*( b(i)-A(i,I)*xnew(I) );
end
err = norm(xnew-xold);
err1(iter)=err;
iter = iter+1;
  
end
x_gauss=xnew;
end


end

%%%%%%%% Solution

Exact solution by Matirx inverse

y =

1.305429864253394
0.635746606334842
-2.438914027149322
0.680995475113122

Solution, number of iteration and error by Gauss Jacobi method

x_jacobian =

1.305429958624094
0.635746583563940
-2.438914093841731
0.680995535312764


iter =

16


err1 =

Columns 1 through 3

3.933615809065920 1.574193895221642 0.486147264969486

Columns 4 through 6

0.175559380981398 0.056338328280127 0.019173026432052

Columns 7 through 9

0.006157919146378 0.002042336090436 0.000629963356748

Columns 10 through 12

0.000208036056003 0.000059033489543 0.000019697795727

Columns 13 through 15

0.000004870769552 0.000001681352088 0.000000334066886

Solution, number of iteration and error by Gauss Seidel method

x_gauss =

1.305429870055986
0.635746615687181
-2.438914040669446
0.680995480397565


iter =

12


err1 =

Columns 1 through 2

3.609349522843711 1.127507478402957

Columns 3 through 4

0.094708804346742 0.016763100058599

Columns 5 through 6

0.005555852012273 0.001033868860446

Columns 7 through 8

0.000112855725824 0.000015284172615

Columns 9 through 10

0.000005320753452 0.000001066916329

Column 11

0.000000125915926

>>

Add a comment
Know the answer?
Add Answer to:
Problem 2 [25 points] (Coding, pen and paper) Write the code to perform Jacobi and Gauss- Seidel ...
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
  • Problem 3. Find the first two iterations of both the Jacobi and the Gauss-Seidel methods for the ...

    Problem 3. Find the first two iterations of both the Jacobi and the Gauss-Seidel methods for the following linear systems, using X 0. a. b. 1011-22-9 Problem 3. Find the first two iterations of both the Jacobi and the Gauss-Seidel methods for the following linear systems, using X 0. a. b. 1011-22-9

  • How can one write a Matlab code for using Jacobi and Gauss-seidel methods to solve the...

    How can one write a Matlab code for using Jacobi and Gauss-seidel methods to solve the linear systems in exercise 7.3 question 3(a) and 3(d)? (Numerical Analysis 9th Edition by Burden and Faires)

  • 2. 3x 25」LX2 (a) Perform three iterations for the following iterative methods using initial guess x0. Compute relative residual for each iteration. (You can use a calculator) · Jacobi method » Gauss...

    2. 3x 25」LX2 (a) Perform three iterations for the following iterative methods using initial guess x0. Compute relative residual for each iteration. (You can use a calculator) · Jacobi method » Gauss-Seidel method · SOR method with ω 1.2 (b) For each iterative method, express its iteration procedure in the following matrix form: In other words, determine B and c for (2). 2. 3x 25」LX2 (a) Perform three iterations for the following iterative methods using initial guess x0. Compute relative...

  • 1. [12 marks] In the following parts of this question, write a MATLAB code to solve a linear syst...

    1. [12 marks] In the following parts of this question, write a MATLAB code to solve a linear system A b (A is a square nonsingular matrix) using Jacobi and Gauss-Seidel algorithms. Do not use the built-in Matlab functions for solving linear systems (a) Write a Matlab function called Jacobi that consumes a square n x n matrix A, and an n x 1 vector b, and uses the Jacobi technique to solve the system Ax-b, starting with the zero...

  • just 1,2,4 Problem 1 Consider the linear system of equations Ax = b, where x €...

    just 1,2,4 Problem 1 Consider the linear system of equations Ax = b, where x € R4X1, and A= 120 b = and h= 0.1. [2+d -1 0 0 1 1 -1 2+d -1 0 h2 0 -1 2 + 1 Lo 0 -1 2+d] 1. Is the above matrix diagonally dominant? Why 2. Use hand calculations to solve the linear system Ax = b with d=1 with the following methods: (a) Gaussian elimination. (b) LU decomposition. Use MATLAB (L,...

  • Task 2 (25 points + 4 points for commenting): Write computer code to perform the Fixed.Point...

    Task 2 (25 points + 4 points for commenting): Write computer code to perform the Fixed.Point method and use your code to find the root of the following equation using an initial guess of 3 and a stopping criterion of 0.001%; f(x) e -4x For Fixed-Point, you do not have to code Matlab to take the derivatives of the function and check the g'(x). You can do that step by hand, and then show me your hand cal ulations to...

  • Question 1 (10 marks) For a linear system Ax- b with 1 0 -1 A-1 2-1 2 -1 3 b=14 18 and compute by...

    Please do question 5 for me. Thanks Question 1 (10 marks) For a linear system Ax- b with 1 0 -1 A-1 2-1 2 -1 3 b=14 18 and compute by hand the first four iterations with the Jacobi method, using x()0 Hint: for the ease of calculation, keep to rational fractions rather than decimals Question 2 For the same linear system as in Question 1, compute by hand the first three iterations (10 marks) with the Gauss Seidel method,...

  • Question 1 (10 marks) For a linear system Ax b with 1 0-1 A-1 2-1 2-13 and b4 18 compute by hand ...

    Question 1 (10 marks) For a linear system Ax b with 1 0-1 A-1 2-1 2-13 and b4 18 compute by hand the first four iterations with the Jacobi method, usg0 Hint: for the ease of calculation, keep to rational fractions rather than decimals. (10 marks) Question 2 For the same linear svstem as in Question 1. compute by hand the first three iterations with the Gauss Seidel method, us0 Hint: for the ease of calculation, keep to rational fractions...

  • PLEASE USE MATLAB. The code is basically given to you with the algorithm above. Write a...

    PLEASE USE MATLAB. The code is basically given to you with the algorithm above. Write a program for solving the system Ar-b by using Gauss-Seidel iterative method discussed in class that uses a computer memory for one iterate only. This could be summarized as follows: Input n -- the size of the system, matrix A-(a(i,j), the vectoir b (b (1), . . . , b(n)), the intitial guess x (x(1), ..., x(n)) (you can use x=b) maximum number of iterations...

  • Solve b and c only a) i. A numcrical scheme is known to perform best if...

    Solve b and c only a) i. A numcrical scheme is known to perform best if the height of the uniform subinterval is small enough in the interval (0.2]. The equation that gives the height h of the uniform subinterval is given by f(h) = 3 +h-1=0 Use the fixed point method of finding roots of equations to find the height h for which the scheme performs best. Conduct only four (4) iterations. 6 Marks ii. find algebraically all the...

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