mkpp(x, P)ppval(pp, xx)
mkpp
.mkpp
will return a piecewise polynomial structure, that is a list
with components breaks=x
, pieces=P
, order=k
and
dim=1
for scalar-valued functions.pp<-mkpp(x,P)
builds a piecewise polynomial from its breaks
x
and coefficients P
. x
is a monotonically increasing
vector of length L+1
, and P
is an L-by-k
matrix where
each row contains the coefficients of the polynomial of order k
, from
highest to lowest exponent, on the interval [x[i],x[i+1])
. ppval(pp,xx)
returns the values of the piecewise polynomial
pp
at the entries of the vector xx
. The first and last
polynomial will be extended to the left resp. right of the interval
[x[1],x[L+1])
.
cubicspline
## Example: Linear interpolation of the sine function
xs <- linspace(0, pi, 10)
ys <- sin(xs)
P <- matrix(NA, nrow = 9, ncol = 2)
for (i in 1:9) {
P[i, ] <- c((ys[i+1]-ys[i])/(xs[i+1]-xs[i]), ys[i])
}
ppsin <- mkpp(xs, P)
plot(xs, ys); grid()
x100 <- linspace(0, pi, 100)
lines(x100, sin(x100), col="darkgray")
ypp <- ppval(ppsin, x100)
lines(x100, ypp, col="red")
Run the code above in your browser using DataLab