You are on page 1of 5

CHEN 5128 / MCEN 5146

Applied Statistics in
Fall 2004
Research and Development
Name: _________________________

Workshop for Class #26

Parameter Estimation in Ordinary Differential Equation Models Using Matlab


The numerical solution of ordinary differential equations is generally required as part of the least
squares parameter estimation process. This is often feasible using Excel, but not that convenient.
Also, there are concerns about the accuracy of the numerical results. It is possible to couple more
advanced numerical methods into Excel, via links to Matlab, Fortran or C/C++ library routines. It is
also convenient to carry out the entire estimation task in Matlab, which is what you will do here.
1. Log on to one of the computers in the HP Lab Plaza. Launch Matlab. As in the last workshop, in
this workshop, you will solve Problem 9.7 on page 326 of the Himmelblau supplement. Review
the problem statement once again.
Launch Matlab 7 and, when you see the command prompt,

, change the default directory path

and
to My Documents (or Z:\ ). You can do this conveniently by clicking the browse button
selecting the My Documents folder or by typing Z:\ directly into the Current Directory: field.
At the Matlab command prompt, enter the experimental data as column vectors. This is shown
below.
t=[72 144 216 288 346 432]';
yobs=[195 377 542 687 783 911]';
Create a graph of the data with the plot commands:
plot(t,yobs,'ko')
grid
axis([ 0 500 0 1000 ])
title(' <your groups names> ')
xlabel('t')
ylabel('y')
Again, what initial condition is apparent from the graph? y ( t = 0 ) = _____________
Expand the time vector to include initial time and create an initial

y value.

tspan = [ 0 ; t ];
y0 = <your initial value for y>;
2. The first task you will accomplish is to solve the differential equation numerically, retaining solution
values at the times in the tspan vector. Matlab has a collection of functions to solve ordinary
differential equations. You will use a conventional one here called ode45. The ode45 function
has automatic control of integration step size to guarantee accuracy of the numerical solution.
You will not know what step-sizes it takes, but it will report out the solution at the measurement
times (or any other set of times you specify).
To use the ode45 function, you must create a Matlab user-defined function that computes the
derivative specified by the differential equation of the problem in question. To do this, To do this,
open a new m-file with the button on the left end of the toolbar in the command window,

CHEN 5128 / MCEN 5146

Class 26 Workshop

Page 2

In the Editor window, enter the following short Matlab code:


function dy = logistic(t,y,flag,a,b1,b2)
dy = b1/b2 * (y a) * (b2 (y a));
and save this file as logistic.m on the desktop.
Back in the Matlab Command window, enter the following commands to solve the differential
equation using ode45:
a=5000;
b1=-1e-5;
b2=125;
[t ymodel]=ode45('logistic',tspan,y0,[],a,b1,b2);
Then add the solution to your plot1 with the commands
hold on
plot(t,ymodel,'k--')
Observe the plot. It is not convenient in Matlab to have a live plot as you did in Excel for tuning
up the model parameters.
3. Next, you will create a Matlab function that uses the ode45 function to compute the sum-squareof-errors performance index for a given set of parameter estimates.
Switch over to the Matlab editor and open a new file. Enter the following commands there:
function val=SSE(k)
a=k(1);
b1=k(2);
b2=k(3);
tspan=[0 72 144 216 288 346 432]';
yobs=[<your initial value for y> 195 377 542 687 783 911]';
y0=<your initial value for y>;
[t ymodel]=ode45('logistic',tspan,y0,[],a,b1,b2);
resid = ymodel-yobs;
val = resid'*resid;
Save this file as SSE.m on the desktop.
Back in the Command Window, test this new function with your current parameter values:
SSE([a b1 b2])
and it should display the SSE value for that set. What is this value? ________________

Note: the solution will not look very continuous because it was only generated at your
measurement points.

CHEN 5128 / MCEN 5146

Class 26 Workshop

Page 3

4. Now, you will use this SSE.m function with one of Matlabs minimization functions, fminsearch, to
solve the nonlinear regression. You can execute this function from the Matlab Command Window.
k=[a b1 b2];
options=optimset('MaxFunEvals',10000,'MaxIter',10000);
[k SSEmin]=fminsearch('SSE',k)
It may take some time for this command to complete. This is a difficult optimization because the
surface has a very steep valley. This property seems to be typical in nonlinear regression.
If successful, the function should report out the optimal parameter estimates and the SSE value. If
the algorithm stops at some limit, you can restart it from the current parameter values by reissuing
(use the key) the command
[k SSEmin]=fminsearch('SSE',k)
Observe the reduction in SSE value. You can examine the values of the parameters, k, after each
optimization attempt. It may be convenient for you to change Matlabs numerical display format
for this by entering the command:
format long e
If you restart the process one or more times, stop when the SSE value isnt changing much.
Map the parameters back to the original variables:
a = k(1);
b1 = k(2);
b2 = k(3);
What are these values?
a __________

b1 __________

b2 __________

SSE __________
Create a new plot of the data and fitted model with
figure(2)
[t ymodel]=ode45('logistic',tspan,y0,[],a,b1,b2);
yobs = [<your initial value for y> ; yobs];
plot(t,yobs,'ko',t,ymodel,'k--')
grid
title(' <your groups names> ')
xlabel('t')
ylabel('y')
5. As with Excel, it is useful to study the optimization surface, either in the vicinity of the solved
optimum or elsewhere in order to help find your way. This can be done conveniently in Matlab.
Do this for the a/b1 parameter pair here. Start by creating two vectors of 11 values for these
parameters in the vicinity of your determined optimum.
For example, if your a value were 2000 and b1 value -0.002 :

CHEN 5128 / MCEN 5146

Class 26 Workshop

Page 4

av=linspace(198,2020,11);
adjust these to suit your
b1v=linspace(-0.00205,-0.00195,11);
values
Then form meshgrid matrices with
[ A B1 ] = meshgrid(av,b1v);
and compute the SSE values for each of the 121 points of the grid
for i=1:11
for j=1:11
SSEMat(i,j)=SSE([A(i,j) B1(i,j) b2]);
end
end
With these in hand, try a few plots:
figure(3)
surfc(A,B1,SSEMat)
figure(4)
contourf(A,B1,SSEMat)
colorbar
Examine these figures. Use the rotate feature on the surface plot.

6. In the last step of the exercise, you will compute estimated confidence intervals for the model
parameters. In the past, to do this, we have formulated an X matrix based directly on analytical
derivations of the model. This is not generally possible for differential equation models. Instead,
we approximate the X matrix numerically. For example, the i, j
be approximated by

y
j

xi

th

element of the X matrix would

y ( xi , j + ) y ( xi , j )
2

This implies that we must solve the model several times for perturbations of the parameter
estimates. This is easy to do with the Matlab functions we have already built.
Start by computing appropriate values for the different parameters. A typical value would be
1% of the parameter value or less.
ad=0.01*a;
b1d=0.01*b1;
b2d=0.01*b2;
Now, use ode45 to compute sets of y values for the different perturbed parameters:
[t
[t
[t
[t
[t
[t

yap]=ode45('logistic',tspan,y0,[],a+ad,b1,b2);
yam]=ode45('logistic',tspan,y0,[],a-ad,b1,b2);
yb1p]=ode45('logistic',tspan,y0,[],a,b1+b1d,b2);
yb1m]=ode45('logistic',tspan,y0,[],a,b1-b1d,b2);
yb2p]=ode45('logistic',tspan,y0,[],a,b1,b2+b2d);
yb2m]=ode45('logistic',tspan,y0,[],a,b1,b2-b2d);

CHEN 5128 / MCEN 5146

Class 26 Workshop

Page 5

and compute the X matrix from these:


X=[(yap-yam)/2/ad (yb1p-yb1m)/2/b1d (yb2p-yb2m)/2/b2d];
T
Then, it's easy to compute the X X

XtXinv=inv(X'*X);

matrix:
%Note: Matlab may provide a warning here.

and a few other quantities:


n=length(tspan);
p=3;
SE=sqrt(SSEmin/(n-p));
Sa=SE*sqrt(XtXinv(1,1));
Sb1=SE*sqrt(XtXinv(2,2));
Sb2=SE*sqrt(XtXinv(3,3));
alpha=0.05;
% tval=tinv(1-alpha/2,n-p);
am=a-tval*Sa
ap=a+tval*Sa
b1m=b1-tval*Sb1
b1p=b1+tval*Sb1
b2m=b2-tval*Sb2
b2p=b2+tval*Sb2
The last 6 values represent the estimates of the 95%-confidence limits for the model parameters.
Complete the following table:
_____________ a _____________
_____________ b1 _____________
_____________ b2 _____________
What do you observe about these intervals?

7. Make print-outs of your Figure 1 and Figure 2 windows and attach them to this document.
Close out Matlab and log off the computer.

You might also like