How to create a function that returns a vector of interpolated values

I currently trying to generate a function which will allow me to evaluate multiple linear interpolation functions at a single point and generate a vector of resulting values. The x values are the same for each function I wish to interpolate. The y data is given by a matrix of values of which each row represents a single functions data. I can write a function in which I manually generate each interpolation function and evaluate them at a particular point. However, I cant seem to generate the interpolation functions iteratively for any given rows of a matrix. My application has 18 rows of data so I don’t really want to do this manually. Sorry if this is a basic question. I’m still learning. Please see the example below. Thanks!

using Interpolations 
function create_interp_func(grid, values)
    vf_g = LinearInterpolation(grid, values[1,:], extrapolation_bc = Line());
    vf_b = LinearInterpolation(grid, values[2,:], extrapolation_bc = Line());
    vf = function(x) return [vf_g(x), vf_b(x)] end 
    return vf
end

g = range(0,length=10,1);
vals = zeros(2,10);
vals[1,:] = sin.(grid);
vals[2,:] = cos.(grid);

f = create_interp_func(g, vals)
f(0.26493543)

Hi,
how about this:

function create_interp_func(grid, vals)
    itps = eachrow(vals) .|> v->LinearInterpolation(grid, v, extrapolation_bc = Line())
    return x->[itp(x) for itp∈itps]
end

f = create_interp_func(grid, vals)
f(0.2)

EDIT: I suggest not to use the variable name values, its a predefined name in Julia.

2 Likes

Ah perfect, exactly what I’m looking for. Many thanks for the help and I’ll make note not to use the name values.

2 Likes