• No results found

Numerical tools for modelling

In document Advanced Control using MATLAB (Page 142-147)

Modelling dynamic systems with differential equations

3.5 Numerical tools for modelling

“Unfortunately there are no known methods of solving Eqn 1(a nonlinear differential equation). This, of course, is very disappointing.”

M. Braun, [33, p493]

Since differential equations are difficult to solve analytically, we typically need to resort to nu-merical methods as described in many texts such as [39,41,46,94,104,169] and in addition to [201].

Differential equations are broadly separated into two families:

ODEs Ordinary differential equations (ODEs) where time is the only independent variable. The solution to ODEs can be described using standard plots where the dependent variables are plotted on the ordinate or y axis against time on the x axis.

PDEs Partial differential equations (PDEs) are where space (and perhaps time) are independent variables. To display the solution of PDEs, contour maps or 3D plots are required in general.

Generally PDEs are much harder to solve than ODEs and are beyond the scope of this text.

The solution of nonlinear differential equations

One of the most important “tools” in the desk-top experimenter’s collection, is a good numerical integrator. We need numerical integrators whenever we are faced with a nonlinear differential equation which is intractable by any other means. MATLABsupplies a number of different nu-merical integrators optimised for different classess of problems. Typically the 4th/5th dual order Runge-Kutta implementation,ode45, is a good first choice.

To use the integrators, you must write a function subroutine that contains your differential system to be solved which will be called by the numerical integration routine.

1. The script file that calls the integrator (eg:ode45) with name of the function subroutine to be integrated and parameters including initial conditions, tolerance and time span.

2. The function subroutine (named in the script file) that calculates the derivatives as a func-tion of the independent variable (usually time), and the dependent variable (often x).

We will demonstrate this procedure by constructing templates of the above two files to solve a small nonlinear differential equation. One can reuse this template as the base for solving other ODE systems.

ODE template example

Suppose our task is to investigate the response of the nonlinear pendulum system given in Eqn.3.6and compare this with the linearised version. The script file given in Listing3.9 com-putes the angle θ = x1(t) trajectory for both the nonlinear and linear approximation.

Listing 3.9: Comparing the dynamic response of a pendulum to the linear approximation g=8.81; l=1; m = 5; T_c=0; % constants

xdot = @(t,x) [x(2); -g/l*sin(x(1)) + T_c/m/lˆ2]; % dx/dt x0 = [1,1]'; % start position x(0)

5 [t,x]= ode45(xdot,[0,10],x0); % Integrate nonlinear

% Now try linear system

A = [0 1; -g/l 0]; B = [0;0]; C = [1,0];D=0;

tv = linspace(0,10)';

10 [y2,t2]=lsim(ss(A,B,C,D),0*tv,tv,x0);

plot(t,x(:,1),t2,y2) % See Fig.3.23.

Note how the trajectories for the nonlinear (solid) result as calculated from ode45and linear approximation calculated usinglsimgradually diverge in Fig.3.23.

0 2 4 6 8 10

−1.5

−1

−0.5 0 0.5 1 1.5

time

x(t)

Non Linear

Linearised Figure 3.23: The trajectory of the true non-linear pendulum compared with the lin-earised approximation.

Problem 3.4 1. One of my favourite differential equations is called the Van der Pol equation;

md2y

dt2 − b 1 − y2 dy

dt + ky = 0

wherem = 1,b = 3andk = 2. Solve the equation for 0 < t < 20using bothode23and ode45. Sketch the solution. Which routine is better? You will need to create a function calledvandepol(t,y)which contains the system of equations to be solved.

2. A damped forced pendulum is an example of a chaotic system. The dynamic equations are dω

dt =−ω

q − sin θ + g cos φ (3.67)

dt = ω (3.68)

dt = ωd (3.69)

where there are three states, (x=

ω θ φ 

), and three parameters, (ρ=

q g ωd 

).

Simulate this system for 0 ≤ t ≤ 500 starting from x =  −1 2 0.3  and using ρ=

2 1.5 2/3 

as parameters. Be warned, this may take some computer time!

Problem 3.5 A particularly nasty nonlinear chemical engineering type model is found in [83]. This is a model of two CSTRs8 in series, cooled with a co-current cooling coil. The model equations are:

where the state, control, disturbance and measured variables are defined as xdef= 

Ca1 T1 Ca2 T2 

, udef= qc, ddef= 

Caf Tcf 

ydef= Ca2

and the parameters values are given in Table3.5.

Table 3.5: The parameter values for the CSTR model description variables value units

reactant flow q 100 l/min

conc. of feed reactant A Caf 1.0 mol/l

temp. of feed Tf 350 K

temp. of cooling feed Tcf 350 K

volume of vessels V1= V2 100 l

heat transfer coeff. hA1= hA2 1.67· 105 J/min.K pre-exp. constant k0 7.2· 1010 min−1

E/R E/R 1.0· 104 K

reaction enthalpy −∆H 4.78· 104 J/mol

fluid density ρc= ρ 1.0 g/l

heat capacity cp= cpc 0.239 J/g.K

Note that the model equations are nonlinear since the state variable T1 (amongst others) is a appears nonlinearly in the differential equation. In addition, this model is referred to as control nonlinear, since the manipulated variable enters nonlinearly. Simulate the response of the con-centration in the second tank (Ca2) to a step change of±10%in the cooling flow using the initial conditions given in Table3.6.

You will need to write a.m file containing the model equations, and use the integrator ode45.

Solve the system over a time scale of about 20 minutes. What is unusual about this response?

How would you expect a linear system to behave in similar circumstances?

Problem 3.6 Develop aMATLABsimulation for the high purity distillation column model given in [140, pp459]. Verify the open loop responses given on page 463. Ensure that your simulation is easily expanded to accommodate a different number of trays, different relative volatility etc.

8continuously stirred tank reactors

Table 3.6: The initial state and manipulated variables for the CSTR simulation description variable value units

coolant flow qc 99.06 l/min

conc. in vessel 1 Ca1 8.53· 10−2 mol/l

temp. in vessel 1 T1 441.9 K

conc. in vessel 2 Ca2 5.0· 10−3 mol/l

temp. in vessel 2 T2 450 K

Problem 3.7 A slight complexity to the simple ODE with specified initial conditions is where we still have the ODE system, but not all the initial conditions. In this case we may know some of the end conditions instead, thus the system is in principle able to be solved, but not in the standard manner;– we require a trial and error approach. These types of problems are called two point boundary problems and we see them arise in heat transfer problems and optimal control. They are so called two point boundary problems because for ODEs we have two boundaries for the independent variable; the start and the finish. Try to solve the following from [196, p178].

A fluid enters an immersed cooling coil 10m long at 200C and is required to leave at 40C. The cooling medium is at 20C. The heat balance is

d2T

dx2 = 0.01(T− 20)1.4 (3.70)

where the initial and final conditions are;

Tx=0= 200, Tx=10= 40

To solve the system, we must rewrite Eqn3.70as a system of two first order differential equations, and supply a guess for the missing initial condition dTdx x=0=? We can then integrate the system untilx = 10and check that the final condition is as required. Solve the system.

Hint: Try−47 < dTdx x=0<−42.

3.5.1 Differential/Algebraic equation systems and algebraic loops

In many cases when deriving models from first principals, we end up with a coupled system of some differential equations, and some algebraic equations. This is often due to our practice of writing down conservation type equations (typically dynamic), and constraint equations, say thermodynamic, which are typically algebraic. Such a system in general can be written as

f( ˙x, x, t) = 0 (3.71)

and is termed a DAE or differential/algebraic equation system. If we assume that our model is “well posed”, that is we have some hope of finding a solution, then we expect that we have the same number of variables as equations, and it follows that some of the variables will not appear in the differential part, but only in the algebraic part. We may be able to substitute those algebraic variables out, such that we are left with only ODEs, which can then be solved using normal backward difference formula numerical schemes such asrk2.m. However it is more likely that we cannot extract the algebraic variables out, and thus we need special techniques to solve these sorts of problems (Eqn.3.71) as one.

In a now classic article titled Differential/Algebraic Equations are not ODE’s, [158] gives some in-sight into the problems. It turns out that even for linear DAE systems, the estimate of the error,

which is typically derived from the difference between the predictor and the corrector, does not decrease as the step size is reduced. Since most normal ODE schemes are built around this as-sumption, they will fail. This state of affairs will only occur with certain DAE structures where the nilpotency or index is≥ 3. Index 2 systems also tend to cause BDF schemes to fail, but can be tackled using other methods. The index problem is important because in many times it can be changed (preferably reduced to less than two), by rearranging the equations. Automated modelling tools tend, if left alone, to create overly complex models with a high index that are im-possible to solve. However if we either by using an intelligent symbolic manipulator or human, change these equations, we may be able to reduce the index.

The difficulties of algebraic loops

One reason that computer aided modelling tools such as SIMULINKhave taken so long to mature is the problem of algebraic loops. This is a particular problem when the job of assembling the many different differential and algebraic equations in an efficient way is left to a computer.

Suppose we want to simulate a simple feedback process where the gain is a saturated function of the output, say

K(y) = max(min(y, 5), 0.1)

If we simulate this inSimulink using

1 s+1 Transfer Fcn Step

Scope

Saturation Product

1 Gain1 1

Gain

we run into an Algebraic Loop error. MATLABreturns the following error diagnostic (or something similar):

Warning: Block diagram ’sgainae’ contains 1 algebraic loop(s).

Found algebraic loop containing block(s):

’sgainae/Gain1’

’sgainae/Saturation’ (discontinuity)

’sgainae/Product’ (algebraic variable)

Discontinuities detected within algebraic loop(s), may have trouble solving

and the solution stalls.

The simplest way to avoid these types of problems is to insert some dynamics into the feedback loop. In the example above, we could place a transfer function with a unit gain and very small time constant in place of the algebraic gain in the feedback loop. While we desire the dynamics of the gain to be very fast so that it approximates the original algebraic gain, overly fast dynamics cause numerical stability problems, hence there is a trade off.

In document Advanced Control using MATLAB (Page 142-147)