|
CUBESPLX.OMS
Script File:
# Description:
# Fits and evaluates a cubic spline.
#
clear
# define the function to interpolate
function f(x) begin
return cos(x)
end
#
# number of data pairs
Nd = 7
# number of interpolation points
Ni = 31
# end of the interval
xend = 2 * pi
# x values for the data pairs
xdata = xend * (seq(Nd) - 1) / real(Nd - 1)
# y value for the data pairs
ydata = f(xdata)
# Value of the second derivative of f at the end points
dd1 = - cos(0.)
ddn = - cos(xend)
# compute spline coefficients
abc = cubespl(xdata, ydata, dd1 / 2, ddn / 2)
# new grid to evaluate spline on
xint = xend * (seq(Ni) - 1) / real(Ni - 1)
# values for the spline on new grid
yint = cubeval(xdata, ydata, abc, xint)
# plot the interpolated values as a line
gplot(xint, yint, "solid")
# plot the data values as symbols
gplot(xdata, ydata, "cross")
# title for the plot
gtitle("Cubic Spline of Cosine")
Output:
|
|