Group Abstract Group Abstract

Message Boards Message Boards

0
|
9.7K Views
|
9 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Solving Cubic Splines Symbolically

Posted 11 years ago
9 Replies
Posted 10 years ago

Explicit procedural loops are inefficient in Mathematica. You should avoid them and use functional programming style instead.

I have a matrix of 5 columns and 100 rows with the 5 columns giving the interval values for the cubic spline fit and the 100 rows being the 100 cubic spline fits I would like stored for. I would like a loop that would take the 5x100 row matrix and output the parameters for the 100 fits.

There is unique clamped cubic spline interpolating 5 points which consists of (5-3)=2 cubic parabolas. You can get them in explicit form if you apply PiecewiseExpand to the sp2[x] function defined in this excellent answer.

POSTED BY: Alexey Popkov
Posted 11 years ago
POSTED BY: Alexey Popkov

Yes, you can do a piecewise definition, or else you can combine two separate plots using Show:

F1[t_] = k1 + k2*t + k3*t^2 + k4*t^3;
F2[t_] = k5 + k6*t + k7*t^2 + k8*t^3;
Data = Solve[{F1[0] == 0, F1[5] == 3, F2[5] == 3, F1'[5] == F2'[5], 
    F1''[5] == F2''[5], F2[15] == 11, F1'[0] == 0, F1''[0] == 0}];
{F1[t], F2[t]} /. Data
Show[Plot[F1[t] /. Data, {t, 0, 5}, PlotStyle -> Red],
 Plot[F2[t] /. Data, {t, 5, 15}, PlotStyle -> Blue], PlotRange -> All]
POSTED BY: Gianluca Gorni

You can tell Solve what variables you want to solve for:

sol = First@Solve[{F1[0] == Fi1, F1[1] == Fi2, F2[1] == Fi2, 
  F1'[1] == F2'[1]}, {k1, k2, k3, k4, k5, k6, k7, k8}]

You cannot plot the solutions directly, because some constants are still symbolic:

{F1[t], F2[t]} /. sol

You can assign all the remaining parameters the value 1, for example, and then plot the functions:

{F1[t], F2[t]} /. sol /. 
 Thread[{Fi1, Fi2, k1, k2, k3, k4, k5, k6, k7, k8} -> 1]
Plot[%, {t, 0, 2}]
POSTED BY: Gianluca Gorni

Do you know how I would have it solve for the k's instead of the Fi's? Also how could I plot these two functions and save the k's?

I am not sure I understand your question, but perhaps you will get started with this:

Clear[F1, F2, k1, k2, k3, k4, k5, k6, k7, k8];

F1[t_] = k1 + k2*t + k3*t^2 + k4*t^3;
F2[t_] = k5 + k6*t + k7*t^2 + k8*t^3;

Solve[{F1[0] == Fi1, F1[1] == Fi2, F2[1] == Fi2, F1'[1] == F2'[1]}]
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard