Clear CLC: Numerical Integration
Clear CLC: Numerical Integration
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;
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);
//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;
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