This is to be a C++ program. No other language using the following steps and alg
ID: 3865838 • Letter: T
Question
This is to be a C++ program. No other language using the following steps and algorithm:
To construct the cubic spline interpolant S for the function f , defined at the numbers
x0 < x1 < · · · < xn, satisfying S(x0) = S(xn) = 0:
INPUT n; x0, x1, . . . , xn; a0 = f (x0), a1 = f (x1), . . . , an = f (xn).
OUTPUT aj , bj , cj , dj for j = 0, 1, . . . , n 1.
(Note: S(x) = Sj(x) = aj + bj(x xj) + cj(x xj)2 + dj(x xj)3 for xj x xj+1.)
Step 1: For i = 0, 1, . . . , n 1 set hi = xi+1 xi.
Step 2: For i = 1, 2, . . . , n 1 set
i = 3
hi
(ai+1 ai) 3
hi1
(ai ai1).
Step 3: Set l0 = 1; (Steps 3, 4, 5, and part of Step 6 solve a tridiagonal linear system
using a method described in Algorithm 6.7.)
0 = 0;
z0 = 0.
Step 4: For i = 1, 2, . . . , n 1
set li = 2(xi+1 xi1) hi1i1;
i = hi/li;
zi = (i hi1zi1)/li.
Step 5: Set ln = 1;
zn = 0;
cn = 0.
Step 6: For j = n 1, n 2, . . . , 0
set cj = zj jcj+1;
bj = (aj+1 aj)/hj hj(cj+1 + 2cj)/3;
dj = (cj+1 cj)/(3hj).
Step 7: OUTPUT (aj , bj , cj , dj for j = 0, 1, . . . , n 1);
STOP
Explanation / Answer
#include #include #include #include "spline.h" int main(int argc, char** argv) { std::vector X(5), Y(5); X[0]=0.1; X[1]=0.4; X[2]=1.2; X[3]=1.8; X[4]=2.0; Y[0]=0.1; Y[1]=0.7; Y[2]=0.6; Y[3]=1.1; Y[4]=0.9; tk::spline s; s.set_points(X,Y); // currently it is required that X is already sorted double x=1.5; printf("spline at %f is %f ", x, s(x)); return EXIT_SUCCESS; }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.