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

Numerical Computation Portfolio: Bisection Method Objective: Bisection Method

1) The document describes several numerical methods for approximating solutions to equations including the bisection method, Newton's method, secant method, and spline interpolation. 2) It provides example code implementations of each method and discusses how they work such as using successive approximations to narrow the range containing a root. 3) The document also covers other numerical techniques like LU factorization, Gauss-Seidel iteration, and Lagrange interpolation for solving systems of equations or interpolating values.

Uploaded by

Alee Sajjad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Numerical Computation Portfolio: Bisection Method Objective: Bisection Method

1) The document describes several numerical methods for approximating solutions to equations including the bisection method, Newton's method, secant method, and spline interpolation. 2) It provides example code implementations of each method and discusses how they work such as using successive approximations to narrow the range containing a root. 3) The document also covers other numerical techniques like LU factorization, Gauss-Seidel iteration, and Lagrange interpolation for solving systems of equations or interpolating values.

Uploaded by

Alee Sajjad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Numerical Computation Portfolio

Chapter 1

Bisection Method

Objective:
To approximate values the any function using Bisection Method.

Bisection Method:
This is the first technique based on the intermediate value theorem called the bisection
method or Binary search method .

Program of Bisection Method

function bi=bisection(f,a,b,N,tol)

i=0;

format long

t=-10;

while t<=10

h=f(t);

while h*(f(t+1))<=0

c=[t t+1];

display (c)

a=t;

b=t+1;

break

end

t=t+1;

end
FA=f(a);

fprintf('%15s%15s%15s%15s\n','itterations ','an','ab','pn')

while i<=N

p=(a+b)/2;

FP=f(p);

fprintf('%15f%15f%15f%15f\n',i,a,b,p)

if FP==0||(b-a)/2<tol

display (p)

return

end

i=i+1;

if FA*FP>0

a=p;

FA=FP;

else

b=p;

end

end

Extreme Value Problem

Objective:
To find the maximum or minimum value of the function by Extreme value theorem.

Extreme value theorem:


If F is lies in c(a,b) then c1 and c2 also lies in [a,b] exist with F(c1) <F(c2), for all x lies in
[a,b]. in addition if f is differentiable on (a,b) then the no c1 and c2 either exist at the end points or
where f’ is zero .
Extreme Value theorem Program

function k=extreme(f,a,b,tol)

m=0;

ans1=f(a)

ans2=f(b)

syms x

g=diff(f(x));

h=inline(g,'x')

tol=tol;

y1=h(a);

y2=h(b);

i = 0;

if y1 * y2 >= 0

disp('Didn’t not find a change in sign. Will not continue...');

else

m=newton(h,a,b,tol)

w = f(m);

end

if abs(f(a))>abs(f(b))&&abs(f(a))>abs(f(m))

end

if abs(f(b))>abs(f(a))&&abs(f(b))>abs(f(m))

end
if abs(f(m))>abs(f(b))&&abs(f(m))>abs(f(a))

else

if abs(f(a))>abs(f(b))

else

end

end

end

False Position Method

Objective:
To approximate the value of the any function using False position method.

False position Method:


False position method and regular false method are two early, and still current,
names for a very old method for solving an equation with one unknown.

False position Method Program:

x0=input('starting guess point 1:');

x1=input('starting guess point 2;');

x2=x0;

tol=1-e-8;

iters=0;
while ((iters<30) & (abs(func(x2))>tol))|(iters==0)

iterations = iterations + 1 ;

f0 = func(x0);

f1 = func(x1);

x2 = x0-f0*(x1-x0)/(f1-f0);

if func(x2)*f0 < 0

x1 = x2;

else

x0 = x2;

end

end

if iters==30

disp('No root found')

else

disp([' Root = ' num2str(x2,10) ' found in '...

num2str(iters) ' iters.'])

end

end

Newton’s Method

Objective:
To approximate the value of the any function by Newton’s Method.

Newton’s Method:
In numerical analysis, Newton's method (also known as the Newton–Raphson method), is
a method for finding successively better approximations to the roots (or zeroes) of a real-valued
function.
Newton’s Method Program

syms x

e=input('enter the function interms of x=','s');

f=inline(e,'x');

d=diff(f(x));

fo=inline(d);

i=0;

tol=input('enter the tollerence');

format long

t=-10;

while t<=10

h=f(t);

while h*(f(t+1))<=0

c=[t t+1];

display (c)

p=t;

po=t+1;

break

end

t=t+1;

end

fprintf('%15s%15s\n','itteration','pn')
while i<=N

p=po-(f(po)/fo(po));

fprintf('%15f%15f\n',i,p)

if abs(p-po)<tol

display(p)

return

end

i=i+1;

po=p;

end

Secant Method

Objective :
To approximate the value of any function by using secant method.

Secant Method:
In numerical analysis, the secant method is a root-finding algorithm that uses a succession of roots
of secant lines to better approximate a root of a function f. The secant method can be thought of as a
finite difference approximation of Newton's method.

Secant Method Program :

function S=secant(f,a,b,n,tol)

i=2;

while i<=n

p=b-((f(a)*(b-a))/(f(b)-f(a)));

if (p-b)<tol

fprintf('the ans is %d after %d iterations',p,i);


return

end

i=i+1;

b=p;

a=b;

end

end

Chapter 2

Gauss Elimination Method

Objective :
Find the value of the function using Gauss elimination method (backward substitution).

Gauss Elimination Method:


In linear algebra, Gaussian elimination (also known as row reduction) is an algorithm for solving
systems of linear equations. It is usually understood as a sequence of operations performed on the
corresponding matrix of coefficients

Gauss Elimination Program :

function bckwrd=backward(a)

[m,n]=size(a);

for j=1:1:m-1

if a(j,j)==0

for k=j+1:1:m

if a(k,j)~=0

temp=a(j,:)
a(j,:)=a(k,:)

a(k,:)=temp

end

end

end

for i=j+1:1:m

a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j))

end

g=[];

k=1;

for i=1:1:m

for k=1:1:n-1

g(i,k)=a(i,k);

end

end

c=n;

b=[];

for j=1:1:m

b(j,1)=a(j,c);

end

Jacobi Iterative method

Objective:
To find the values of the Function using Jacobi Method.
Jacobi Method :
In numerical linear algebra, the Jacobi method is an algorithm for determining the solutions of
a diagonally dominant system of linear equations. Each diagonal element is solved for, and an
approximate value is plugged in. The process is then iterated until it converges. This algorithm is a stripped-
down version of the Jacobi transformation method of matrix diagonalization. The method is named
after Carl Gustav Jacob Jacobi.

Jacobi Method
function j=jacobi(c,d,x,x0,tol)

[m,n]=size(c);

k=1;

t=zeros(1,0);

N=10;

while k<=N

for i=1:1:m

sum=0;

for j=1:1:m

if (j~=i)

sum=sum+c(i,j)*x0(j);

end

end

x(i)=d(i)-sum/c(i,i);

end

for i=1:1:m

t(i)=x(i)-x0(i);

end

q=max(t);
if q<tol

k=27;

end

k=k+1;

x0=x;

end

x0

LU Factorization

Objective :
Find the values of the given functions using Lu factorization method .

LU Factorization:
In numerical analysis and linear algebra LU decomposition (where 'LU' stands for 'lower upper',
and also called LU factorization) factors a matrix as the product of a lower triangular matrix and an
upper triangular matrix. The product sometimes includes a permutation matrix as well. The LU
decomposition can be viewed as the matrix form of Gaussian elimination.

LU Factorization Program.

function l=lu(a,b)

l = eye(4);

[m,n]=size(a)

for j=1:1:m-1;

if a(j,j)==0
for k=j+1:1:m

if a(k,j)~=0

t=a(j,:);

a(j,:)=a(k,:);

a(k,:)=t;

break

end

end

end

for i=j+1:1:m

l(i,j)=(a(i,j)/a(j,j));

a(i,:)=a(i,:)-(a(i,j)/a(j,j))*a(j,:);

