You are on page 1of 8

Name: Adithyadev Malikayil

Reg No: 19BCM0006

Computational Methods in Process Engineering – (CHE 3001)


LAB EXPERIMENT 2

AIM
To develop a Matlab code for Newton Raphson and Secant method for the given problem.
PROBLEM
To determine the molar volume of carbon dioxide gas (1 mole) at 373 K and 50 atm,
develop a Matlab code for the Newton Raphson and secant method. The molar volume
of the gas is given by the Van der Waals equation • Data: Van der Waals constants for
carbon dioxide: a = 3.61 L2atm/mol2 ; b = 0.0428 L/mol. Universal gas constant, R =
0.080206 L.atm/mol.K. Stopping criteria, εs = 0.00001%.
THEORY
1. Newton Raphson method:
• Function f(x) , f(x)=0
• One initial guess is required I.e., xo. xo is some point nearer to the root.
• Requires the calculation of the function's derivative
• The formula for estimation of root is given as xi+1 = xi – f(xi)/f’(xi)
2. Secant Method:
• Improved form of regular falsi method.
• Function f(x) is f(x)=0
• Two initial guesses are required x0,x1
• The formula for estimation of root is given as xi+1=xi--f(xi)(xi-xi-1)/f(xi)-f(xi-1)
such that x0 and x1 are two guesses for the root such that f(x0)f(x1)< 0, i.e., f (x)
changes sign between x0 and x1
ALGORITHM

1. Newton Raphson Method


Step – 1
• Evaluate f ‘(x)
symbolicallyStep – 2
• Estimate the new value of the root, xi+1 . Use initial guess (xi ) for NR Method
xi+1 = xi –f(xi)/f’(xi)
Step – 3
• Find the absolute relative approximate
error asI€aı = |xi+1 – x1/xi+1 | * 100
• Compare the absolute relative approximate error I€aı with the pre-specified
relative errortolerance €s
• If I€aı > €s then go to Step 2, else stop the algorithm.
2. Secant method
Step – 1
• Use initial guesses. Choose x0 and x1 as two guesses for the root such that
f(x0)f(x1)< 0, orin other words, f(x) changes sign between x0 and x1
Step – 2
• Estimate the new value of the
root, xi+1xi+1=xi--f(xi)(xi-xi-1)/f(xi)-
f(xi-1)
Step – 3
• Find the absolute relative approximate
error asI€aı =ı xi+1 – x1/xi+1 ı * 100
• Compare the absolute relative approximate error I€aı with the pre-specified
relative errortolerance €s
If I€aı > €s then go to Step 2, else stop the algorithm.

CODE

1. Newton Raphson Method:

clc
clear all
disp('Adithyadev M')
disp('19BCM0006')
syms v
p=input('Enter pressure = ');
T=input('Enter temperature = ');
a=input('Enter of value of constant a = ');
b=input('Enter value of constant b = :');
R=input('Enter value of R = :');
%taking n=1
f(v)= (p*v)-(p*b)+(a/v)-(a*b/v^2)-(R*T);
fd=diff(f);
tol=input('Enter tolerance limit:');
x=input('Enter initial guess:');
for i=1:500
r=f(x(i))/fd(x(i));
x(i+1)=x(i)-r;
error=abs(((x(i+1)-x(i))/x(i+1)))*100;
q(i)=i;
s(i)=error; if error<=tol break;
end
end
fprintf('The root is:%f\n',x);
fprintf(' Iteration:%f',i);
plot(q,s);
title('Convergence');
xlabel('Iteration number');
ylabel('Absolute error');
CODE:
2. Secant method:

disp('Adithyadev M')
disp('19BCM0006')
syms v
P=input('Enter pressure = ');
T=input('Enter temperature = ');
a=input('Enter of value of constant a = ');
b=input('Enter value of constant b = :');
R=input('Enter value of R = :');
%taking n=1
f(v)= (P*v)-(P*b)+(a/v)-(a*b/v^2)-(R*T);
x0=input('Enter the first guess:');
x1=input('Enter the second guess:');
tol=input('Enter the tolerance:');
for i=1:100
r=(f(x1)*(x1-x0))/(f(x1)-f(x0));
x2=x1-r;
err=abs(r/x2)*100;
q(i)=i;
s(i)=err;
if err<tol break;
else
x0=x1; x1=x2;
end
end
fprintf('The root is:%f\n',x2);
fprintf('Iteration:%d',i);
plot(q,s);
title('Secant Method');
xlabel('Iteration number');
ylabel('Absolute error');
CODE:

OUTPUT:
1. Newton Raphson Method
2. Newton Raphson Method
Conclusion

As compared to secant newton is faster. Knowledge of derivative is required for


newtonmethod. Two initial guesses are required for secant method.

You might also like