Sie sind auf Seite 1von 3

Lab 7, ODEs and initial value problems

Topics
Mathematics: Euler and simple RungeKutta methods; errors and step sizes. MATLAB: using functions as input arguments for other functions; built-in ODE solvers ode23 and ode45.

Preparation
Read your lecture notes. See also Chapra and Canale, Section 25.1 (pages 681689) and Section 25.2 (pages 693699). In the rst part of this lab you will work through some of the exercises from this chapter (Exercises 25.1 to 25.3). You will need an extra function M-le for this lab session: myeuler.m solves the initial value problem y = f (x, y ), by using Eulers method. This M-le is available from the Sample M-les on the course web page. Read the help for this M-le to see how to use it. You do not need to change this le, but you do need to know what input variables it needs, and what output variable it produces, so read the help carefully! y (x0 ) = x0

A. Passing a function as an argument to another function


It is very useful in MATLAB to be able to pass one function as an argument (input) to another function. For example, we dont want a program that will only do Eulers method on one particular dierential equation. We would like to be able to use the same program for any dierential eqaution. An easy way to do this is to write a short function M-le that calculates our right-hand side f (x, y ) for given values of x and y . Then all we need to do is to tell myeuler (or whichever dierential equation solver we are using) the name of that function, and it will be able to the solve the dierential eqaution for us. Write a function M-le myfn.m which accepts scalar inputs x and y and then returns the value f (x, y ) = yx2 1.1y. Your function le should not print out any values (use semicolons in the le). Check your function does what you think it should by entering the command >> myfn(1,1) at the command line. Now your function is working, we need to be able to pass it as an argument to other functions. We want to pass the name of the function, rather than its value (output). To do this we put a @ symbol immediately before the name of the function: @myfn. This is what MATLAB calls a function handle. Youll see an example of how to do this in just a moment.

B. Eulers method
In this part of the lab, you will use Eulers method to solve the following initial value problem y = yx2 1.1y, and look at how reducing the step size aects the errors. 1. Solve the problem analytically over the interval from x = 0 to x = 2. (Hint: the dierential equation is rst order and separable.) 1 y (0) = 1

2. Use Eulers method with h = 0.20, h = 0.10 and h = 0.05 to solve the above initial value problem numerically over the interval from x = 0 to x = 2. Hints: Remember to pass your function as a function handle (@myfn), so your call to myeuler will begin with myeuler(@myfn,...). myeuler.m needs to know the number of steps to take (N ), so think about how many steps you will need for these each value of h. 3. Plot the exact solution and your approximate solutions on the same graph. Checkpoint: Tabulate the error verses h at x = 1.60 when h is successively halved; i.e. h = 0.20, 0.10, 0.05. What happens to the error in the numerical approximation as you reduce the step size? Use your tabulation of the error to estimate the order of your Euler method.

C. Heuns method, a second order RungeKutta method


Make a copy of myeuler.m and call it myrk2.m. Modify the code so that it carries out Heuns method for solving initial value problems y = f (x, y ), y (x0 ) = x0 . To calculate yj from yj 1 Heuns method uses the following steps: k1 = f (xj 1 , yj 1 ) k2 = f (xj 1 + h, yj 1 + hk1 ) yj = yj 1 + h(k1 + k2 )/2 the slope at the latest point estimate next point and nd the slope there use average slope to get a better estimate

Hints: The new program myrk2.m will need a new rst line function [x, y] = myrk2(dydx, xspan, y0, N)

Notice that myeuler.m already calculates the rst slope estimate. Insert a line of MATLAB code for the second slope estimate. Finally, modify the line which calculates the new y-value. 1. Use Heuns method with h = 0.20, h = 0.10 and h = 0.05 to solve the above initial value problem numerically over the interval from x = 0 to x = 2. 2. Plot the exact solution you found in part B and your approximate solutions from using Heuns method on the same graph. Checkpoint: What happens to the error in the numerical approximation as you reduce the step size? How does Heuns method compare to Eulers method? Tabulate the error verses h at x = 1.60 when h is successively halved; i.e. h = 0.20, 0.10, 0.05. What happens to the error in the numerical approximation as you reduce the step size? Use your tabulation of the error to estimate the order of your RK method.

D. MATLABs built-in ODE solvers


In a simple model of a chemical reaction, the temperature u of the system at time t is governed by two main factors: the energy being produced by the reaction, and energy being lost to the surroundings by convection. An energy balance gives the following dierential equation for u(t) ( ) E du = A exp k (u ua ) dt Ru where A is the rate of the reaction, 2

E is the activation energy (the minimum energy needed for the reaction to occur), R is the gas constant, u is the temperature (measured in Kelvin), k is a Newtonian cooling constant, and ua is the ambient temperature, the temperature of the surroundings (also in Kelvin). The rst term on the right-hand side is called the Arrhenius term (after the rst person to make an in-depth study of the eect of temperature on the rate of reactions). This term comes from looking at the probability of atoms in the system colliding with one another. The second term on the right-hand side represents Newtonian cooling, where an object cools at a rate proportional to its temperature dierence with its surroundings. In this part of the lab, you will solve this dierential equation with the following parameter values: A = 100 Ks1 , E = 24900 J, R = 8.3 JK1 , k = 1 s1 , ua = 300 K,

and an initial condition u(0) = 900 K. 1. Write a function le (like myfn.m) for the slope. Even though this function does not involve t you will still need to list it as a variable in the rst line of the function le, since your ODE solvers all expect the slope to depend on both variables. 2. Use Heuns method, with a step size of h = 0.5, to nd estimates for u(t) for 0 t 10. 3. MATLAB has several built-in ODE solvers. Read the help for the commands ode23 and ode45, and then use one of them to solve for u(t) for 0 t 10. Checkpoint: Plot your two estimates for u(t) on the same diagram. Describe how the physical system behaves over the rst 10 seconds.

Das könnte Ihnen auch gefallen