0% found this document useful (0 votes)
38 views7 pages

Assignment

The document contains MATLAB code for numerical integration and solving systems of linear equations using various methods like Gauss-Jacobi, Gauss-Seidel, LU decomposition, Cholesky decomposition etc. It provides functions to calculate integrals using Simpson's, Romberg, Boole's and Weddle rules. Functions are also given for Trapezoidal rule and solving linear systems using Gauss-Jacobi, Gauss-Seidel and LU decomposition methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views7 pages

Assignment

The document contains MATLAB code for numerical integration and solving systems of linear equations using various methods like Gauss-Jacobi, Gauss-Seidel, LU decomposition, Cholesky decomposition etc. It provides functions to calculate integrals using Simpson's, Romberg, Boole's and Weddle rules. Functions are also given for Trapezoidal rule and solving linear systems using Gauss-Jacobi, Gauss-Seidel and LU decomposition methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Page | 1

Assignment

Numerical Analysis Lab


Matlab Programs

Submitted by : Syed Zohaib Ali


Reg Number : 2018-CH-428
Submitted To : Dr. Abdur-Rehman
Date Of Submission : 27-March-2021
Department : Chemical Enghineering
Page | 2

Guass Jacobi Method:

function [gaussjacobi] = gjf(A,B,raccur,maxiter)


[m,n]=size (A);
if(m~=n)
error('matrix of coefficient must be square');
end sum=0;
for i=1:m
for j=1:n
if (i==j)
continue;
end
sum=sum+abs(A(i,j));
end
if (abs(A(i,i))<sum)
error('matrix is not dignolly dominant');
end
sum=0;
end
iter=0;
daccur=1;
for i=1:n
xi(i)=0;
end
while(iter<maxiter & daccur>raccur)
xo=xi;
for i=1:n
sum=0;
for j=1:n
if (i==j)
continue;
end
sum=sum+A(i,j)*xo(j);
end
xi(i)=(1/A(i,i)*(B(i)-sum));
end
accur=abs(xi-xo);
daccur=max(accur);
iter=iter+1;
end
sol=xi;
disp(sol);
end
Page | 3

1
Simson Method:
3

function x=simson(func,a,b,h)
n=(b-a)/h k=1;
for i=a;h;b
b(k)=func(i) k=k+1;
end
no=length(b)
sum1=0;
sum2=0;
for i=2:2:no-1
sum1=sum1+4*b(i)
end
for i=3:2:no-1
sum2=sum2+2*b(i)
end
ans=h/3*(b(1)+b(no)+(sum1)+(sum2))
end

𝟑
Simson Method:
𝟖

function x=simson(func,a,b,h)
n=(b-a)/n
k=1;
for i=a;h;b
b(k)=func(i)
k=k+1;
end
no=length(b)
sum1=0;
sum2=0;
sum3=0;
for i=2:3:no-1
sum1=sum1+3*b(i)
end
for i=3:3:no-1
sum2=sum2+3*b(i)
end
for i=4:3:no-1
sum3=sum3+2*b(i)
end
ans=(3*h/8)*(sum1+sum2+sum3+b(1)+b(no))
end
Page | 4

Romberg Integration:

function x = romberg(func,a,b,n)
h=(b-a)/n
h1=b-a
k=1;
for i=a:h:b
b(k)=func(i);
k=k+1;
end
no=length(b);
I11=1/2*(b(1)+b(no));
I12=(I11+func(a+h1/2));
I13=(I12+func(a+h1/4)+func(a+3*h1/4));
I14=(I13+func(a+h1)/8)+func(a+(3*h1)/8)+func(a+(5*h1)/8)+func(a+(7*h1)/8);
T11=h1*I11;
T12=(h1/2)*I12;
T13=(h1/4)*I13;
T14=(h1/8)*I14;
T22=T12+(1/3)*(T12-T11);
T23=T13+(1/3)*(T13-T12);
T24=T14+(1/3)*(T14-T13);
T33=T23+(1/15)*(T23-T22);
T34=T24+(1/15)*(T24-T23);
T44=T34+(1/63)*(T34-T33);
ans=T44;
disp ans;
end

Bool’s Method:

function x=bool(func,a,b,n)
h=(b-a)/n;
k=1;
for i=a;h;b
b(k)=func(i);
k=k+1;
end
m=length(b);
sum1=0;
sum2=0;
for j=2:2:m-1
sum1=sum1+32*b(j);
end
for j=3:2:m-1
sum2=sum2+12*b(j);
end
ans=(2/45*h)*(7*b(1)+7*b(m)+(sum1)+(sum2));
end
Page | 5

Weddle Method:

function x=weddle(func,a,b,n)
h=(b-a)/n
k=1;
for i=a;h;b
b(k)=func(i)
k=k+1;
end m=length(b);
sum1=0;
sum2=0;
sum3=0;
for i=2:4:m-1
sum1=sum1+5*b(i);
end
for i=4:4:m-1
sum2=sum2+6*b(i);
end
for i=3:2:m-1
sum3=sum3+b(i);
end
ans=(3*h/10)*(b(1)+b(m)+sum1+sum2+sum3);
display(ans);
end

Trapizoidal Method:
function ans = Trapizodal(f,a,b,n)
disp('it is trapizodal rule dude')
h=(b-a)/n;
sum=0;
for i=a+h:h:b-h
sum=sum+feval(f,i);
end
answ=h*(feval(f,a)+2*sum+feval(f,b))/2
end
Page | 6

Choleski Method:
function cholesski
A=input('enter the matrix');
b=A';
if (A~=b)
error ('this is not symmetric matrix');
end
M=input('enter number of rows of matix');
N=M;
I=eye([M,N])
[L,U]=lu(A)
LT=U
AI=LT\inv(L)
b=AI*A
end

LU Decomposition Method:
functionDecoposition method;
A=input('enter A matrix')
[L,U]=lu(A)
b=input('Enter B matrix')
y=L/b;

y;

x=U/y;

end
Page | 7

Gauss Sheidal Method:


function [gausssheidalmethod] = gsmf(A,B,raccur,maxiter)
disp('solving problem by gauss seidal method for any order of matrix');
[m,n]=size(A);
if (m~=n)
error('matrix of coefficient must be square');
end sum=0;
for i=1:m
for j=1:n
if (i==j)
continue;
end
sum =sum+abs(A(i,j));
end
if (abs(A(i,i))<sum)
error ('matrix is not diagonally dominant');
end
sum=0;
end
iter=0;daccur=1;
for i=1:n
xi(i)=0;
end
while(iter<maxiter & daccur>raccur)
xo=xi;
for i=1:n
sum1=0;
sum2=0;
for j=1:(i-1)
sum1=sum1+A(i,j)*xi(j);
end
for j=(1+i):n
sum2=sum2+A(i,j)*xo(j);
end
xi(i)=(1/A(i,i)*(B(i)-sum1-sum2));
end
accur=abs(xi-xo);
daccur=max(accur);
iter=iter+1;
end
sol=xi;
fprintf('approxmate solution is\n'); disp(sol);
fprintf('After %i itterations\n',iter); fprintf('with accuraccy of
%e\n',daccur);
end

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy