In [1]:
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[1]:
In [5]:
grid = [2, 8]
vals = [4, 5]
f = lin_interp(grid, vals)
f(7)
Out[5]:
In [6]:
Out[6]:
In [2]:
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[2]:
In [2]:
function lin_interp(grid, vals)
function func(x)
index_1 = searchsortedfirst(grid, x)
index_2 = searchsortedlast(grid, x)
x_1 = grid[index_1]
x_2 = grid[index_2]
y_1 = vals[index_1]
y_2 = vals[index_2]
y = ((y_2- y_1)/(x_2 - x_1))*(x - x_1) + y_1
return y
end
return func
end
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[2]:
In [3]:
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[3]:
In [4]:
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[4]:
In [13]:
function my_lin_interp(grid, vals)
function func(x::Real)
if x <= grid[1]
index_1 = 1
index_2 = 2
elseif x >= grid[length(grid)]
index_1 = length(grid) - 1
index_2 = length(grid)
else
index_1 = searchsortedfirst(grid, x)
index_2 = searchsortedlast(grid, x)
end
x_1 = grid[index_1]
x_2 = grid[index_2]
y_1 = vals[index_1]
y_2 = vals[index_2]
y = ((y_2- y_1)/(x_2 - x_1))*(x - x_1) + y_1
return y
end
function func{T<:Real}(x::AbstractVector{T})
y = zeros(length(x))
for i in 1:length(x)
y[i] = func(x[i])
end
return y
end
return func
end
Out[13]:
In [17]:
Pkg.clone("https://github.com/ellenjunghyunkim/MyInterpolations.jl")
In [18]:
Pkg.test("MyInterpolations")
In [11]:
Pkg.checkout("MyInterpolations", "master")
In [12]:
Pkg.update("MyInterpolations", "master")
In [8]:
Pkg.checkout("MyInterpolations", "master")
In [9]:
using MyInterpolations
In [10]:
Pkg.test("MyInterpolations")
In [ ]: