%%Matlab code for Gauss Siedel Method and Jacobi method
clear all
close all
%Loop for table for n iterations
nn=[5 250 1250];
fprintf('Table for Jacobi and Gauss iterations\n\n')
fprintf('\tn\terr_jacob\tn_jacob\terr_gauss\tn_gauss\n\n')
for i=1:3
n=nn(i);
%creating A matrix
A=ones(n,n);
for ii=1:n
A(ii,ii)=n;
end
%creating B vector
b=ones(n,1);
%initial x and convergence
x0=zeros(n,1);
conv=10^-6;
%Jacobi method
[x_j,it_j]=Jacobi_method(A,b,x0,conv);
%Gauss Siedel method
[x_g,it_g]=Gauss_method(A,b,x0,conv);
%exact solution
x_ext=A\b;
error_j=norm(x_j-x_ext);
error_g=norm(x_g-x_ext);
fprintf('\t%d,\t%e,\t%d,\t%e,\t%d,\n',n,error_j,it_j,error_g,it_g)
end
%measuring runtime
n=50;
A=ones(n,n);
for ii=1:n
A(ii,ii)=n;
end
%creating B vector
b=ones(n,1);
%initial x and convergence
x0=zeros(n,1);
conv=10^-6;
%loop for 10 time running
for i=1:10
tic;
[x_j,it_j]=Jacobi_method(A,b,x0,conv);
t_jac(i)=toc;
tic;
[x_g,it_g]=Gauss_method(A,b,x0,conv);
t_gau(i)=toc;
end
fprintf('\n\nMean Runtime for Jacobi method for n=%d is
%f.\n',n,mean(t_jac))
fprintf('Mean Runtime for Gauss Siedel method for for n=%d is
%f.\n',n,mean(t_gau))
%Function for Jacobi method
function [x,it]=Jacobi_method(A,b,x0,conv)
%Jacobi method
ea=1;it=0;x1=zeros(length(b),1);
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;
if it==1000
break
end
if it==1
ea=10;
else
ea = norm(x1 - x0',
'inf');
end
x1=x0;
end
x=x0';
end
%Function for Gauss Siedel method
function [x,it]=Gauss_method(A,b,x0,conv)
%Jacobi method
ea=1;it=0;x1=zeros(length(b),1);
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==1000
break
end
if it==1
ea=10;
else
ea = norm(x1 - x0',
'inf');
end
x1=x0;
end
x=x0';
end
%%%%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%%%%%%%
1. [12 marks] In the following parts of this question, write a MATLAB code to solve a linear syst...
in MATLAB 3. Write a function called gauss_seidel that inputs an n x n matrix, A, a column vector, b, an initial guess xo), an error tolerance e, and a maximum number of iterations, and output an approximate solution obtained using the Gauss-Seidel method, the error and the number of iterations. The header should look like (x, err, N] = gauss_seidel (A, b, x0, tol, Nmax). Use the method to find approximate solutions to the linear system -2 1 0...
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,...
Relevant Information: 1" (20%) (Linear systems) Given a linear system C1 +33 2 One can convert it into an iterative formula x(n+1) TX(m) + c where X(n) = (a (n),X(n), a (n))t įs the approximated solution at the nth iteration, T3x3 is the iterative matrix and caxi is the vector associated with the correspondent iterative method. (a) (5 %) Compute the associated matrix T and vector c associated with Jacobi method. (b) (5 %) Compute (T) and determine if Jacobi...
DO NOT GIVE ME A STEP BY STEP SOLUTION ON PAPER. JUST GIVE ME THE MATLAB FUNCTION FILE (code) TO BE USED IN MATLAB. ONLY (a) use the Gauss-Seidel method to solve the following system until the percent relative error falls below s a. 5%. 10x1 + 2x2-x,-27 3x1 -6x2 + 2x3 61.5 25x321.5 b. (b) write an M-file to implement the Gauss-Seidel method using the above system as a test case (a) use the Gauss-Seidel method to solve the...
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...
I am new to Matlab so details would be appreciated! Use Matlab or any other Language /Tool to implement the project work Algorithm for Gauss-Seidel Method to solve the linear (n x n) system Ax = b in matrix form is given by x(0) = initial vector x(4+1) = D-1 (b – Lx(k+1) – Ux(k)), k = 0, 1, 2, ...... where A = L+D+U. Here L, D, U are respectively lower, diag- onal and upper matrices constructed from A....
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...
For the following two problems, use the built-in simple MATLAB matrix algebra rules, i.e. if you're multiplying a matrix L by a vector xi, it's just L*xi. You do not have to code matrix multiplication in for loops. Code up the Jacobi Method and use it to solve the large matrix of problem 3 from HW5. Return the iteration count to solve this problem and a plot of the solution. Modify the Jacobi method to use the most current information...
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...
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,...