0% found this document useful (0 votes)
83 views10 pages

Lab 2 Edt FRRM Sahil

This document describes solving nonlinear algebraic equations using MATLAB. It discusses using the roots, fzero, and fsolve functions to find zeros of polynomials, continuous nonlinear functions of one variable, and sets of nonlinear functions respectively. It provides examples of using polyval to evaluate polynomials, roots to find the roots of a polynomial, and fzero to find the real root of a single variable equation. It also describes performing a flash calculation to determine vapor-liquid equilibrium given feed conditions using fzero to solve the nonlinear flash equation.

Uploaded by

Aneesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views10 pages

Lab 2 Edt FRRM Sahil

This document describes solving nonlinear algebraic equations using MATLAB. It discusses using the roots, fzero, and fsolve functions to find zeros of polynomials, continuous nonlinear functions of one variable, and sets of nonlinear functions respectively. It provides examples of using polyval to evaluate polynomials, roots to find the roots of a polynomial, and fzero to find the real root of a single variable equation. It also describes performing a flash calculation to determine vapor-liquid equilibrium given feed conditions using fzero to solve the nonlinear flash equation.

Uploaded by

Aneesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

CE1532 Process Modeling and Simulation Lab

Process Modelling and Simulation Lab (CE 1532)

LAB 2: NON-LINEAR ALGEBRAIC EQUATIONS


1. Nonlinear algebraic equations
Nonlinear algebraic equations are of the form f(x) = 0 and are generally presented as a set of
equations:
f1(x) = 0
f2(x) = 0

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

3. Solving polynomial equations


Evaluating Polynomials
Polynomials are a special type of non-linear algebraic equation of the general form:

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.

MATLAB function: polyval

MATLAB represents polynomials as row vectors containing coefficients ordered by


descending powers. For example, the equation P(x) = x4 + 7x3 - 5x + 9 can be represented as:

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

%To evaluate a given polynomial p at x = 5


%the given instruction is used to calculate the values

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:

MATLAB function: roots

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:

f1(x) = x4 – 3x3 – 2x2 + 5x + 2 = 0 (1)

f2(x) = x3 – 2x + 3 = 0 (2)

Also get poly.m to plot both functions on the same graph:


 Use x values in the range –4 to +4 with a step size of 0.1
 You can use the polyval function to calculate the corresponding values of f1(x) and f2(x),
or you can write your own statements to do it

Page 2
CE1532 Process Modeling and Simulation Lab

 Label the x and y axes appropriately


 Include a suitable legend
 Restrict the x-axis of the graph to the range –3 to 4 and the y-axis to the range –20 to
40 by using axis ([-3 4 -20 40]), or use xlim and ylim to set them separately
 Turn on gridlines using grid on
Script File:

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

MATLAB function: polyfit

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)

4. Solving a nonlinear equation of one variable


The fzero function in MATLAB is used to find the real root of a single equation. Its syntax:
fzero (function, x0)
where function is the name of the function being evaluated and x0 is the initial guess. For
two initial guess values, a square bracket may be used to pass these values in such a manner
that it bracket the opposite signs.
fzero (function, [x1, x2])
The fzero function works as follows:
(i) If a single guess is passed it performs a search in both positive and negative
directions of the guess to identify the sign change.
(ii) The fast methods (secant and inverse quadratic interpolation) are used unless the
root estimate falls outside the bracket.
(iii) If an unacceptable result happens, bisection is implemented until an acceptable root
is obtained.

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])

x = fzero (@(x) x^2-9, 4)


x = fzero (@(x) x^2-9, -4)
x = fzero (@(x) x^2-9, 0)
x = fzero(@(x)x^2-9,[-4,4])
Error using fzero (line 290)
The function values at the interval endpoints must differ in sign.

5. Performing a flash calculation (with consideration of program structure)


A two-phase flash calculation finds the equilibrium vapour and liquid compositions for a given
feed stream at given temperature and pressure conditions. For a feed containing n species with
mole fractions zi, and for temperature T and pressure P, and assuming ideal behaviour, the
calculation can be performed as follows:

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)
𝑖
𝑦𝑖 = 𝐾𝑖 𝑥𝑖

You’re asked to perform a flash calculation for the following conditions:


Temperature: 45°C
Pressure: 2000 kPa
Feed mole fractions: ethene 0.05, ethane 0.35, propane 0.50, n-butane 0.10

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')

% Enter the parameters for Antoine vapour pressure equation:


% pvap (kPa) = 10^(A - B/(T+C)) for T in degC
% ethene ethane propane n-butane
A=[5.76299 5.94834 5.92257 5.92695]
B=[395.74 663.72 804.00 935.77]
C=[266.681 256.681 247.040 238.789]
p=[A B C]
T=45+273.15
pvap=10.^(A-B/(T+C))
% Feed mole fractions
% ethene ethane propane n-butane
z=[0.05 ;0.35;0.50 ;0.10]

Page 6
CE1532 Process Modeling and Simulation Lab

% Flash conditions P and T


P=2000

% 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

The corresponding function file flashcalc.m is:


function [alpha,x,y,K] = flashcalc(z,T,P,A,B,C,pvap)
% FLASHCALC: Perform an ideal flash calculation. Uses nested function
% approach for passing parameters to fzero. Also note use of array
% operators to vectorise calculations.
% Inputs:
% z = feed mole fractions
% T = flash temperature
% P = flash pressure
% A, B, C = Antoine equation parameters
% Outputs:
% alpha = vapour phase to feed mole ratio
% x = species mole fractions in liquid phase
% y = species mole fractions in vapour phase
% K = equilibrium constants

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

% Calculate vapour and liquid mole fractions from alpha


for i=1:4
x(i)=z(i)/(1+alpha*(K(i)-1));
y(i)=K(i)*x(i);
end
function f = ferr(alpha)
% FERR: Error function for flash calculation: the correct alpha will
% give ferr = 0. Note this function is "nested" inside the flashcalc
% function and therefore has access to its variables (esp. z and K).
f = sum(z.*(1-K)./(1+alpha*(K-1)));
end % "end" statement is needed here

end % "end" statement is also needed here

Page 7
CE1532 Process Modeling and Simulation Lab

To help you check your coding, you should get these answers:

Vapour phase fraction: 0.7131


Component x y K
ethene 0.0044 0.0683 15.5691
ethane 0.1532 0.4292 2.8005
propane 0.6145 0.4539 0.7387
n-butane 0.2279 0.0486 0.2131

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

The variables are defined by

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

V=fzero(@(x) ((p*x)-(p*b)+(a/x)-((a/b)/x^2)-(R*T)), 0.6);


Z=(P*V)/(R*T);
Opts=optimset(‘Display’,’iter’);
for i=1:5
v(i)=fzero(@(x)((p(i)*x)-(p(i)*b)+(a/x)-((a*b)/x^2)-
(R*T)), 0.5);
z(i)=(p(i)*v(i))/(R*T);
end
end

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

Attempt to execute SCRIPT vanderwaals as a function:


D:\waste box1\vanderwaals.m

Error in ranjith (line 10)


[V,Z,z,v]=vanderwaals(a,b,P,p,T,R);

>> vanderwaals
Cannot find an exact (case-sensitive) match for 'Function'

The closest match is: function in C:\Program


Files\MATLAB\R2018a\toolbox\matlab\lang\function.m

Error in vanderwaals (line 1)


Function [V,Z,z,v]=vanderwaals(a,b,P,p,T,R)

Graph:

Page 10

You might also like