Professional Documents
Culture Documents
AIMS: 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
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, or in other words, f(x) changes sign between x0 and x1
Step – 2
• Estimate the new value of the root, xi+1
xi+1=xi--f(xi)(xi-xi-1)/f(xi)-f(xi-1)
Step – 3
• Find the absolute relative approximate error as
|€a| = | xi+1 – x1/xi+1 | * 100
• Compare the absolute relative approximate error I€aı with the pre-specified relative
error tolerance €s If |€a | > €s then go to Step 2, else stop the algorithm.
CODE
clc
clear all disp(‘Hridya Ashokan’)
disp('19BCM0009')
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');
CHE3001- Computational Methods in Process Engineering
2. Secant method:
disp(‘Hridya Ashokan’)
disp('19BCM0009')
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');
Input:
Secant methods:
CHE3001- Computational Methods in Process Engineering
CHE3001- Computational Methods in Process Engineering
OUTPUT
Secant method
CHE3001- Computational Methods in Process Engineering
CONCLUSION
As compared to secant method, newton method is faster as it requires lesser number of iterations.
Knowledge of derivative is required for newton method. Two initial guesses are required for secant
method.