Numerical Method HW4
Numerical Method HW4
Problem 1 (20 points). In this problem you will develop and implement an ODE solver for
problems of the form
dy
= f (y)
dt
y(t0 ) = y0
dy
y(t + ∆t) = y(t) + ∆t (1)
dt estimate
my ode solver should use the 2nd-order Runge-Kutta (RK2) estimate if option(1) > 0,
dy 1
= f (y(t) + ∆tf (y(t)))
dt estimate 2
and the Crank-Nicolson (CN, a.k.a. trapezoid rule) estimate if option(1)< 0,
dy 1
= (f (y(t)) + f (y(t + ∆t)))
dt estimate 2
Note that if you plug the Crank-Nicolson estimate into eqn. 1, you will get an implicit equation
in the unknown y(t + ∆t), so you will need to solve a system of (usually nonlinear) equations
to compute y(t + ∆t). You can solve the system of equations using any MATLABroutine
(e.g. fsolve) or a solver of your own devising. We suggest you use the RK2 update formula
to provide the initial guess.
Set the initial ∆t = |option(2)|. If option(2)> 0, hold constant at this ∆t. If option(2)< 0,
implement adaptive time-stepping by comparing ∆y = y(t+∆t)−y(t) computed two different
1
ways. For both the RK2 and CN methods, we suggest you compare the RK2 and CN estimates
of ∆y, using in either case the value for y(t + ∆t) you just computed. If you find that
1 1
∆tf y(t) + ∆tf (y(t)) − ∆t f (y(t)) + f (y(t + ∆t)) > atol + rtolky(t)k
2 2
then ∆t is unacceptably large and should be reduced in size (say cut in half) and the calcula-
tion repeated; otherwise accept the value of y(t+∆t) and increase ∆t by a user-defined factor
of option(3) for the next step. Set the error tolerances from user input, atol=option(4) and
rtol=option(5). For this problem use rtol=1e-8 and atol=1e-8. You may need to add a pro-
tection against ∆t becoming so tiny that t + ∆t and t are indistinguishable by the computer,
otherwise the program may get caught in an infinite loop.
Note that the user usually wants to know the values exactly at tf, so adjust the last ∆t to
make the final t = tf.
1. Implement my_ode_solver as described above. Test your ODE solver on the ODE-IVP
system considered in HW0, Problem 1. You can do this by slightly modifying the posted
solution so that it calls your function my_ode_solver. Test the RK2 and Crank-Nicolson
methods, each with and without adaptive time-stepping for initialize steps size of 10−4 and
10−2 . Generate plots for the same situation modeled in the posted solution. You have been
provided code to help create plots in ODE_plotter. Do all your solutions look OK? Note that
in the posted solution the tolerances in ode23 were left at their default values, which were
too loose. Hence, the adaptive time-stepping was not effective. Tighten the tolerances and
see how the number of time steps computed by ode23 increases. Report the number of steps
for ode23 at default and improved tolerances as well as the number of steps each of your four
methods take for each of the two initial step sizes.
2. There are some hidden issues with the posted solution. We expect dT/dr=0 at r=0, otherwise
there will be a non-differentiable cusp in T(r) at r=0. Also, physically we expect T to be
highest at the center and to monotonically decrease with radius. Hence we expect u2 (y =
R) = 0 and u2 < 0 for 0 < y < R. Do any of the solutions (from ode23, RK2, or CN) satisfy
this physicality condition?
3. The posted solution ends at y = R − 0.01 = 0.19m, i.e. at r = 1cm. Use ode23 and your
ODE-IVP solver to solve all the way to y = R − 0.001 = 0.199m, i.e. r = 1mm. Does the
computed u2 = dTdr look reasonable at small values of r? Does the implicit Crank-Nicolson
method work any better than the explicit RK2 method for this problem? Does adaptive
time-stepping help?
2
Problem 2 (10 points). Another issue for the solution to problem 1 for HW0 is that all of the
parameters in the problem are only known to a finite number of significant figures. In fact, qdot
is only given to one significant figure! The question is how sensitive our computed T and dT/dr
are to these uncertainties. A popular way to compute the sensitivities, ∂yi , is to differentiate the
∂pm
equation dy/dt = f(y; p) with respect to each of the M uncertain parameter values.
To perform this analysis, we consider a larger ODE problem. For our system, we define
h i
ỹ T = y1 y2 ∂ y1 ∂y1 ∂y2 ∂y2
∂p1 ∂p2 ∂p1 ∂p2
Here we have two state variables and two parameters but this definition could be extended in a
straightforward manner for a larger system. Note that ỹ is of length N + N M where N is the
number of state variables and M is the number of parameters. We want to consider the system dỹ
dt .
dỹk
= fk (t, y; p) for k = 1, . . . , N
dt
∂yi ∂yi,0
=
∂pm t=0 ∂pm
Write a 0$7/$% function that computes the sensitivity of the computed u(y) to these two uncertain
parameters: surface temperature (393 K), known to about∆p1 = 1K and qdot (20000W/m3 ) known
to within about ∆p2 = 5000W/m3 . Estimate the uncertainty in the predicted values of T and dT dr
at r = 1cm using this common approximation:
" 2 2 # 12
∂u1 ∂u1
δ(u1 ) ∼ ∆p1 + ∆p2
∂p1 ∂p2
" 2 2 # 12
∂u2 ∂u2
δ(u2 ) ∼ ∆p1 + ∆p2
∂p1 ∂p2
Describe the system that is being solved and report its initial conditions. You have been provided
code that calculates the finite difference approximation for the Jacobian (jacobian_solve). Note:
If one needs to compute a large number of sensitivities (e.g. if your model includes many uncertain
parameters and many differential equations), there are much more efficient methods for computing
the sensitivities, which take advantage of the fact that the ODEs for the sensitivities are partially
decoupled from each other and from the original ODEs. This is particularly important if the ODE
system is stiff.
3
Problem 3 (20 points). Epoxides are used on large scale, for example to make polyethers which
can be used in medical implants without being rejected by the immune system, or to make durable
polyurethane materials (e.g. skateboard wheels, transparent coatings for hardwood floors). Epox-
ides are made by catalytic oxidizing alkenes using O2 . Despite a lot of work (including by this
year’s Hottel Lecturer A. Corma) the catalysts available are not ideal. Your task is to figure out
the optimal energetics for a homogeneous catalyst X which works by this reaction sequence:
L
XOO + alkene → epoxide + XO k1 = 106 exp(−Ea1 /RT )
mol s
L
X + O2 → XOO k2 = 109 exp(−Ea2 /RT )
mol s
1014
XOO → X + O2 k2,rev = exp(−Ea2,rev /RT )
s
L
XO + XO → X + XOO k3 = 108 exp(−Ea3 /RT )
mol s
L
X + XOO → XO + XO k3,rev = 108 exp(−Ea3,rev /RT )
mol s
Assume Hf (X) = 0 and this relationship between each Ea and the corresponding ∆Hrxn .
r
1 1
Ea,i = ∆Hrxn,i + Q2i + ( ∆Hrxn,i )2
2 2
where
The objective is to maximize the amount of epoxide formed in a plug-flow-reactor (PFR) with a
diameter of 10mm and length of 15m. Recall the material balance equation for a PFR:
dFi X
=A Si,n rn
dz n
where Fi (z) is the moles of i fed per second passing through a cross-section at position z, A is the
cross-sectional area, S are the stoichiometric coefficients and r are the reaction rates.
The feed composition is 66 mole % alkene, 33 mole % O2 , 1 mole % catalyst, all in gas phase.
The total feed flow rate is 2.5 moles/second. P is 10 bar and pressure drop is negligible. Assume
the system is isothermal and you can vary the T, any T < 700 K is acceptable. We will also apply
a constraint that values of Hf (XO) and Hf (XOO) be greater than −300kJ/mol.
Hint: consider what values of Hf (XO) and Hf (XOO) will yield exothermic reactions in the
direction of the catalytic cycle that results in production of epoxide.
1. Write a function that returns the reaction rates rn based on the flow rates and the values of
T , Hf (XO), and Hf (XOO). Check to ensure that the units work out. Test your function
on the initial conditions for the nominal case where T = 650K, Hf (XO) = −30kJ/mol, and
Hf (XOO) = −80kJ/mol. Show the vector of activation energies used. Describe what your
function does. For vectors, use the reaction order r1 , r2,f wd , r2,rev , r3,f wd , and r3,rev .
4
2. Write the stoichiometry matrix S. Use the reaction order described above and for the com-
ponents, order them in the following manner: X, O2 , alkene, epoxide, XO, XOO. Write a
F (z=15m)
function that outputs the yield of epoxide epoxide
Falkene (z=0) given values of the parameters T ,
Hf (XO), and Hf (XOO). Use ode15s to solve the ODE. Describe what your function does.
3. Use MATLAB’s built in constrained minimizer fmincon to find the optimal T , Hf (XO), and
Hf (XOO) given the constraints in the problem. Write out the method you use to search
through the space and ensure you are not caught in a local minimum that may not be the
global minimum. To get good convergence, you may need to play with the tolerances for the
ODE solver and also fmincon parameters. Parameters that may need to be tweaked include
tolerances, algorithms, and finite difference calculation parameters. Describe how you do so
in detail.
4. Would using the method described in problem 2 applied to this problem have required more
or less function evaluations per iteration? Why or why not?
5
MIT OpenCourseWare
https://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms.