Numerical Analysis Assignment

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

HITEC University Taxila

Department of Electrical Engineering


Subject: Numerical Analysis (MATLAB)
Assignment #01
Submitted To: Mr Amjad Hussain
Submitted By: Nisar Ahmed Rana
07-EE-HITEC-94

Bisection Method
function sol=bisect(fn,a,b,tol) Command Window Operation
fa=feval(fn,a); syms x
fb=feval(fn,b); >> f=inline(x-sin(x^0.5))
if fa*fb>0; f = Inline function:
fprintf('Endpoints have same sign') f(x) = x-sin(x.^(1./2))
return >> bisect(f,3*pi,-4*pi,0.001)
else f_solution = -3.8347e-004
while abs (b - a)>tol ans = 0.7680
c=(a+b)/2;
fc=feval(fn,c); syms x
if fa*fc < .000000000000001; f= inline(x^2-exp(x))
b=c; f = Inline function:
else a = c; f(x) = x.^2-exp(x)
end >> bisect(f,-1,0,0.001)
end f_solution = 0.0012
end ans = -0.7041
sol=c;
f_solution=feval(fn,sol)

Regula Falsi Method


function b=c;
sol=falseP(fn,a,b,tol,MaxI) fb=fc;
x0=a; else
x1=b; a=c;
fa=feval(fn,x0); fa=fc;
fb=feval(fn,x1); end
if (fa*fb > 0) end
fprintf('Endpoints have same solution=feval(fn,c)
sign')
return
end
for i=1:MaxI
c=(fb*a-fa*b)/(fb-fa)
fc=feval(fn,c);
if(abs(fc) < tol)
break
end
if (fa*fc < 0)
Command Window Operation syms x
syms x >> f=inline(sin(x)-x*exp(x))
f= inline(x^2-exp(x)) f = Inline function:
f = Inline function: f(x) = sin(x)-x.*exp(x)
f(x) = x.^2-exp(x) >>falseP(f,3*pi,-4*pi,0.001,1)
>> falseP(f,-1,0,0.001,1) c = -12.5664
c = -0.6127 solution = 4.3831e-005
solution = -0.1665
Secant Method
function sol=secant(fn,a,b,tol) Command Window Operation
x0=a; syms x
x1=b; >> f=inline(sin(x)-x*exp(x))
fa=feval(fn,x0);fb=feval(fn,x1); f = Inline function:
while abs(x1-x0)>tol f(x) = sin(x)-x.*exp(x)
old=x1-fb*(x1-x0)/(fb-fa); secant(f,3*pi,-4*pi,0.001)
x0=x1; solution = -12.5664
fa=fb;
x1=old;
fb=feval(fn,old);
end
solution=old
Newton Method
function sol=newton(fn,dfn,x0,tol)
old=x0+1;
while abs(x0-old)>tol
old=x0;
x0=old-feval(fn,old)/feval(dfn,old);
end
sol=x0
Command Window Operation
syms x
>> f=inline(x^2-exp(x))
f = Inline function:
f(x) = x.^2-exp(x)
>> df=inline(2*x-exp(x))
df = Inline function:
df(x) = 2.*x-exp(x)
>> newton(f,df,1.5,0.001)
sol = -0.7035
ans = -0.7035

You might also like