The Interpolating Natural Cubic Spline Given data { (xi , yi) i = 1 , 2 , ... ,
ID: 648924 • Letter: T
Question
The Interpolating Natural Cubic Spline Given data { (xi , yi) i = 1 , 2 , ... , n} , write an algorithm for computing the interpolating natural cubic spline. Use your tridiagonal matrix solver from Computing Assignment 1 to compute the array of m-values. Test your program on the data given in Example 4.2 of the previous chapter on the least-square method. In your report, include a plot of the resulting spline. Example 4.2. Consider the following m = 6 data values relating a variable y to a variable t. I 1 2 3 4 5 6 ti 0 0.50 1.00 1.30 2.00 3.00 Yi 0 1.60 2.00 1.93 1.06 0.38 Suppose the relation between t and y is assumed to be y = x1e^-t + x2e^-2t. Then there are n = 2 basis functions, b1(t) = e^-t and b2(t) = e^-2t . We thenExplanation / Answer
?
CSPL.evalSpline = function(x, xs, ys, ks)
{
var i = 1;
while(xs[i]<x) i++;
var t = (x - xs[i-1]) / (xs[i] - xs[i-1]);
var a = ks[i-1]*(xs[i]-xs[i-1]) - (ys[i]-ys[i-1]);
var b = -ks[i ]*(xs[i]-xs[i-1]) + (ys[i]-ys[i-1]);
var q = (1-t)*ys[i-1] + t*ys[i] + t*(1-t)*(a*(1-t)+b*t);
return q;
}
CSPL.getNaturalKs = function(xs, ys, ks)// in x values, in y values, out k values
{
var n = xs.length-1;
var A = CSPL._gaussJ.zerosMat(n+1, n+2);
for(var i=1; i<n; i++) // rows
{
A[i][i-1] = 1/(xs[i] - xs[i-1]);
A[i][i ] = 2 * (1/(xs[i] - xs[i-1]) + 1/(xs[i+1] - xs[i])) ;
A[i][i+1] = 1/(xs[i+1] - xs[i]);
A[i][n+1] = 3* ( (ys[i]-ys[i-1])/ ((xs[i] - xs[i-1])*(xs[i] - xs[i-1]))
+ (ys[i+1]-ys[i])/ ((xs[i+1] - xs[i])*(xs[i+1] - xs[i])) );
}
A[0][0 ] = 2/(xs[1] - xs[0]);
A[0][1 ] = 1/(xs[1] - xs[0]);
A[0][n+1] = 3 * (ys[1] - ys[0]) / ((xs[1]-xs[0])*(xs[1]-xs[0]));
A[n][n-1] = 1/(xs[n] - xs[n-1]);
A[n][n ] = 2/(xs[n] - xs[n-1]);
A[n][n+1] = 3 * (ys[n] - ys[n-1]) / ((xs[n]-xs[n-1])*(xs[n]-xs[n-1]));
CSPL._gaussJ.solve(A, ks);
}
Where A is n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.