27
Computational Methods in the Introductory Courses to Chemical Engineering (KETA01) and Biotechnology (KKKA05) Carmen Ar´ evalo Lund University [email protected] Lecture 1 Ar´ evalo KETA01 & KKKA05

Computational Methods in the Introductory Courses to ... · Computational Methods in the Introductory Courses to Chemical Engineering (KETA01) and Biotechnology (KKKA05) Carmen Ar

Embed Size (px)

Citation preview

Computational Methods in theIntroductory Courses to

Chemical Engineering (KETA01) andBiotechnology (KKKA05)

Carmen Arevalo

Lund University

[email protected]

Lecture 1

Arevalo KETA01 & KKKA05

Anonymous functions in matlab

Anonymous functions can be written directly in the Command Window.

Structure of an anonymous function:

”name of function” = @(”name of variable”) ”formula”

Example: To construct the anonymous function for g(x) =( x2.4

)3

− 2x+ cosπx

12write in the Command Window

g = @(x) (x/2.4)^3 - 2*x+cos(pi*x/12);

To evaluate this function at x = 4.8, write

y = g(4.8)

Arevalo KETA01 & KKKA05 1

M-file functions

• Always starts with the word function

• Must be saved in an M-file

• The names of the M-file and of the function must be the same

Structure:

function ["output param"] = "function name"("input param")"body of the function" (can consist of several statements)

If only one output parameter, brackets may be ommitedIf several input or output parameters, separate by commas.

Arevalo KETA01 & KKKA05 2

Example of a simple M-file function

fun(x) → p

function p = fun(x)% Evaluates a function p at x% Input: x, any real number% Output: pp = (x/2.4)^3 - 2*x + cos(pi*x/12);

This is written using the matlab editor and saved in a file called fun.m

To evaluate the function at 4.8 and name the result y we write the command

y = fun(4.8)

Arevalo KETA01 & KKKA05 3

Example of an M-file function with 2 output parameters

The following function finds the roots of a quadratic polynomial. It mustbe saved in a file called quadroot.m

function [x1,x2] = quadroot(a,b,c)% Solves equation ax^2+bx+c=0% Input: a,b,c, coefficients of the quadratic polynomial% Output: x1,x2, the complex roots of the polynomialt = -b/(2*a);d = sqrt(t^2-c/a);x1 = t + d;x2 = t - d;

To name the roots of 3x2 − 5x+ 7 root1 & root2, write the command

[root1, root2] = quadroot(3,-5,7);

Arevalo KETA01 & KKKA05 4

The matlab for-loop

Used to repeat some commands.

Always starts with the word for and ends with the word end

Example: construct the row vector x with 50 elements

x(j) =1

2j + 3j, j = 1, . . . , 50

We construct a script with the commands

for j=1:50v(1,j) = 1/(2^j+3^j);

end

Arevalo KETA01 & KKKA05 5

Examples of for-loops

for j = 1:3a(j,1) = j^2;

end

produces vector a =(

12 22 32)T =

(1 4 9

)Tw = 1:10;for k = 1:2:10

w(k) = 0;end

produces w =(

0 2 0 4 0 6 0 8 0 10)

Arevalo KETA01 & KKKA05 6

Constructing a for-loop

Let’s add −2 times the first row to the second and third rows of

A =

1 3 22 2 12 1 3

for j = 2:3

A(j,:) = A(j,:) - 2*A(1,:);end

The result is

A =

1 3 20 −4 −30 −5 −1

Arevalo KETA01 & KKKA05 7

Gas laws

Ideal gas law: pV = nRTp is absolute pressure, V is volume, n is number of moles, R is universalgas constant, T is absolute temperature.

van der Waals equation:(p+

a

v2

)(v − b) = RT

v = V/n is molar volume, a and b constants which depend on the gas.

Project: Estimate v for carbon dioxide and oxygen for all combinations ofT = 300, 500, 700 K, and p = 1, 10, 100 atm, in order to choose appropriatecontainment vessels.

Data: R = 0.082054 L atm/(mol K), carbon dioxide (a = 3.592, b =0.04267), oxygen (a = 1.360, b = 0.03183)

Arevalo KETA01 & KKKA05 8

Using the Ideal Gas Law

Molar volumes for both gases: v =V

n=RT

p

Temperature, K Pressure, atm Molar Volume, L/mol300 1 24.6162

10 2.1416100 0.2462

500 1 41.027010 4.0127

100 0.4103700 1 57.4378

10 5.7438100 0.5744

Arevalo KETA01 & KKKA05 9

Using the van der Waals equation

We know a, b, R and are given P , T . The unknown is V . To solve(P +

a

V 2

)(V − b) = RT

is the same as to solve(P +

a

V 2

)(V − b)−RT = 0

or to find the zeros (roots) of the function

f(V ) =(P +

a

V 2

)(V − b)−RT

We need a numerical method that solves (finds the zeros of) non-linearequations of the form f(x) = 0.

Arevalo KETA01 & KKKA05 10

The graphical method: where f crosses the x-axis

Let’s solve in the interval V ∈ [0, 50] for the case P = 1, T = 300.

To plot the function, we first define the function by constructing an M-function

function f=myvanderW(v)% Input: v, vector of volumes% Output: f, value of function at vR=0.082054;a=3.592; b=0.04267; %carbon dioxideP=1;T=300;f=(P+a./v.^2).*(v-b)-R*T;

Arevalo KETA01 & KKKA05 11

Plotting the van der Waals function

>> v=linspace(0,50);>> y=myvanderW(v);>> plot(v,y)>> grid

0 5 10 15 20 25 30 35 40 45 50−30

−20

−10

0

10

20

30van der Waals equation for carbon dioxide

