Professional Documents
Culture Documents
Applied Statistics in
Fall 2004
Research and Development
Name: _________________________
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,
Class 26 Workshop
Page 2
Note: the solution will not look very continuous because it was only generated at your
measurement points.
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 :
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
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);
Class 26 Workshop
Page 5
XtXinv=inv(X'*X);
matrix:
%Note: Matlab may provide a warning here.
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.