end

end

U=a

y=zeros(1,m)

for s=1:1:m

e=0;

for k=1:m

e=e+l(s,k)*y(k);

end

y(s)=(b(s)-e)/l(s,s)

display('lu factorization')
end

y'

x=zeros(1,m)

for s=m:-1:1

c=0;

for k=m:1

c=c+a(s,k)*x(k);

end

x(s)=(y(s)-c)/a(s,s)

display('lu factorization')

end

x'

Gauss Seidel Method

Objective:
To find the value of the given function by Gauss seidle method.

Gauss Siedel Method:


In numerical linear algebra, the Gauss–Seidel method, also known as the
Liebmann method or the method of successive displacement, is an iterative method used to
solve a linear system of equations.It was only mentioned in a private letter from Gauss to his
student Gerling in 1823.

Gauss Siedel Program


function j=seidle(A,B,x,x0,tol)

[m,n]=size(A);

k=1;
t=zeros(1,0);

N=10;

while k<=N

for i=1:1:m

sum=0;

for j=1:1:m

if (j~=i)

sum=sum+A(i,j)*x0(j);

end

end

x(i)=B(i)-sum/A(i,i);

end

for i=1:1:m

t(i)=x(i)-x0(i);

end

q=max(t);

if q<tol

X0=x;

end

k=k+1;

end

q
x

Chapter 3
Newton Divided Difference Method

Objective:
To find the value of the function by Newton Divided difference Method.

Newton Divided Difference Method:


In the mathematical field of numerical analysis, a Newton polynomial, named after its inventor
Isaac Newton, is the interpolation polynomial for a given set of data points in the Newton form.

Newton Divided Difference Method Program.

function M=newtondivideddifference (x,y,X)

for i=1:1:5

for j=2:i

y(i,j)=(y(i,j-1)-y(i-1,j-1))/(x(i)-x(i+1-j))

end

end

syms C;

D=0;

for i=2:1:5

C=1;

for j=1:1:i-1

C=C*(X-x(j));

end

D=D+y(i,i)*C;
end

p=y(1,1)+D

Langrange Method

Objective:
To find the value of the function Using Langrange method.

LANGRANGE METHOD:
In mathematical optimization, the method of Lagrange multipliers (named after Joseph
Louis Lagrange) is a strategy for finding the local maxima and minima of a function subject to
equality constraints

LANGRANGE METHOD Program.

function [ P ] = lagrange( x,pointx,pointy )

n=length(pointx);

L=ones(n,1);

for i=1:n

for j=1:n

if(i~=j)

L(i,1)= L(i,1).*(x-pointx(j))/(pointx(i)-pointx(j));

end

end

end

P=0;

for i=1:n

P=P+(pointy(i).*L(i,1));

end
P

End

Spline Method

Objective:
To find values of equations using Spline Method.

Spline:
This method involves solution and interpolation of equations especially on curved shapes.

Program:
function s=spline(x,y)

[m,n]=size(x)

h=zeros(m-1,1)

a=zeros(m,1)

for i=1:1:m-1

h(i)=(x(i+1)-x(i));

end

for i=1:1:m

a(i)=y(i);

end

a=zeros(m,m)

A(1,1)=1;

A(m,m)=1;

for i=2:1:m-1

A(1,1)=2*(h(i-1)+h(i));

A(i,i-1)=h(i-1);

A(i,i+1)=h(i);
end

b=zeros(m,1)

for i=2:1:m-1

b(i)=3*((a(i+1)-a(i))/h(i)-(a(i)-a(i-1))/h(i-1));

end

c=zeros(m,1)

c=LU(A,b)

B=zeros(m-1,1)

for i=1:1:m-1

B(i)=((a(i+1)-a(i))/j(i)-(h(i)*(c(i+1)+2*c(i))/(3)))

end

d=zeros(m-1,1)

for i=1:1:m-1

d(i)=(c(i-1)-c(i))/3*h(i)

end

Chapter 4
Numerical Differentiation Method

