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

1

Group No: 11
Student ID: 0906055


Experiment No: 08



Name of the Experiment:
Solutions to Non-linear Equations




Date of Submission: 22.10.2012 Name: Shahnewaz Karim Sakib
Section: A2
Level/ Term: 2/ 2
Department: EEE
Partners ID: 0906056

2

1
st
Problem:
We were asked to find the root of the following two equations:
(i) f (x)=e
-x
x
2
sin
2
x in the range x=-1 to 1
(ii) f (x)=x
2
lnx cos(x/2) in the range x=0.5 to 2
using the following methods and to provide the plots of iteration vs. absolute error in
each if the cases-
Bisection Method
False Position Method
Newton- Raphson Method
Secant Method

I have used two different user defined functions for the two functions that were given as
well as two different functions to find the derivative of these given functions.
User Defined Function:
1
st
function:
function z=sakib_08(x)

z=(exp(-x)*x^2)-(sin(x))^2;
end

2
nd
function:

function z=sakib_09(x)

z=(x^2*(log(x)))-(cos((pi*x)/ 2));
end

function for the derivative of 1
st
function:

function z=sakib_08_deriv(x)

h=0.001;
p=sakib_08(x+h);
q=sakib_08(x-h);
z=(p-q)/ (2*h);
end


3

function for the derivative of 2
nd
function:

function z=sakib_09_deriv(x)

h=0.001;
p=sakib_09(x+h);
q=sakib_09(x-h);
z=(p-q)/ (2*h);
end

Bisection Method:

Code:
close all;
clear all;
clc;

xlower=-1;
xupper=1;
root=0;
xtol=10^-5;
ylower=sakib_08(xlower);
xmid=(xlower+xupper)/ 2;
ymid=sakib_08(xmid);
i=1;
iter=0;

while((xupper-xlower)/ 2>xtol)
iter1(i)=iter+1;
iter=iter+1;
if(ylower*ymid>0.0)
xlower=xmid;
else
xupper=xmid;
end
xmid=(xlower+xupper)/ 2;
ymid=sakib_08(xmid);
error(i)=abs(root-xmid);
i=i+1;
end

xmid
ymid
plot(iter1,error)
grid on

The output curve is given from iteration no. 1
4

Output Curve:
For 1
st
function:

For 2
nd
function:


False Position Method:

Code:
close all;
clear all;
clc;

xlower=0.5;
xupper=2;
root=1;
yupper=sakib_09(xupper);
ylower=sakib_09(xlower);
xtol=10^-5;
xmid=xupper-((yupper*(xlower-xupper))/ (ylower-yupper));
ymid=sakib_09(xmid);
i=1;
5

iter=0;
error1=root-xmid;

while(abs(error1)>xtol)
iter1(i)=iter+1;
iter=iter+1;
if(ylower*ymid>0.0)
xlower=xmid;
ylower=sakib_09(xlower);
else
xupper=xmid;
yupper=sakib_09(xupper);
end
xmid=xupper-((yupper*(xlower-xupper))/ (ylower-yupper));
ymid=sakib_09(xmid);
error(i)=abs(root-xmid);
error1=root-xmid;
i=i+1;
end

xmid
ymid
plot(iter1,error)
grid on

Output Curve:
For 1
st
function:








6

For 2
nd
function:

Newton- Raphson Method:

Code:
close all;
clear all;
clc;

xlower=2;
root=1;
xtol=10^-5;
ylower=sakib_09(xlower);
iter=1;
dx=-ylower/ sakib_09_deriv(xlower);
sol=xlower+dx;
i=1;
while(abs(dx)>xtol)
iter1(i)=iter;
dx=-sakib_09(sol)/ sakib_09_deriv(sol);
sol=sol+dx;
iter=iter+1;
error(i)=abs(root-sol);
i=i+1;
end

sol
plot(iter1,error)
grid on



7

Output Curve:
For 1
st
function:

For 2
nd
function:


Secant Method:

Code:
close all;
clear all;
clc;

xlower=0.5;
xupper=2;
iter=1;
i=1;
xtol=10^-5;
yupper=sakib_09(xupper);
ylower=sakib_09(xlower);
root=1;
sol=((xlower*yupper)-(xupper*ylower))/ (yupper-ylower);
8

ykplus1=sakib_09(sol);
while(abs(sol-xupper)>xtol)
iter1(i)=iter;
xlower=xupper;
ylower=yupper;
xupper=sol;
yupper=ykplus1;
sol=((xlower*yupper)-(xupper*ylower))/ (yupper-ylower);
ykplus1=sakib_09(sol);
iter=iter+1;
error(i)=abs(root-sol);
i=i+1;
end

sol
plot(iter1,error)
grid on


Output Curve:
For 1
st
function:




9

For 2
nd
function:



2
nd
Problem:
Here, Vp =5 Volt
Frequency, f =50 Hz
Resistance, R=100
Is =10
-15
A
VT =25.210
-3
V
n=1
And the equation for the current across the diode is, ID =Is (e
Vd/ nVt
-1)
The problem asks us to plot the voltage across the resistor as a function of time from
t=0 second to t=0.1 second.
We know, the built in voltage for a diode is 0.7 volt and the diode will not conduct when
the applied voltage is less than 0.7 volt and when it is in reverse biased.
Two user defined function was used to solve this problem. They are given below:




10

1
st
Function:
function z=prob_02(x,y)
Is=10^-15;
n=1;
r=100;
Vt=25.2*10^-3;
p=log(((x/ r)+Is)/ Is);
z=x-y+(n*Vt*p);
end

2
nd
Function:
function z=prob_02_deriv(x)
Is=10^-15;
n=1;
r=100;
Vt=25.2*10^-3;
z=1+((n*Vt)/ (x+(r*Is)));
end

Main Code:
close all;
clear all;
clc;

x0=5;
xtol=10^-5;
r=100;
vp=5;
f=50;
T=1/ f;
Is=10^-15;
n=1;
Vt=25.2*10^-3;
h=0.5/ 1000;
i=1;
k=0;

for t=0:h:0.1
time(i)=t;
v1(i)=vp*(sin(2*f*pi*(t-(k*0.02))));
if(v1(i)>=0.7)
dx=-prob_02(x0,v1(i))/ prob_02_deriv(x0);
root=x0+dx;
while(abs(dx)>xtol)
dx=-prob_02(root,v1(i))/ prob_02_deriv(root);
root=root+dx;
end
else
11

root=0;
end
if (mod(t,0.02)==0 && t~=0)
k=k+1;
end
x(i)=root;
i=i+1;
x0=root;
end
plot(time,x)
hold on
plot(time,v1,'r')

Output Curve:

Where red line indicates the applied voltage and the blue line indicates the voltage
across the resistor.

You might also like