0% found this document useful (0 votes)
74 views10 pages

Clear CLC: Numerical Integration

This document discusses numerical integration techniques including trapezoidal rule, Simpson's 1/3 rule, and Simpson's 3/8 rule. It provides code to calculate integrals using these different rules, and compares the numerical solutions to the exact solution by plotting the graphs. The document also discusses solving simultaneous linear equations using Gauss-Jordan elimination and finding roots of non-linear equations using the Regula-Falsi method.

Uploaded by

SM Mecreg
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)
74 views10 pages

Clear CLC: Numerical Integration

This document discusses numerical integration techniques including trapezoidal rule, Simpson's 1/3 rule, and Simpson's 3/8 rule. It provides code to calculate integrals using these different rules, and compares the numerical solutions to the exact solution by plotting the graphs. The document also discusses solving simultaneous linear equations using Gauss-Jordan elimination and finding roots of non-linear equations using the Regula-Falsi method.

Uploaded by

SM Mecreg
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/ 10

end;

NUMERICAL INTEGRATION I=(1/3)*A*dx //Value of the


integral
clear;clc; //Simpson's 3/8 Rule
//Declare the limits and the
//Declare the function in the number of sub-intervals
integral a=0; //lower limit
function fx=f(x) b=5; //upper limit
fx=2*exp(-3*x).*cos(x); N=24; //number of sub-intervals
//Function to be integrated (MUST BE MULTIPLES OF 3)
endfunction; dx=(b-a)/N;
A=f(a)+f(b);
//Trapezoidal Integration p=3;q=1;
//Declare the limits and the for k=1:(N-1)
number of sub-intervals A=A+p*f(a+k*dx);
a=0; //lower limit if q==1 then
b=5; //upper limit p=3;q=2;
N=10; //number of sub-intervals elseif q==2 then
dx=(b-a)/N; p=2; q=3;
A=(f(a)+f(b))/2; elseif q==3 then
for k=1:(N-1) p=3;q=1;
A=A+f(a+k*dx); end;
end; end;
I=A*dx //Value of the integral I=(3/8)*A*dx //Value of the
integral
//Simpson's 1/3 Rule
//Declare the limits and the
number of sub-intervals
a=0; //lower limit
b=5; //upper limit
N=10; //number of sub-intervals
(MUST BE EVEN)
dx=(b-a)/N;
A=f(a)+f(b);
p=4;
for k=1:(N-1)
A=A+p*f(a+k*dx);
if p==4 then
p=2;
else
p=4;
end;
EULER's METHOD
clear;clc;
//Declare dx
dx=0.1;
//Declare the given initial
conditions
x=0; ; fx=1; fpx=2;
//Compute the initial value of
the second derivative
fppx=2*fpx+3*fx;
//Initialize the iteration table
with the initial conditions
IT=[x, fx, fpx, fppx];

//Generate values for x>0 using //Generate values for negative x


forward differences and update using backward differences and
the iteration table update the iteration table
for x=dx:dx:2 for x=-dx:-dx:-2
fx=fx+fpx*dx; fx=fx-fpx*dx;
fpx=fpx+fppx*dx; fpx=fpx-fppx*dx;
fppx=2*fpx+3*fx; fppx=2*fpx+3*fx;
IT=[IT;x, fx, fpx, fppx]; IT=[x, fx, fpx, fppx;IT];
end; end;
//Reinitialize the variables with //Plot the graph of f(x) using the
the initial conditions analytical solution and the
x=0; ; fx=1; fpx=2; numerical solution
fppx=2*fpx+3*fx; x=-2:dx:2;
fx=0.25*exp(-x)+0.75*exp(3*x);
clf; plot2d(x,fx);xgrid; //Plot of
exact solution

//Plot the numerical solution


