You are on page 1of 9

Question 1

The matlab code for calculating the pressure using the three different methods is
below. Where we can see that the secant method has the fastest convergence rate
compared with the other two methods for the same value of the tolerance error.
Using the secant method. In order to find a numerical value for the initial guesses
we plot the van dar Waals equation for different values of the specific volume v as
shown in figure 1. From this plot we found out the graphical solution of the problem
is around 5

Figure 1

Using bisection method = 4.8181


Using secant method = 4.8181
Using newton Raphson method = 4.8181

For the different the range of pressure values from 5 to 50 atm we got the following
solution using the three different method is shown n figure 2. Where the three
methods converged to the same numerical solution but with the different number of
iteration. The secant method required the lowest number of iteration (converged
faster) compared with the other two methods
The Matlab Code

clear all
close all
clc
global R a b

%% Bisection method
p=5;
T=300;
R=0.082054;
a=3.592;
b=0.04267;
vol=0:0.1:10;
func=R*T-(vol-b).*(p+(a./vol.^2));

plot(vol,func)
grid on
% paremeters for Q1 A

xl=4;
xu=6;
xrBisection=(xl+xu)/2;
for i=1:1000

xrOld=xrBisection;

if f(xl,p,T)*f(xrBisection,p,T)<0
xu=xrBisection;
else
xl=xrBisection;
end

xrBisection=(xl+xu)/2;
error=abs((xrBisection-xrOld)/xrBisection*100);
if error<0.000005
break
end

end

BisectionMethod=xrBisection
i

%% Newton Raphson Method

xrNewton=5;

for j=1:1000
xrold=xrNewton;
xrNewton=xrNewton-f(xrNewton,p,T)/fder(xrNewton,p,T);

error=abs((xrNewton-xrOld)/xrNewton*100);
if error<0.000005
break
end

end

NewtonRaphson=xrNewton

%% Secant Method
x0=3;
xrSecant=5;

for k=1:10000

xrold=xrSecant;
xrSecant=xrold-(f(xrold,p,T)*(x0-xrold))/(f(x0,p,T)-f(xrold,p,T));

error=abs((xrSecant-xrold)/xrSecant*100);
if error<0.000005
break
end

x0=xrold;
end
k
Secant=xrSecant
%% HW2 Q1b
for p=5:50
T=400;
xl=-100;
xu=100;
xrBisection=(xl+xu)/2;
for i=1:1000

xrOld=xrBisection;

if f(xl,p,T)*f(xrBisection,p,T)<0
xu=xrBisection;
else
xl=xrBisection;
end

xrBisection=(xl+xu)/2;
error=abs((xrBisection-xrOld)/xrBisection*100);
if error<0.000005
break
end

end

BisectionMethod(p)=xrBisection;

%% Newton Raphson Method

xrNewton=4.5;

for j=1:1000
xrold=xrNewton;
xrNewton=xrNewton-f(xrNewton,p,T)/fder(xrNewton,p,T);

error=abs((xrNewton-xrOld)/xrNewton*100);
if error<0.000005
break
end

end

NewtonRaphson(p)=xrNewton;

%% Secant Method
x0=3;
xrSecant=5;

for k=1:10000

xrold=xrSecant;
xrSecant=xrold-(f(xrold,p,T)*(x0-xrold))/(f(x0,p,T)-f(xrold,p,T));
error=abs((xrSecant-xrold)/xrSecant*100);
if error<0.000005
break
end

x0=xrold;
end

Secant(p)=xrSecant;
Pressure(p)=p;

end

figure()
plot(Pressure,BisectionMethod,'r^',Pressure,NewtonRaphson,'bo',Pressure,Secant
,'ks')
grid on
xlabel('Pressure(atm)')
ylabel('volume')
grid on

function Value= f(v,p,T);


global R a b
R=0.082054;
a=3.592;
b=0.04267;

Value=R*T-((v-b)*(p+(a./v.^2)));

function DERValue= fder(v,p,T);


global R a b
R=0.082054;
a=3.592;
b=0.04267;

DERValue=-((1-b)*(p+(a./v.^2)))-((v-b)*(p+(-2*a./v.^3)));
Question 2

Using the false position method we developed a program to find the temperature as
function of the Oxygen concentration. Where the error was fixed at 0.000005.

For Oxygen concentration of 8 we found that the corresponding temperature is


299.9301
For Oxygen concentration of 10 we found that the corresponding temperature is
288.5382
For Oxygen concentration of 14 we found that the corresponding temperature is
274.7046

The Matlab Code

clear all
clc
close all
%Question 2
Oconcentration=14;% remember that i have three different test values

Ta=273:325;

funct=-139.344111+1.575701e5./Ta-6.642308e7./Ta.^2+1.243800e10./Ta.^3-
8.621949e11./Ta.^4-log(Oconcentration)

plot(Ta,funct)

xlabel('Temprature (K)')
ylabel('Function')
grid on
Tl=273;
Tu=325;

Tr=Tu-((fun(Tu,Oconcentration)*(Tl-Tu))/(fun(Tl,Oconcentration)-
fun(Tu,Oconcentration)))

for i=1:1000

TrOld=Tr;

if fun(Tl,Oconcentration)*fun(Tr,Oconcentration)<0
Tu=Tr;
else
Tl=Tr;
end

Tr=Tu-((fun(Tu,Oconcentration)*(Tl-Tu))/(fun(Tl,Oconcentration)-
fun(Tu,Oconcentration)))

error=abs((Tr-TrOld)/Tr*100);

if error<0.000005
break
end
end

the Function

function result=fun(Ta,Oconcentration)

result=-139.344111+1.575701e5./Ta-6.642308e7./Ta.^2+1.243800e10./Ta.^3-
8.621949e11./Ta.^4-log(Oconcentration);
Question 3

The lowest positive root of f ( x )=7 sin ( x ) exp (x )1

Using the newton method and using the secant method is 0.1702. but for the given
initial guesses the newton Raphson method converged faster compared with secant
method for an error value of 0.00005

The Matlab Code

% Hw2 question 3
clear all
clc
close all

%% Newton Method
x=-1:0.01:1;
fff=7*sin(x).*exp(-x)-1;
plot(x,fff)
grid on

xrNewton=0.3;

for j=1:1000
xrold=xrNewton;
xrNewton=xrNewton-q3(xrNewton)/q3D(xrNewton);

error=abs((xrNewton-xrold)/xrNewton*100);
if error<0.000005
break
end

end

NewtonRaphson=xrNewton
%% Secant Method
x0=0.5;
xrSecant=0.4;

for k=1:10000

xrold=xrSecant;
xrSecant=xrold-(q3(xrold)*(x0-xrold))/(q3(x0)-q3(xrold));

error=abs((xrSecant-xrold)/xrSecant*100);
if error<0.000005
break
end

x0=xrold;
end

SecantMethod=xrSecant

The function value


function result=q3(x)

result=7*sin(x)*exp(-x)-1;

The derivative of the function

function resultD=q3D(x)

resultD=7*cos(x)*exp(-x)-7*sin(x)*exp(-x);

You might also like