0% found this document useful (0 votes)
37 views3 pages

CLC Clear: Gauss Elimination Code

This document contains Gauss elimination code to solve systems of linear equations. The code takes in the number of equations, the coefficient matrix A, and the constant term vector r. It performs Gauss elimination to transform A into an upper triangular matrix U and solves the system of equations to find the solution vector x. The code displays the solution x.

Uploaded by

rawad
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)
37 views3 pages

CLC Clear: Gauss Elimination Code

This document contains Gauss elimination code to solve systems of linear equations. The code takes in the number of equations, the coefficient matrix A, and the constant term vector r. It performs Gauss elimination to transform A into an upper triangular matrix U and solves the system of equations to find the solution vector x. The code displays the solution x.

Uploaded by

rawad
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

Gauss elimination code

Clc (Clears Command window.)


clear all (Removes variables from memory)
%======================================================
===========
disp('Solution of N-equation "[A][X]=[r]"')
(Displays contents of an array or string.)
m=input ('Enter number of Equations :');
A=input ('Enter Matrix [A]:');
r=input ('Enter Matrix [r]:');
D=A;d=r;
(All numbers are real without fractions)
%------------------------------------------------------
-----------
%create upper triangular matrix
U=0; (select the upper or down triangular in the matrix to)
for j=1:m-1 ( number of equation became m=2 )
if A(j,j)==0 (Relational operator: equal to.)
k=j; (it means if j=0 so k=j)
for k=k+1:m
(generates regularly spaced elements and represents an entire row or column.)

if A(k,j)==0 (if k=0 j=0)


continue
end
break
end
B=A(j,:);
C=r(j);
A(j,:)=A(k,:);
r(j)=r(k);
A(k,:)=B; r(k)=C;

end
for i=1+U:m-1
L=A(i+1,j)/A(j,j);
A(i+1,:)=A(i+1,:)-L*A(j,:);
r(i+1)=r(i+1)-L*r(j);
end
U=U+1;
end
%------------------------------------------------------
-----------
%Solution of equations
x(m)=r(m)/A(m,m);
for i=m-1:-1:1
sum=0;
for j=i+1:m
sum=sum+A(i,j)*x(j);
end
x(i)=(1/A(i,i))*(r(i)-sum)
end

Solution of N-equation "[A][X]=[r]"

Enter number of Equations :3

Enter Matrix [A]:[2 5 8;7 9 1;2 4 7]

Enter Matrix [r]:[4 7 6]

x=

0 -3.2973 1.2973

x=

5.0541 -3.2973 1.2973


Sabratah University
Chemical Engineering Department

Numerical Methods

HOMWORK (2)

Rawad Mounir Jabure

You might also like