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

**URL:** <https://discourse.julialang.org/t/how-to-create-a-function-that-returns-a-vector-of-interpolated-values/52424>\
**Category:** New to Julia\
**Created:** [December 26, 2020, 10:00pm UTC](https://discourse.julialang.org/t/how-to-create-a-function-that-returns-a-vector-of-interpolated-values/52424 "2020-12-26T22:00:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Williams](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_williams/32/7668_2.png) [@Mark\_Williams](https://discourse.julialang.org/u/Mark_Williams)\
**Post date:** [December 26, 2020, 10:00pm UTC](https://discourse.julialang.org/t/how-to-create-a-function-that-returns-a-vector-of-interpolated-values/52424/1 "2020-12-26T22:00:43Z")

</div>

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!

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![AndiMD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andimd/32/6531_2.png) [@AndiMD](https://discourse.julialang.org/u/AndiMD)\
**Post date:** [December 26, 2020, 10:21pm UTC](https://discourse.julialang.org/t/how-to-create-a-function-that-returns-a-vector-of-interpolated-values/52424/2 "2020-12-26T22:21:33Z")

</div>

Hi,  
how about this:

```julia
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.

---

<div class="post-metadata">

**Author:** ![Mark\_Williams](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_williams/32/7668_2.png) [@Mark\_Williams](https://discourse.julialang.org/u/Mark_Williams)\
**Post date:** [December 26, 2020, 10:27pm UTC](https://discourse.julialang.org/t/how-to-create-a-function-that-returns-a-vector-of-interpolated-values/52424/3 "2020-12-26T22:27:29Z")

</div>

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