Objective:
Interpolate the function using Deffrentiation Method.

Deffrentiation Method :
the primary motivation for the study of differentiation was the tangent line problem: for a given
curve, find the slope of the straight line that is tangent to the curve at a given point. The
word tangent comes from the Latin word tangens, which means touching. Thus, to solve the tangent
line problem, we need to find the slope of a line that is "touching" a given curve at a given point, or,
in modern language, that has the same slope. But what exactly do we mean by "slope" for a curve.

Deffrentiation Method Program .

function G=differentiationmethod(X,f,h,a)

[m,n]=size(X);

for i=1:1:m

if X(i)==a

t=i;

end

end

if m<5

if t==1

display('3 point forward formula');

y=(0.5/h)*(-3*f(t)+4*f(t+1)-f(t+2));

end

if t==m

display('3 point backward formula');

y=(0.5/-h)*(-3*f(t)+4*f(t-1)-f(t-2));

end

if (1<t)&&(t<m)

display('3 point midpoint formula');

y=(0.5/h)*(f(t+1)-f(t-1));

end

y
end

if m>5

if t<=2

display('5 point forward formula');

y=(1/(12*h))*((-25*f(t)+48*f(t+1)-36*f(t+2)+16*f(t+3)-3*f(t+4)));

end

if t>=(m-1)

display('5 point Backward formula')

y=(1/(-12*h))*((-25*f(t)+48*f(t-1)-36*f(t-2)+16*f(t-3)-3*f(t-4)));

end

if(2<t)&&(t<m-1)

display('5 point midpoint formula')

y=(1/(12*h))*(f(t-2)-8*f(t-1)+8*f(t+1)-f(t+2)) ;

end

display(y);

end

if m==5

if t==1

display('5 point forward formula');

y=(1/(12*h))*((-25*f(t)+48*f(t+1)-36*f(t+2)+16*f(t+3)-3*f(t+4)));

end

if t==5

display('5 point backward formula');

y=(1/(-12*h))*((-25*f(t)+48*f(t-1)-36*f(t-2)+16*f(t-3)-3*f(t-4)));
end

if (1<t)&&(t<5)

display('3 point midpoint formula');

y=(0.5/h)*(f(t+1)-f(t-1));

end

if t==3

display('5 point midpoint formula');

y=(1/(12*h))*(f(t-2)-8*f(t-1)+8*f(t+1)-f(t+2)) ;

end

end

end

Simpson Rule

Objective:
Find the value of function by using Simpson Rule.

Simpson Rule:
Simpson's rule is a method of numerical integration that provides an approximation of a definite integral
over the interval [a,b] using parabolas. Generally, the function f(x) over interval [a,b] can be approximated

as .
subinterval

Simpson Method Program.

function M=simpson(f,a,c,b)

h=(a-b)/2;

g=h*(f(a)+4f(c)+f(b));

end

Trapezoidal Method

Objective:
Find the value of function by usning Trapeziodal Rule.

Trapezoidal Method:
In mathematics, and more specifically in numerical analysis the trapezoidal rule (also known as
the trapezoid rule or trapezium rule) is a technique for approximating the definite integral
The trapezoidal rule works by approximating the region under the graph of the function as
a trapezoid and calculating its area.

Trapezoidal Method Program.

function S=trapeziodal(f,a,b)

h=a-b;

g=h/2*(f(a)+f(b));
end

chapter 5
Midpoint Euler Method
function M= Eu(f,a,b,Alpha,h,ex)

f=@(t,y)y-t^2+1

a=0

b=2

h=0.25

N=(b-a)/h

M=zeros(N+1,4)

Alpha=0.5

t(1)=0

for i=1:1:N

t(i+1)=a+(i*h)

end

for i=1:1:N+1

M(i)=t(i)

end

w0=.5

w(1)=w0

for i=1:1:N

w(i+1)=w(i)+h*f(t(i)+(h/2),w(i)+(h/2)*f(t(i),w(i)))

end

M(1,2)=w(1)
M(2,2)=w(2)

for i=3:1:N+1

M(i,2)=w(i)

