In [1]:
?linspace
Out[1]:
In [2]:
?collect
Out[2]:
In [3]:
collect(linspace(0, 1, 9))
Out[3]:
In [4]:
?fill
Out[4]:
In [5]:
?vcat
Out[5]:
In [6]:
?cumsum
Out[6]:
In [7]:
A = [2, 3]
cumsum(A, 1)
Out[7]:
In [11]:
[2, 3], [[2, 3]]
Out[11]:
In [12]:
?minimum
Out[12]:
In [14]:
minimum(linspace(0, 1, 10))
Out[14]:
In [15]:
?AbstractVector
Out[15]:
In [1]:
zeros(10)
Out[1]:
In [2]:
?push!
Out[2]:
In [3]:
push!([], 2, 3)
Out[3]:
In [4]:
push!([2, 3], 4, 5)
Out[4]:
In [6]:
?zeros
Out[6]:
In [7]:
zeros(10, 2)
Out[7]:
In [12]:
zeros(2, 2)
Out[12]:
In [14]:
?copy!
Out[14]:
In [15]:
?vcat
Out[15]:
In [42]:
ygrid0 = linspace(-4, 4, 10)
Out[42]:
In [43]:
using BasisMatrices
# 1st method
y_basis = Basis(ChebParams(length(ygrid0), minimum(ygrid0), maximum(ygrid0)))
Out[43]:
In [44]:
S, (ygrid) = nodes(y_basis);
In [45]:
Φ = BasisMatrix(y_basis, Expanded(), S, 0)
Out[45]:
In [46]:
# Actual function at interpolation nodes
f(x::Vector{Float64}) = 1./(1 .+ 25x.^2)
y = f(S[:,1])
# Get coefficients
c = Φ.vals[1] \ y;
In [47]:
using QuantEcon
ygridf = linspace(-4, 4, 100)
Sf = gridmake(ygridf)
yf = f(Sf[:, 1])
interp = funeval(c, y_basis, Sf);
In [48]:
using Plots
plotlyjs()
plt_a = plot(ygridf, [interp[1:length(ygridf)] yf[1:length(ygridf)]],
label=["Interpolant", "Original"]',
lw=2,
xlabel="a")
Out[48]:
In [1]:
using QuantEcon
In [2]:
#Approximated Function
f(x::Array{Float64, 1}) = 1./(1 .+ 25x.^2)
Out[2]:
In [3]:
c_grid = linspace(-1, 1, 1000)
c_basis = Basis(ChebParams(length(c_grid), minimum(c_grid), maximum(c_grid)))
#Approximant
c_S, (c_grid) = nodes(c_basis)
c_phi = BasisMatrix(c_basis, Direct(), c_S)
y = f(c_S[:, 1])
c = c_phi.vals[1] \ y
c_interp = funeval(c, c_basis, c_S)
#Plot
ylims = (-1.1, 1.1)
plot(c_grid, c_interp, ylims=ylims, leg=false)
In [ ]: