0% found this document useful (0 votes)
15 views4 pages

Na Lab 4

Uploaded by

romesaali23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Na Lab 4

Uploaded by

romesaali23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

INSTITUTE OF SPACE

TECHNOLOGY
ISLAMABAD

Numerical Analysis Lab 4

Submitted To: Dr Umair Ali


Submitted by: Syed Muhammad Ali Jelani
Reg No.: 220101039
Batch: Aero21-A

Department of Aeronautics and Astronautics, Institute of Space Technology Islamabad


Matlab code for Jacobi Method:
clc;
clear all;
A = [4, -1, 0; -1, 4, -1; 0, -1, 3]; % Coefficient matrix of the system of
equations
b = [1; 4; 2]; % Right-hand side vector
x0 = [0; 0; 0]; % Initial guess for the solution vector
tol = 1e-6; % Tolerance for convergence
max_iter = 1000; % Maximum number of iterations

n = length(b); % Number of equations (size of the system)


x = x0; % Initial guess for the solution vector
x_new = x0; % Initialize the updated solution vector
iter = 0; % Initialize the iteration counter

% Perform Jacobi iterations until convergence or maximum iterations reached


while iter < max_iter
% Iterate over each equation
for i = 1:n
sigma = 0;
% Calculate the sum of the product of coefficients and solution values,
excluding the diagonal element
for j = 1:n
if j ~= i
sigma = sigma + A(i, j) * x(j);
end
end
% Calculate the new value for the solution vector using the Jacobi
iteration formula
x_new(i) = (b(i) - sigma) / A(i, i);
end

% Print current iteration


disp(['Iteration ', num2str(iter + 1), ':']);
disp(x_new);

% Check for convergence based on the maximum norm of the difference between
old and new solution vectors
if norm(x_new - x, inf) < tol
break;
end
% Update the solution vector for the next iteration
x = x_new;
% Increment the iteration counter
iter = iter + 1;
end

% Check if maximum iterations reached without convergence


if iter == max_iter
disp('Maximum iterations reached without convergence');
end

% Display the final solution vector and the number of iterations performed
disp('Final Solution:');
disp(x_new);
disp(['Number of iterations:', num2str(iter)]);
Output:

Matlab code for Guass siedal:


A = [4, -1, 0; -1, 4, -1; 0, -1, 3];
b = [1; 4; 2];
x0 = [0; 0; 0];
tol = 1e-6;
max_iter = 1000;

n = length(b);
x = x0;
iter = 0;
while iter < max_iter
x_new = x;
for i = 1:n
sigma = 0;
for j = 1:i-1
sigma = sigma + A(i, j) * x_new(j);
end
for j = i+1:n
sigma = sigma + A(i, j) * x(j);
end
x_new(i) = (b(i) - sigma) / A(i, i);
end

% Display current iteration


disp(['Iteration ', num2str(iter + 1), ':']);
disp(x_new);
if norm(x_new - x, inf) < tol
break;
end
x = x_new;
iter = iter + 1;
end

if iter == max_iter
disp('Maximum iterations reached without convergence');
end

disp('Solution:');
disp(x);
disp(['Number of iterations:', num2str(iter)]);

output:

You might also like