0% found this document useful (0 votes)
2 views

A2

This document contains a MATLAB script implementing the Gauss-Seidel method for solving a system of linear equations. It initializes with a guess and iteratively refines the solution over 15 iterations using the input matrix and constants. The final output provides an approximate solution to the equations after the specified iterations.

Uploaded by

danial2k24
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)
2 views

A2

This document contains a MATLAB script implementing the Gauss-Seidel method for solving a system of linear equations. It initializes with a guess and iteratively refines the solution over 15 iterations using the input matrix and constants. The final output provides an approximate solution to the equations after the specified iterations.

Uploaded by

danial2k24
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/ 3

MTH375 – Numerical Computations

Lab Assignment # 2

Name M. Daniyal Akram

Registration FA21-BEE-233
Number

Class BEE-7B
Script file of Gauss Seidel method:
function [x] = GaussSeidel(A, b, xo, N)
A = [5 -2 1; -3 10 -1; 1 1 5];
b = [-15; 45; -7];
xo = [0; 0; 0];
N = 15;
L = length(b);
x = xo;
for i = 1:N
for k = 1:L
x(k) = (b(k) - A(k, [1:k-1]) * x([1:k-1]) - A(k, [k+1:L]) * xo([k+1:L])) /
A(k, k);
end
xo = x
end
x
end

Output:
Explanation:
This MATLAB code implements the Gauss-Seidel iterative method to solve a
system of linear equations Ax=b. It starts with an initial guess xo = [0;0;0] and
refines the solution over 15 iterations. The input matrix A contains the coefficients
of the equations, b represents the constants on the right-hand side, and xo is the
initial guess for the solution. For each iteration, the code updates each variable x(k)
by solving the corresponding equation, using the most recently updated values for
the variables already calculated in the current iteration and the previous iteration's
values for the remaining variables. After updating all variables, the current solution
x is stored and used for the next iteration. The final result, x, gives an approximate
solution to the system of equations after the specified number of iterations.

You might also like