#線形補間 課題1 ##鈴木大志


In [1]:
println(readall("lin_interp2.jl"))


immutable lin_interp2
    grid::Array
    vals::Array
end

function Base.call(points::lin_interp2, x::Real)
    i = searchsortedlast(grid, x)
    if i == 0 || i == length(grid)
        return 0
    end
    x_i = points.grid[i]
    x_j = points.grid[i + 1]
    y_i = points.vals[i]
    y_j = points.vals[i + 1]
        
    y = y_i + (y_j - y_i) * ((x - x_i) / (x_j - x_i))
    return y
end

function Base.call{T<:Real}(points::lin_interp2, x::AbstractVector{T})
    n = length(x)
    out = Array(Float64, n)
    for t in 1:n
        out[t] = points(x[t])
    end
    return out
end

In [2]:
include("lin_interp2.jl")


Out[2]:
call (generic function with 1102 methods)

#sin(x)の線形補間と残差


In [3]:
using PyPlot
grid = [-8, -4, -2, 0, 2, 4, 6, 8]
vals = sin(grid)
points = lin_interp2(grid, vals)
x = [-10:0.01:10]
residual(x) = sin(x) - points(x)
plot(points(x))
plot(sin(x))
plot(residual(x))


WARNING: [a] concatenation is deprecated; use collect(a) instead
 in depwarn at deprecated.jl:73
 in oldstyle_vcat_warning at abstractarray.jl:29
 in vect at abstractarray.jl:32
 in include_string at loading.jl:282
 in execute_request_0x535c5df2 at C:\Users\taishi\.julia\v0.4\IJulia\src\execute_request.jl:183
 in eventloop at C:\Users\taishi\.julia\v0.4\IJulia\src\IJulia.jl:143
 in anonymous at task.jl:447
while loading In[3], in expression starting on line 5
Out[3]:
1-element Array{Any,1}:
 PyObject <matplotlib.lines.Line2D object at 0x0000000029E5E588>

In [ ]: