You are on page 1of 3

ChEn 3201: Numerical methods in ChEn applications

Computer Lab 9 Goals


Investigate numerical instability during the solution of ODE-IVPs. Investigate several time integration techniques for the solution of a set of ordinary dierential equations.

Preliminaries
To save time, you may want to download the les vEuler.m, vEuler2.m, vImpEuler.m, vImpEuler2.m, vRK4.m, vRK42.m, and fun2.m to your own diskette beforehand from the course web site.

Exercises
For all of the exercises in todays computer lab, well use the functions vEuler.m, vImpEuler.m, and vRK4.m, which employ the Explicit Euler method, the Implicit Euler method (with the Newton-Raphson method), and the Fourth-order Runge-Kutta method to solve for a set of ODE-IVPs (the v denotes vector): dy = f (y), dt with initial conditions y(0) = y 0 . All are called as follows: [T,Y] = vEuler(t0,tf,y0,h) [T,Y] = vImpEuler(t0,tf,y0,h) [T,Y] = vRK4(t0,tf,y0,h) where t0 and tf are the initial and end values of the independent variable (typically time, t), y0 is the initial value of y at t0, and h is the step size, h = ti+1 ti . These functions provide a matrix [T,Y] containing the values of time and the solution for each integration step, as follows: t0 y1 (t0 ) y2 (t0 ) . . . yn (t0 ) t1 y1 (t1 ) y2 (t1 ) . . . yn (t1 ) [T, Y ] = t2 y1 (t2 ) y2 (t2 ) . . . yn (t2 ) . . . . . . . . . . . . . . . tf y1 (tf ) y2 (tf ) . . . yn (tf ) The dimension of matrix [T,Y] is (nsteps+1) (n+1), where nsteps denotes the number of integration steps taken and n denotes the number of ODEs in the equation set. This matrix can be used to conveniently plot output from the computation. For example, the command, >> plot(T,Y) will produce a plot of all values of y1 , y2 , . . . , yn versus t. Within each of these functions is another vector function rhs=f(y) which returns the values of the righthand-side function of the ODE evaluated at y. The function vImpEuler.m also has a subroutine J=ssjac(y) that computes the values of the steady-state Jacobian, namely, Jij = fi /yj . You should take a look at these functions and make sure you understand how they work.

Numerical instability during the solution of ODE-IVPs


Lets consider solving the following problem, as discussed in lecture: dy dx y(0) = y, = 1.

We want to investigate the stability of dierent methods for solving this equation numerically. Weve already set up the functions to solve this scalar equation. First, give these methods a trial run by issuing the following commands, >> t0=0; tf=6; y0=1; h=0.2; >> [T1,Y1] = vEuler(t0,tf,y0,h); [T2,Y2] = vImpEuler(t0,tf,y0,h); [T3,Y3] = vRK4(t0,tf,y0,h); Do you understand what each argument stands for and what problem is being solved? Plot y versus t for each method, along with the exact solution, via: >> [te,ye] = fplot(exp(-x),[0 6]); >> plot(te,ye,-,T1,Y1,o-,T2,Y2,^-,T3,Y3,d-) Which curves correspond to which methods? Now, lets repeat this for larger time steps (youll want to cut and paste commands to make this easier). For a time step size of h=1: >> h=1; >> [T1,Y1] = vEuler(t0,tf,y0,h); [T2,Y2] = vImpEuler(t0,tf,y0,h); [T3,Y3] = vRK4(t0,tf,y0,h); Plot the results again using: >> plot(te,ye,-,T1,Y1,o-,T2,Y2,^-,T3,Y3,d-) Are the results stable? Are the results accurate? Repeat for an even larger time step size of h=2.01: >> h=2.01; >> [T1,Y1] = vEuler(t0,tf,y0,h); [T2,Y2] = vImpEuler(t0,tf,y0,h); [T3,Y3] = vRK4(t0,tf,y0,h); Plot the results again using: >> plot(te,ye,-,T1,Y1,o-,T2,Y2,^-,T3,Y3,d-) Are the results stable? Are the results accurate? Repeat for an even larger time step size of h=3: >> h=3; >> [T1,Y1] = vEuler(t0,tf,y0,h); [T2,Y2] = vImpEuler(t0,tf,y0,h); [T3,Y3] = vRK4(t0,tf,y0,h); Plot the results again using: >> plot(te,ye,-,T1,Y1,o-,T2,Y2,^-,T3,Y3,d-) Are the results stable? Are the results accurate? Do you understand what is happening and why for each method?

Solution of a set of ODE-IVPs


Lets examine the solution of the following set of ODEs: dy1 dt dy2 dt y1 (0) y2 (0) = 0.02y1 , = = = 0.02y1 0.02y2 , 20, 10.

This equation set can be integrated sequentially to nd the analytical solution: y1 (t) y2 (t) = = 20et/50 , (0.4t + 10)et/50 .

To make your life easier, weve placed alternative functions, named vEuler2.m, vImpEuler2.m, vRK42.m, on the web site which have the above equation set in place. Run these as follows. Start by setting the parameters and initial condition; >> t0=0; tf=50; y0=[20 10]; h=5; Then use the dierent methods to integrate the equation set: >> [T1,Y1] = vEuler2(t0,tf,y0,h); [T2,Y2] = vImpEuler2(t0,tf,y0,h); [T3,Y3] = vRK42(t0,tf,y0,h); Compare your results to the exact solution in a plot, using: >> [t1e,y1e] = fplot(20*exp(-x/50),[0 50]); [t2e,y2e] = fplot((0.4*x+10)*exp(-x/50),[0 50]); >> plot(t1e,y1e,t2e,y2e,T1,Y1,o-,T2,Y2,^-,T3,Y3,d-) There are lots of curves here. Make sure you understand which curve is what. Try repeating the above with smaller time steps. What happens? We can also use the MATLAB intrinsic functions to integrate sets of ODEs. For example, you can simply call >> [T,Y] = ode45(odefun,tspan,y0) with y0 as a vector and odefun as a vector-valued function. Just to remind you, the arguments of this function consist of tspan = [to tf], containing initial and nal times t0 and tf, with initial conditions y0. Function odefun(t,y) must return a column vector corresponding to f(t,y). Each row in the solution array Y corresponds to a time returned in the column vector T. Thus the array [T,Y] is identical in form to the arrays we dened above. Weve supplied a simple function fun2.m, representing the problem we solved above. To see how the MATLAB routine works on this problem issue the command: >> [T,Y] = ode45(@fun2,[0 50],y0); Compare this numerical solution with the analytical solution via, >> plot(t1e,y1e,t2e,y2e,T,Y,o-) Did the MATLAB function do a good job? You can nd more about how to use it (and other algorithms) in the MATLAB help section.

You might also like