end

v=@(t) (t+1)^2-(0.5)*exp(t)

for i=1:1:N+1

s(i)=v(t(i))

end

for i=1:1:N+1

M(i,3)=s(i)

end

for i=1:1:N+1

o(i)=abs(s(i)-w(i))/abs(s(i))

end

for i=1:1:N+1

M(i,4)=o(i)

end

Modified Euler Method

Objective:

Write a program using Euler Modified Method for initial value problem.

Modified Euler Method :


The Modified Euler method(also called forward Euler method) is a first-order numerical
procedure for solving ordinary differential equations (ODEs) with a given initial value by
modifying Basic Euler Method.

Modified Euler Method Program:

function M= Eu(f,a,b,Alpha,h,ex)

f=@(t,y)y-t^2+1

a=0

b=2

h=0.25

N=(b-a)/h

M=zeros(N+1,4)

Alpha=0.5

t(1)=0

for i=1:1:N

t(i+1)=a+(i*h)

end

for i=1:1:N+1

M(i)=t(i)

end

w0=.5

w(1)=w0

for i=1:1:N

w(i+1)=w(i)+(f(t(i),w(i))+f(t(i+1),w(i)+h*f(t(i),w(i))))

end

M(1,2)=w(1)
M(2,2)=w(2)

for i=3:1:N+1

M(i,2)=w(i)

end

v=@(t) (t+1)^2-(0.5)*exp(t)

for i=1:1:N+1

s(i)=v(t(i))

end

for i=1:1:N+1

M(i,3)=s(i)

end

for i=1:1:N+1

o(i)=abs(s(i)-w(i))/abs(s(i))

end

for i=1:1:N+1

M(i,4)=o(i)

end

Runge Kutta Method

Objective:
Write a MATLAB Program of Range katta Method .

Runge kutta Method :


In numerical analysis, the Runge–Kutta methods are a family of implicit and explicit
iterative methods, which includes the well-known routine called the EulerMethod, used in
temporal discretization for the approximate solutions of ordinary differential equations.
Runge kutta Method Program

function N=rungekutta(f,a,b,h,alpha)

format long

N=(b-a)/h

t=zeros(1,N+1)

t(1)=a

alpha=1

k=zeros(N,1)

for i=2:1:N+1

t(i)=a+((i-1)*h)

end

w1=alpha

for i=1:1:N

k(1)=h*f(t(i),w(i))

k(2)=h*f(t(i)+(h/2),w(i)+(k(1)/2))

k(3)=h*f(t(i)+(h/2),w(i)+(k(2)/2))

k(4)=h*f(t(i+1),w(i)+k(3))

w(i+1)=w(i)+(k(1)+2*k(2)+2*k(3)+k(4))/6

end

Chapter 6
Finite Difference Method

Objective:
To solve the boundry value problem write a MATLAB program.
Finite Difference Method :
In mathematics, finite-difference methods (FDM) are numerical methods for solving
differential equations by approximating them with difference equations, in which finite
differences approximate the derivatives. FDMs are thus discretization methods.

Finite Difference Method Program.

function fdm

a=1

b=2

N=4

h=(b-a)/(N+1)

y1=1

alpha=y1

y2=2

beta=y2

x0=a

for i=1:1:N+1

x(i)=a+(i*h)

end

for i=1:1:N

p(i)=(-2)/x(i)

q(i)=2/x(i)^2

r(i)=sin(log(x(i)))/x(i)^2

end

M=zeros(N,N)

for i=1:1:N
M(i,i)=2+(h^2)*(q(i))

end

for i=1:1:N-1

M(i,i+1)=-1+(h/2)*(p(i))

end

for i=2:1:N

M(i,i-1)=-1-(h/2)*(p(i))

end

n=zeros(N,1)

n(1)=(-h^2*r(1))+(alpha*(1+(h/2)*(p(1))))

n(4)=(-h^2*r(4))+(beta*(1-(h/2)*(p(4))))

for i=2:1:3

n(i)=(-h^2)*(r(i))

end

You might also like