Math 227 Lab 3, Splines and Nonlinear Data Fitting 10/8/2009
Contents
Splines
The syntax for using MATLAB's built-in spline function is very simple: First let's set up the data
x=1:10; y=sin(x);
Apply the spline at one point
xstar=pi; ystar=spline(x,y,xstar); fprintf('Using the spline, our approximation to sin(pi) is %5.3f.\n',ystar) fprintf('Of course we know sin(pi)=0.\n')
Using the spline, our approximation to sin(pi) is 0.002. Of course we know sin(pi)=0.
Apply the spline over an interval
xx=linspace(1,10,91); yy=spline(x,y,xx); figure(1) plot(x,y,'o',xx,yy,xstar,ystar,'p') xlabel('x') ylabel('y') legend('Original data','Smooth curve generated by spline','Spline at one point') figure(2); plot(xx,sin(xx)-yy) xlabel('x');title('difference between exact curve and spline interpolant')
Nonlinear data fitting
I would like to explain a few of the techniques used in
Here, we are implementing the methodology described in the photocopied pages by Mooney and Swift. I use a few Matlab functions that might be new to you.
- mesh: Draws a mesh plot of a function of two independent variables. Syntax: mesh(x,y,H) where x is a vector of length nx, y is a vector of length ny and h is an array of size nx by ny. The commands surf and contour work the same way. surf makes a solid surface rather than a mesh. contour makes a contour plot. You can also experiment with pcolor and contourf which have the same syntax and offer slightly different visualization of surfaces.
- colorbar: To help understand the output of contour, pcolor, or contourf
- fminsearch: Find the minimum of a function of several variables. Suppose we have a function h(x) that returns a scalar-value h, where x is a vector of length n. The basic syntax is:
x=fminsearch(@h,x0).
Note the @ sign before the h. This indicates that you are passing a function handle to the fminsearch routine. You don't need to understand too much about what that means. Just remember that you need it. If fminsearch succeeds, it will return the location x of a local minimum. (There might be a global minimum somewhere else, but just like in your calculus class, the best you can do is find all the local minima and see which one is the lowest afterward.)
The other input argument is x0, an initial guess for x0.
A more complete syntax for this command is
x=fminsearch(@h,x0,options,p1,p2,...). where options contains some options that change the way the program runs, and any remaining variables are listed in the order they appear in the function definition.
Our call params=fminsearch(@penalty_logistic,paramguess,[],t,M); takes a guess paramguess, followed by [] which is an empty vector of parameters, followed by t and M which are additional parameters needed by the program penalty_logistic.
Exercises
Use Matlab's built-in spline function for the first two exercises:
- Section 4.4 project 3. Draw a smooth curve between the data, and include the value of the spline for the year of your birth.
- Section 4.4 project 4. Draw a smooth spline curve and find, via experimenting with different values of t, the approximate year when the population reached 1000.
- NIST, the National Institute of Standards and Technology maintains a list of experimental datasets for scientists and students to use in learning and testing data-fitting programs here. Download the data set Misra1c and find the optimal parameter values beta=[b1 b2] for models of the form

Note that the first column of data is y not x. In this example, do not use the trick of eliminating
as is done in the Mooney ans Swift handout. Go through all the steps in my algae example and compute
for your result. Plot your answer and the residual and comment on the effectiveness of your approximation.
4. Now consider the NIST data set Eckerle4. Fit this data to a function of the form

Use the trick from the handout to eliminate the parameter
and proceed as in the algae example to find all three parameters
,
, and
. Also calculate
for your answer. Plot your answer and the residual and comment on the effectiveness of your approximation.