xv=IT(:,1); //Extract values of x
from the first colum of the
iteration table
fxv=IT(:,2); // Extract values of x=0; ; fx=1; fpx=2;
f(x) from the second column of //Compute the initial value of
the iteration table the second derivative
plot2d(xv, fxv); xgrid; //Plot of fppx=2*fpx+3*fx;
numerical solution //Generate values for x<0 and
update the iteration table
for x=-dx:-dx:-2
//Generate values for the
midpoint
fxm=fx-fpx*(dx/2);
fpxm=fpx-fppx*(dx/2);
MODIFIED EULER's METHOD fppxm=2*fpxm+3*fxm;
clear;clc; //Generate values for the
//Declare dx previous point
dx=0.1; fx=fx-fpxm*dx;
//Declare the given initial fpx=fpx-fppxm*dx;
conditions fppx=2*fpx+3*fx;
x=0; ; fx=1; fpx=2; IT=[x, fx, fpx, fppx;IT];
//Compute the initial value of end;
the second derivative IT
fppx=2*fpx+3*fx; //Plot the graph of f(x) using the
//Initialize the iteration table analytical solution and the
with the initial conditions numerical solution
IT=[x, fx, fpx, fppx]; x=-2:dx:2;
fx=0.25*exp(-x)+0.75*exp(3*x);
//Generate values for x>0 and clf; plot2d(x,fx);xgrid; //Plot of
update the iteration table exact solution
for x=dx:dx:2 //Plot the numerical solution
//Generate values for the xv=IT(:,1); //Extract values of x
midpoint from the first colum of the
fxm=fx+fpx*(dx/2); iteration table
fpxm=fpx+fppx*(dx/2); fxv=IT(:,2); // Extract values of
fppxm=2*fpxm+3*fxm; f(x) from the second column of
//Generate value for the the iteration table
next point plot2d(xv, fxv); xgrid; //Plot of
fx=fx+fpxm*dx; numerical solution
fpx=fpx+fppxm*dx;
fppx=2*fpx+3*fx;
IT=[IT;x, fx, fpx, fppx];
end;

//Declare the given initial


conditions
//If the pivot element is zero,
interchange the pivot row with a
row below it
row=prow+1;
while row<=nrow &
abs(AM(prow,prow))<(10^-16)
if AM(row,prow)~=0 then
for col=prow:ncol
temp=AM(prow,col);

AM(prow,col)=AM(row,col);
AM(row,col)=temp;
end;
else
row=row+1;
end;
end;
//Check if the pivot element is
//SOLUTION TO still zero, if it is zero then
SIMULTANEOUS LINEAR terminate the Gaussian-Jordan
EQUATIONS //elimination.
if abs(AM(prow,prow))<(10^-16)
//USING GAUSS-JORDAN then
ELIMINATION disp('No solution');
abort;
clear;clc; end;
disp('GAUSS-JORDAN METHOD') //Normalize the pivot row
//Input the matrix of coefficients pivot=AM(prow,prow); //pivot -
and the matrix of constants pivot element
A=input('Matrix of Coefficients:') for col=prow:ncol
B=input('Matrix of Constants:')
AM(prow,col)=AM(prow,col)/pivot;
//Form the augmented matrix end;
AM=[A B] //AM - Augmented matrix //Make the elements above the
[nrow,ncol]=size(AM);//nrow - no. pivot element zero
of rows, ncol -no. of columns row=1;
if (nrow+1)~=(ncol) then while row<prow
disp('Incorrect number of rows multiplier=AM(row,prow);
and columns in the augmented for col=prow:ncol
matrix'); AM(row,col)=AM(row,col)-
abort; multiplier*AM(prow,col);
end; end;
row=row+1;
//Consider a pivot row starting from end;
the first row until the the last row //Make the elements below the
for prow=1:nrow pivot element be zero
row=prow+1; //Declare the function to be
while row<=nrow optimized
multiplier=AM(row,prow); function fx=f(x)
for col=prow:ncol
fx=20.*x.*exp(-0.2*x);;
AM(row,col)=AM(row,col)-
multiplier*AM(prow,col); endfunction;
end;
row=row+1; //Function for computing the
end; first derivative using central
AM//Display the current difference
augmented matrix function fpx=fp(x)
end; dx=10^(-6);
//Extract and display the matrix of fpx=(f(x+dx)-f(x-dx))/2/dx;
unknowns from the last column of endfunction;
the augmented matrix
disp('Matrix of Unknowns'); //Regula-Falsi Method
X=AM(1:nrow,ncol) //Matrix of disp('REGULA-FALSI METHOD');
unknowns e=10^(-8)
xL=0;
xU=2;
//iterate until an aceptable root
is computed
xr=xL-(fp(xL)*(xL-xU)/(fp(xL)-
REGULA-FALSI fp(xU)));
n=1;//initialize the iteration
METHOD counter n
IT=[n,xr,f(xr),fp(xr)];//initialize
clear;clc the iteration table IT
//PLOTTING THE FUNCTION while (abs(fp(xr))>e)
//Declare the function to be if fp(xL)*fp(xr)<0 then
optimized xU=xr;
function fx=f(x) else
fx=20.*x.*exp(-0.2*x); xL=xr;
endfunction; end;
xr=xL-(fp(xL)*(xL-xU)/(fp(xL)-
//Plot of f(x) over the interval fp(xU)));
0<x<10 n=n+1;
x=0:0.001:10; IT=[IT;n,xr,f(xr),fp(xr)];
clf; plot2d(x,f(x));xgrid(); end;
//display the iteration table and
//REGULA-FALSI METHOD computed root
clear;clc; IT
xr
f(xr) //Golden Search
disp('COMPUTING FOR MINIMUM
POINT USING GOLDEN SEARCH
METHOD');
xL=0;
xU=10;
x1=xL+0.6*(xU-xL);
x2=xU-0.6*(xU-xL);
e=10^-6;

n=1; Error=1;//Dummy data for


the initial estimation error
if f(x1)<f(x2) then
xmin=x1;
fmin=f(x1);
IT=[n, xL, xU, xmin, fmin,
Error];
xL=x2;
else
xmin=x2;
fmin=f(x2);
IT=[n, xL, xU, xmin, fmin,
Error];
xU=x1;
end;

while (Error>e)
n=n+1;
x1=xL+0.6*(xU-xL);
x2=xU-0.6*(xU-xL);
GOLDEN SEARCH if f(x1)<f(x2) then
METHOD (Minimum Error=abs(xmin-x1);
Point) xmin=x1;
//GOLDEN SEARCH METHOD fmin=f(x1);
(Minimum Point) IT=[IT; n, xL, xU, xmin,
clear;clc; fmin, Error];
//Declare the function to be xL=x2;
optimized else
function fx=f(x) Error=abs(xmin-x2);
fx=20-20.*x.*exp(-0.2*x); xmin=x2;
endfunction; fmin=f(x2);
IT=[IT; n, xL, xU, xmin, //Declare the function to be
fmin, Error]; optimized
xU=x1; function fx=f(x)
end; fx=20.*x.*exp(-0.2*x);
end; endfunction;
IT //Golden Search
xmin disp('COMPUTING FOR MAXIMUM
fmin POINT USING GOLDEN SEARCH
METHOD');
xL=0;
xU=10;
e=10^-6
x1=xL+0.6*(xU-xL);
x2=xU-0.6*(xU-xL);

n=1;Error=1; //Dummy data for


the initial estimation error of x
if f(x1)>f(x2) then
xmax=x1;
fmax=f(x1);
IT=[n, xL, xU, xmax, fmax,
Error];
xL=x2;
else
xmax=x2;
fmax=f(x2);
IT=[n, xL, xU, xmax, fmax,
Error];
xU=x1;
end;

//Iterations
Error=1;
while (Error>e)
n=n+1;
GOLDEN SEARCH x1=xL+0.6*(xU-xL);
x2=xU-0.6*(xU-xL);
METHOD (Maximum if f(x1)>f(x2) then
Point) Error=abs(xmax-x1);
//GOLDEN SEARCH METHOD xmax=x1;
(Maximum Point) fmax=f(x1);
clear;clc;
IT=[IT; n, xL, xU, xmax, METHOD OF
fmax, Error];
xL=x2; STEEPEST ASCENT
else //METHOD OF STEEPEST ASCENT
Error=abs(xmax-x2); (Maximum Point)
xmax=x2; clear;clc;
fmax=f(x2);
IT=[IT; n, xL, xU, xmax, //Declare the function to be
fmax, Error]; maximized
xU=x1; function fxy=f(x, y)
end; fxy=-4*x+6*y-4*x^2-9*y^2;
end; endfunction;

IT //Function for partial derivative


xmax wrt x using central difference
fmax function fpx=fpx(x, y, dx)
fpx=(f(x+dx,y)-f(x-
dx,y))/2/dx;
endfunction;

//Function for partial derivative


wrt y using central difference
function fpy=fpy(x, y, dy)
fpy=(f(x,y+dy)-f(x,y-
dy))/2/dy;
endfunction;

//Declare step size and initial


coordinates
dx=10^-8; dy=10^-8;dL=0.001;
x=0; y=0;
//Initialize the iteration table
n=1; fmax=f(x,y);
IT=[n, x,y,fmax];

//Iterate to compute next values


of x and y
Dx=fpx(x,y,dx)/sqrt(fpx(x,y,dx)^
2+fpy(x,y,dy)^2)*dL;
Dy=fpy(x,y,dy)/sqrt(fpx(x,y,dx)^
2+fpy(x,y,dy)^2)*dL;
x=x+Dx;
y=y+Dy;
while (fmax<f(x,y))
n=n+1;
fmax=f(x,y);
IT=[IT; n, x,y,fmax];
METHOD OF
Dx=fpx(x,y,dx)/sqrt(fpx(x,y,dx)^
2+fpy(x,y,dy)^2)*dL; STEEPEST DESCENT
//METHOD OF STEEPEST
Dy=fpy(x,y,dy)/sqrt(fpx(x,y,dx)^ DESCENT
2+fpy(x,y,dy)^2)*dL; clear;clc;
x=x+Dx;
y=y+Dy; //Declare the function to be
end; minimized
//Display the results function fxy=f(x, y)
IT fxy=cos(1+x+2*y);
endfunction;

//Function for partial derivative


wrt x using central difference
function fpx=fpx(x, y, dx)
fpx=(f(x+dx,y)-f(x-
dx,y))/2/dx;
endfunction;

//Function for partial derivative


wrt y using central difference
function fpy=fpy(x, y, dy)
fpy=(f(x,y+dy)-f(x,y-
dy))/2/dy;
endfunction;

//Declare step size and initial


coordinates
dx=10^-6; dy=10^-6;dL=0.01;
x=0; y=0;
//Initialize the iteration table
n=1; fmin=f(x,y);
IT=[n, x,y,fmin];

//Iterate to compute next values


of x and y
Dx=fpx(x,y,dx)/sqrt(fpx(x,y,dx)^
2+fpy(x,y,dy)^2)*dL;
Dy=fpy(x,y,dy)/sqrt(fpx(x,y,dx)^
2+fpy(x,y,dy)^2)*dL;
x=x-Dx;
y=y-Dy;
while (fmin>f(x,y))
n=n+1;
fmin=f(x,y);
IT=[IT; n, x,y,fmin];

Dx=fpx(x,y,dx)/sqrt(fpx(x,y,dx)^
2+fpy(x,y,dy)^2)*dL;

Dy=fpy(x,y,dy)/sqrt(fpx(x,y,dx)^
2+fpy(x,y,dy)^2)*dL;
x=x-Dx;
y=y-Dy;
end;
//Display the results
IT

You might also like