Description Usage Arguments Value Examples
Provides very fast 1D linear interpolation.  Typically about 10 times faste than R's built-in approx function.
| 1 | 
| x | A vector of x-values | 
| y | A vector of y-values | 
| pt | An x-value | 
| n_x | An integer.  The length of  | 
Returns the linear interpolation of (x,x) evaluated at x=pt.  That is, if x= (x_1, x_2, …, x_n), y= (y_1, y_2, …, y_n), and pt= x,  x_i < x ≤ x_{i+1}, then the output is:
y = ≤ft( \frac{x - x_i}{x_{i+1} - x_i} \right) y_i + ≤ft( 1 - \frac{x - x_i}{x_{i+1} - x_i} \right) y_{i+1}
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | ## 1D Interpolation
f.1 <- function(x) x^2
n <- 5
x.grid <- seq( 0, 10, length.out = n )
f.1.grid <- sapply( x.grid, f.1 )
lin( x.grid, f.1.grid, 3, n ) == 3
    # Check interpolation
x.grid.fine <- seq( 0, 10, length.out = n^2 )
plot( x.grid.fine, sapply( x.grid.fine, f.1 ),
      main='Exact and linear interpoliation of x^2', type='l' )
lines( x.grid.fine, sapply( x.grid.fine, lin, x=x.grid, y=f.1.grid, n_x=n ), 
        col='red' )
    # Compare the exact and approximate solution
library(microbenchmark)
microbenchmark(sapply( x.grid.fine, lin, x=x.grid, y=f.1.grid, n_x=n ))
microbenchmark(sapply( x.grid.fine, approx, x=x.grid, y=f.1.grid ))
    # lin is about 10x faster than R's built-in approx function
 | 
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.