volume

nonl

inea

r fu

nctio

n of

vol

ume

Arevalo KETA01 & KKKA05 12

Matlab command ginput

enables you to select points from the figure using the mouse

>> v=linspace(0,50);>> y=myvanderW(v);>> plot(v,y,v,zeros(size(x)))>> grid>> v0=ginput(1)v0 =

2.447076612903226e+001 0

Arevalo KETA01 & KKKA05 13

The Newton-Raphson method: a graphical understanding

We start by guessing the zero to be x0. The point were the tangent of thecurve at x0 crosses the x-axis is usually an improved estimate.

Arevalo KETA01 & KKKA05 14

The Newton-Raphson idea applied to the gas problem

The slope of the tangent at x is f ′(x).

f(V ) =(P +

a

V 2

)(V − b)−RT

f ′(V ) = P +a

V 2− 2aV 3

(V − b)

Guess V = 30: f(30) = 5.4607, f ′(30) = 0.9960

Straight line with slope 0.9960, passing through (30, 5.4607):

y = 0.9960(V − 30) + 5.4607

crosses the X-axis at 0 = 0.9960(V − 30) + 5.4607⇒ V = 24.5175.

Note that f(24.5175) = 0.0049.

Arevalo KETA01 & KKKA05 15

The Newton-Raphson method

Also called Newton’s Method, approximates a solution to f(x) = 0.

An approximation to the derivative at xi is

f ′(xi) =f(xi)− f(xi+1)

xi − xi+1

and if we suppose that we reached the zero of f at xi+1,

f ′(xi) =f(xi)− 0xi − xi+1

⇒ xi+1 = xi −f(xi)f ′(xi)

Arevalo KETA01 & KKKA05 16

Newton-Raphson is an iterative formula

Newton-Raphson’s formula

xi+1 = xi −f(xi)f ′(xi)

starts by guessing the value of the zero of f . This starting value, x0, isthen used iteratively to produce

x1, x2, x3, x4, x5, . . . .

The last calculated value will be an approximation to the zero of thefunction.

Arevalo KETA01 & KKKA05 17

Stopping criterion

We can repeat this process until one (or more) of these occur

• f(xi+1) ≈ 0

• absolute error = |xi − xi+1| ≈ 0

• relative error = |xi − xi+1|/|xi+1| ≈ 0

• we have repeated the process too many times

Arevalo KETA01 & KKKA05 18

Error after each Newton iteration

The number of correct figures approximately doubles with each iteration.

i 0 1 2 3 4xi 0 0.500000000 0.566311003 0.567143165 0.567133290

In some special cases Newton’s Method converges slowly or not at all, andthat is why we need to set a maximum number of iterations.

If the starting point is not close enough to the solution, Newton’s methodmay not converge.

Use a graphic approach to get a good starting point.

Arevalo KETA01 & KKKA05 19

Matlab solvers for f(x) = 0

• Polynomials: r = roots(p)The van der Waals equation can be written as

PV 3 − (RT + bP )V 2 + aV − ab = 0

>> p=[1 -0.082054*300-.04267 3.592 -3.592*.04267];>> r=roots(p)r =24.51260.0731 + 0.0301i0.0731 - 0.0301i

Arevalo KETA01 & KKKA05 20

• Other nonlinear functions:

– if fun is an M-function:>> x = fzero(@fun,x0)

– if f is defined as an anonymous function:>> f=@(t)exp(t/5)-0.3;>> x = fzero(f,2)x = -6.0199

– f can also be defined inside the call to fzero:>> x = fzero(@(t)exp(t/5)-0.3,2)x = -6.0199

Arevalo KETA01 & KKKA05 21

Residual

The residual for f(x) = 0 with approximated solution x is f(x).

We want the residual to be as close to 0 as possible.

>> [x,residual] = fzero(@fun,x0)

>> f=@(t)exp(t/5)-0.3;>> [x,residual] = fzero(f,2)x = -6.0199residual = -5.5511e-017

>> [x,residual] = fzero(@(t)exp(t/5)-0.3,2)x = -6.0199residual = -5.5511e-017

Arevalo KETA01 & KKKA05 22

Solution with fzero

>> v0=ginput(1)

v0 =2.497031112812236e+001 0

>> [x,res] = fzero(@myfunction,v0(1))

root =

2.451258812844150e+001

res =

3.552713678800501e-015

Arevalo KETA01 & KKKA05 23

Failing Newton-Raphson method: dependence on initialguess

Apply the method to find the root of f(x) = −x4 + 3x2 + 2 with initialguess of x0 = 1.

xi+1 = xi −−x4

i + 3x2i + 2

−4x3i + 6xi

x1 = −1

x2 = 1

x3 = −1

The method alternates between the two non-roots -1 and 1, and fails tofind a root. The actual roots are -1.8872 and 1.8872.

Arevalo KETA01 & KKKA05 24

Good choice of initial guess

The Newton-Raphson method is a local method. This means the initialguess should be chosen close enough to the solution. Taking as initial guessx0 = 2.5, we get

x1 = 2.1145

x2 = 1.9323

x3 = 1.8895

x4 = 1.8872

Arevalo KETA01 & KKKA05 25

Newton-Raphson method for systems of equations

For a single equation use fzeroFor a system of equations use fsolve

Example: to solve the system

x2 − 3y + 2 = 0

x3 − 4x2 − xy + 1 = 0

function f=newf2(x)f1=x(1).^2-3*x(2)+2;f2=x(1).^3-4*x(1).^2-x(1).*x(2)+1;f=[f1;f2];

In command window: [x,fval] = fsolve(@newf2,[0,0])

Arevalo KETA01 & KKKA05 26