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

my algae example

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.

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:

  1. 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.
  2. 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.
  3. 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

$$\frac{\beta_1 \beta_2 x}{1+\beta_2 x}.$$

Note that the first column of data is y not x. In this example, do not use the trick of eliminating $\beta_1$ as is done in the Mooney ans Swift handout. Go through all the steps in my algae example and compute $R^2$ 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

$$ K e^{-\frac{1}{2}(\frac{x-x_0}{\sigma})^2}$$

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