In [15]:
function g(x)
grid = [2, 3]
vals = [4, 6]
interpolated_value = (vals[2]-vals[1])/(grid[2]-grid[1])*(x-grid[1])+vals[1]
return interpolated_value
end
Out[15]:
In [ ]:
In [16]:
print(g(2.5))
In [17]:
grid = [-2, 1, 4, 6, 7, 10]
println(grid)
searchsortedlast(grid, 5.5)
Out[17]:
In [21]:
function h(grid, vals, x)
i = searchsortedlast(grid, x)
if i == 0 || i == length(grid)
return 0
end
interpolated_value = (vals[i+1]-vals[i])/(grid[i+1]-grid[i])*(x-grid[i])+vals[i]
return interpolated_value
end
Out[21]:
In [22]:
grid = [0, 2, 4, 6, 8, 10]
vals = [1, 4, 5, 8, 9, 11]
println(h(grid, vals, 3))
In [24]:
function ellenpola(grid, vals)
function func(x)
i = searchsortedlast(grid, x)
if i == 0 || i == length(grid)
return 0
end
interpolated_value = (vals[i+1]-vals[i])/(grid[i+1]-grid[i])*(x-grid[i])+vals[i]
return interpolated_value
end
return func
end
Out[24]:
In [25]:
grid = [0, 2, 4, 6, 8, 10]
vals = [1, 4, 5, 8, 9, 11]
my_func = ellenpola(grid,vals)
my_func(3)
Out[25]:
In [26]:
my_func(7)
Out[26]:
In [21]:
function ellenpola(grid,vals)
function func(x)
i = searchsortedlast(grid, x)
if i == 0 || i == length(grid)
return AbstractVector
else
interpolated_value = (vals[i+1]-vals[i])/(grid[i+1]-grid[i])*(x-grid[i])+vals[i]
return interpolated_value
end
return func
end
end
Out[21]:
In [22]:
grid = [0, 2, 4, 6, 8, 10]
vals = [1, 4, 5, 8, 9, 11]
my_func = ellenpola(grid,vals)
println(my_func(1))
println(my_func(7))
println(my_func(11))
In [2]:
function ellenpola(grid, vals)
function func(x)
i = searchsortedlast(grid, x)
if i == 0 || i == length(grid)
return 0
end
interpolated_value = (vals[i+1]-vals[i])/(grid[i+1]-grid[i])*(x-grid[i])+vals[i]
return interpolated_value
end
function func{T<:Real}(x::AbstractVector{T})
n = length(x)
out = Array(Float64, n)
for i in 1:n
i = searchsortedlast(grid, x)
if i == 0 || i == length(grid)
interpolated_value = 0
else
interpolated_value = (vals[i+1]-vals[i])/(grid[i+1]-grid[i])*(x-grid[i])+vals[i]
end
out[i] = interpolated_value
end
return out
end
end
Out[2]:
In [10]:
module MyLinInterp
export LinearInterpolation
immutable LinearInterpolation
grid::Array
vals::Array
end
function call(a::LinearInterpolation, x::Real)
i = searchsortedlast(grid, x)
if i == 0 || i == length(grid)
return 0
end
interpolated_value = (vals[i+1]-vals[i])/(grid[i+1]-grid[i])*(x-grid[i])+vals[i]
return interpolated_value
end
function call{T<:Real}(a::LinearInterpolation, x::AbstractVector{T})
n = length(x)
out = Array(Float64, n)
for i in 1:n
i = searchsortedlast(grid, x)
if i == 0 || i == length(grid)
interpolated_value = 0
else
interpolated_value = (vals[i+1]-vals[i])/(grid[i+1]-grid[i])*(x-grid[i])+vals[i]
end
out[i] = interpolated_value
end
return out
end
end
Out[10]:
In [1]:
Pkg.add(MyInterpolations)
In [2]:
Pkg.checkout("MyInterpolations", "master")
In [6]:
Pkg.rm("MyInterpolations")
In [7]:
Pkg.rm("MyInterpolations")
In [8]:
Pkg.update()
In [ ]: