0
votes

I am using matlab curve fitting toolbox to fit a curve to my data. I've used cubic spline, in the plot it's fitting well, but I would like to see the equation used. Is there a way to do that?

Also, is there a way to show the code? I mean the code was used to fit the curve by the toolbox?

Edit: I was able to get the code as the following, file->generate code. But still I need to find the equation, can anyone please tell me how I can do that?

Thank you.

1

1 Answers

1
votes

I'm not sure how to display any equations on the graph, but you should be able to replicate the cubic spline interpolation using the commands spline and unmkpp.

% returns the piecewise polynomial form of the cubic spline interpolant
pp = spline(x,Y)
% use unmkpp(pp) to get the piecewise polynomial details
[breaks,coefs,l,k,d] = unmkpp(pp)

Note that you will have a set of coefficients for each piece in the piecewise polynomial. For example:

x = -4:4;
y = [0 .15 1.12 2.36 2.36 1.46 .49 .06 0];
% cs stores the piecewise polynomial
cs = spline(x,[0 y 0]);
% extract the coefficients
[breaks,coefs,l,k,d] = unmkpp(cs)
% the endpoints of each of the polynomial pieces
breaks =
  -4  -3  -2  -1   0   1   2   3   4
% 8 sets of coefficients (each set of 4 coefficients for one polynomial piece)
coefs =
   0.20344  -0.05344   0.00000   0.00000
  -0.09033   0.55689   0.50344   0.15000
  -0.39211   0.28589   1.34622   1.12000
   0.14878  -0.89045   0.74167   2.36000
   0.13699  -0.44411  -0.59289   2.36000
   0.13325  -0.03313  -1.07012   1.46000
  -0.05998   0.36661  -0.73663   0.49000
  -0.06334   0.18668  -0.18334   0.06000
% the number of pieces is 8
l =  8
% order is 4 (so 4 coefficients)
k =  4
d =  1
% plot the interpolation
xx = linspace(-4,4,101);
plot(x,y,'o',xx,ppval(cs,xx),'-');