Lab 2 Edt FRRM Sahil
Lab 2 Edt FRRM Sahil
fn(x) = 0
where, as before, x = (x1, x2, …, xn)T is the vector of unknowns we want to find. If it is possible
to solve a set of nonlinear equations analytically, you should do that. If not, then you have to
resort to numerical methods.
In this lab we will learn to solve the non-linear algebraic equations using the inbuilt MATLAB
functions/solvers mainly roots and fzero.
roots: Find the zeros of a polynomial equation, c1xn + c2xn–1 + … + cnx + cn+1 = 0
fzero: Find a zero of a continuous nonlinear function of one variable, f(x) = 0
fsolve: Find a zero of a set of nonlinear functions, f(x) = 0
We will also learn about:
Structuring a calculation that involves passing parameters between functions
Performing flash calculations for 2 phase systems
f n ( x) a1 x n a 2 x n 1 ... a n 1 x 2 a n x a n 1
where n is the order of polynomial and a’s are the constants. Polynomials in engineering are
extensively used in curve fitting.
p = [1 7 0 -5 9];
Page 1
CE1532 Process Modeling and Simulation Lab
The polyval function is used for evaluating a polynomial at a specified value. For example,
evaluate the above polynomial at x = 5 using the following commands:
p =[1 7 0 -5 9];
%given polynomial equation
polyval(p, 5)
Task 1:
Use the polyval command to solve the following equation:
x 5 3.5 x 4 2.75 x 3 2.125 x 2 3.875 x 125
Evaluate the polynomial at three real roots: 0.5, -1.0, 2 and one pair of complex roots: 1±0.5i.
p = [1 -3.5 2.75 2.125 -3.875 125]
x1=polyval(p, 0.5)
x1=polyval(p, -1)
x1=polyval(p, 2)
x1=polyval(p, 1+0.5i)
x1=polyval(p, 1-0.5i)
Output:
To find the roots of a polynomial roots function can be used. For example, to find the roots of
the above polynomial type the following command:
r = roots (p)
Task 2:
Write a short Matlab script file (poly.m) to find and report the solutions of the following
equations:
f2(x) = x3 – 2x + 3 = 0 (2)
Page 2
CE1532 Process Modeling and Simulation Lab
p1=[1 -3 -2 5 2];
p2=[1 0 -1 3];
r1=roots(p1)
r2=roots(p2)
x=-4:0.1:4;
f1=polyval(p1,x);
f2=polyval(p2,x);
plot(x,f1,'--',x,f2)
xlabel(‘ X axis’)
ylabel(‘Y axis’)
title(‘Graph b/w p1 and p2’)
xlim([-3 4])
ylim([-20 40])
grid on
legend('x','function value from -4 to 4')
Output:
>> poly
r1 =
3.0472
1.4919
-1.1598
-0.3793
r2 =
-1.6717 + 0.0000i
0.8358 + 1.0469i
0.8358 - 1.0469i
Graph:
Page 3
CE1532 Process Modeling and Simulation Lab
The polyfit function finds the coefficients of a polynomial that fits a set of data in a least
square sense. If x and y are the two vectors containing x-axis and y-axis data, respectively to
be fit to nth degree polynomial, then the following command:
p = polyfit (x, y, n)
Page 4
CE1532 Process Modeling and Simulation Lab
The complete syntax of fzero is obtained as: [x, fx] = fzero (function, x0,
options, p1, p2….)
Options is the data structure created by optimset function, and p1, p2,…are the
parameters that the function requires. A complete listing of all the possible parameters can be
obtained by merely entering the optimset command. Commonly used parameters are:
display: when set to ‘iter’ displays the complete set of iterations.
Try these simple MATLAB commands:
For the simple quadratic equation: x2-9, the roots are: +3 and -3.
(i) To find the positive root: x = fzero (@(x) x^2-9, 4)
(ii) To find the negative root: x = fzero (@(x) x^2-9, -4)
(iii) If we put zero as an initial guess, we find the negative root of the equation: x =
fzero (@(x) x^2-9, 0)
(iv) Try the following command and comment: x = fzero(@(x)x^2-9,[-4,4])
1. Calculate the vapour pressures of each species, for example by using the Antoine
equation:
𝐵𝑖
𝐴𝑖 −
𝐶𝑖 +𝑇
𝑝𝑣𝑎𝑝 𝑖 = 10
2. Calculate the equilibrium Ki values:
𝑝𝑣𝑎𝑝 𝑖
𝐾𝑖 = 𝑃
3. Solve the following nonlinear equation for α, which is the molar ratio of vapour to feed
phase fractions:
𝑧𝑖 (1−𝐾𝑖 )
∑𝑛𝑖=1 =0 Note: α must lie in the range 0 ≤ α ≤ 1
1+𝛼(𝐾𝑖 −1)
4. For the correct α value, calculate the liquid and vapour phase mole fractions, xi and yi:
Page 5
CE1532 Process Modeling and Simulation Lab
𝑧
𝑥𝑖 = 1+𝛼(𝐾𝑖 −1)
𝑖
𝑦𝑖 = 𝐾𝑖 𝑥𝑖
The parameters for the Antoine equation for temperature in °C and vapour pressure in
kPa are:
Component A B C
Ethene 5.76299 395.74 266.681
Ethane 5.94834 663.72 256.681
Propane 5.92257 804.00 247.040
N-butane 5.92695 935.77 238.789
Your task is to write a Matlab script file (flash.m) to perform these calculations and present
the results for α, x, y and K.
The challenge in this task is not so much the difficulty of the calculations or the nonlinear
function itself, but rather it is in deciding the best approach to use in structuring your
program.
You’ll need to use fzero to solve the equation, but how will you pass it the information it
needs? Look at this week’s lecture for the options for passing parameters to functions. While
there is only one correct numerical answer, there are many ways you could write the Matlab
files. You are strongly encouraged to sketch out and discuss your approach before attempting
to code it up.
There are many ways to code up and solve this problem. Here is one solution that used
the nested function approach for passing parameters.
Task 3
Complete the missing commands in the script file flash.m:
clc; fprintf('FLASH CALCULATOR\n')
Page 6
CE1532 Process Modeling and Simulation Lab
% Perform flash
[alpha,x,y,K] = flashcalc(z,T,P,A,B,C,pvap);
% Show results
% Put species names in a cell array for ease of printing
s = {'ethene', 'ethane', 'propane', 'n-butane'};
% Produce table
fprintf('\nVapour phase fraction: %10.4f\n',alpha)
fprintf('Component x y K\n')
for i = 1:length(z)
fprintf('%9s %10.4f %10.4f %10.4f\n',s{i}, x(i), y(i), K(i))
end
K=pvap/P
for i=1:4
Pvap(i)= (10)^(A(i)- (B(i)/(C(i)+T)))
K(i)=pvap(i)/P
end
% Solve the flash problem by using fzero to find the correct alpha value
opts = optimset('Display','iter'); % Get fzero to show its iterations
alpha = fzero(@ferr,[0 1],opts); % alpha must be in range 0-1
Page 7
CE1532 Process Modeling and Simulation Lab
To help you check your coding, you should get these answers:
Task 4:
In the previous course Numerical Methods in Chemical Engineering, we solved the following
problem using the Newton-Raphson Technique. Try to solve the same problem with
optimset and fzero.
The ideal gas law can represent the pressure-volume-temperature (PVT) relationship of
gases only at low (near atmospheric) pressures. For higher pressures more complex
equations of state should be used. The calculation of the molar volume and the
compressibility factor using complex equations of state typically requires a numerical
solution when the pressure and temperature are specified. The van der Waals equation
of state is given by:
a
P 2 (V b) RT
V
where
27 R 2TC2 RTC
a and b
64 PC 8PC
P = pressure in atm
V = molar volume in liters/g-mol
T = temperature in K
R = gas constant (R = 0.08206 atm.liter/g-mol.K)
Tc = critical temperature (405.5 K for ammonia)
Pc = critical pressure (111.3 atm for ammonia)
P
Reduced pressure is defined as: Pr
PC
PV
Compressibility factor is defined by: Z
RT
Develop a function file vanderwaals and a script file vanderwaalsprob to find the solution of
the above Non-Linear Algebraic Equation.
Function file:
Function [V,Z,z,v]=vanderwaals(a,b,P,p,T,R)
Page 8
CE1532 Process Modeling and Simulation Lab
Script file:
P=input(‘Enter Pressure’);
Pr=input(‘Enter the value of set of reduced pressure’);
T=450
R=0.08206
Tc=405.5
Pc=111.3
a=(27/64)*(((R^2)*(Tc^2))/Pc)
b=(R*Tc)/(8*Pc)
p=Pr.*Pc
[V,Z,z,v]=vanderwaals(a,b,P,p,T,R);
V
Z
Z
V
plot(Pr,z)
xlabel(‘Reduced Pressure’)
ylabel(‘Compressiblity Factor’)
title(‘Z vs Pr’)
(a) Calculate the molar volume and compressibility factor for gaseous ammonia at a
pressure P = 450 K using the van der Waals equation of state.
(b) Repeat the calculations for the following reduced pressures: Pr = 1, 2, 4, 10 and 20
(c) Also plot a labeled graph of compressibility factor vs reduced pressure. The figure
should be similar to the following figure:
Output:
>> ranjith
Enter Pressure450
Enter the value of set of reduced pressure1
T =
450
R =
Page 9
CE1532 Process Modeling and Simulation Lab
0.0821
Tc =
405.5000
Pc =
111.3000
a =
4.1969
b =
0.0374
p =
111.3000
>> vanderwaals
Cannot find an exact (case-sensitive) match for 'Function'
Graph:
Page 10