Create a User-Defined Interpolation Function Using approxfun

In this method to create a user-defined interpolation function, the user needs to call the approxfun() function and passed the required parameters to get the interpolation accordingly in the R programming language.

Example:

In this example, we will be creating the vector accordingly and passing this given vector to the approxfun function, and plotting the plot of the x and y points with the curve given by the approxfun function in the R programming language.

R




# Create vector
x <- c(1,8,9,4,7,6,5)    
y <- c(0,6,5,1,4,7,9)  
  
# Apply approx function
data_approxfum <- approxfun(x, y)       
data_approxfum    
  
# Draw output of approx function
plot(x, y)                        
curve(data_approxfum, add = TRUE)


Output:

function (v) 
.approxfun(x, y, v, method, yleft, yright, f, na.rm)
<bytecode: 0x000001bf4ba22068>
<environment: 0x000001bf4d2c0408>

 



Interpolation Functions in R

In this article, we will be looking towards the approx() and the aproxfun() interpolation function with working examples in the R Programming language.

Similar Reads

Approx() and Approxfun() interpolation function

These functions return a list of points that linearly interpolates given data points, or a function performing the linear (or constant) interpolation....

Method 1: Apply approx Function to Two Coordinates

In this method, we will be applying the approx function to two coordinates using the approx function passed with the given vector as its parameters to get a list of points which interpolate given two different data points....

Method 2: Apply approx Function to Multiple Coordinates

...

Method 3: Create a User-Defined Interpolation Function Using approxfun

In this method to apply an approx function to multiple coordinates, the user has to create the vectors containing multiple coordinates which further have to be passed as done in the above approach function which will be returning the linearly interpolates a list of the vector passed to the user....