Does this give you what you want:

function make_interp(x, y)
    y = copy(y)
    function interp(x2)
        @assert x2 > x[1] && x2 < x[end]
        i = 1
        while x[i] < x2; i += 1; end
        (x[i] - x2) * y[i-1] + (x2 - x[i-1]) * y[i]
    end
end

x = [1,2,3,4,5]
y = [x2^2 for x2 in x]

my_interp = make_interp(x, y)
println(my_interp(2.1))  # prints 4.5
y[2] = 5
println(my_interp(2.1))  # prints 4.5


On Tue, Feb 25, 2014 at 4:01 PM, Marek Gagolewski
<[email protected]> wrote:
> Dear Andrew,
>
> Nope, unfortunately it's not what I am trying to achieve. The code you've
> kindly (let's forget the interpolation task, what I really meant is some
> programming construct, so it's OK with no delta_x) submitted gives:
>
> my_interp = make_interp(x, y)
> println(my_interp(2.1))  # prints 4.5
>
> y[2] = 5
> println(my_interp(2.1))  # prints 5.4
>
> and I also would like to get 4.5 in the second case.
>
> All the best,
> Marek

Reply via email to