Function Approximation


In [1]:
using BasisMatrices
using Plots

In [2]:
plotlyjs()


Out[2]:
Plots.PlotlyJSBackend()

Chebychev polynomials


In [3]:
n = 10
a, b = 0, 1
basis1 = Basis(ChebParams(n, a, b))


Out[3]:
1 dimensional Basis on the hypercube formed by [0.0] × [1.0].
Basis families are BasisMatrices.Cheb

In [4]:
xgrid = collect(linspace(a, b, 101))
ys = []
m = 9
for i in 1:m
    c = zeros(n)
    c[i] = 1
    y = funeval(c, basis1, xgrid)
    push!(ys, y)
end
ylims = (-1.1, 1.1)
plot(xgrid, ys, layout=m, ylims=ylims, leg=false)


Plotly javascript loaded.

To load again call

init_notebook(true)

[Plots.jl] Initializing backend: plotlyjs
Out[4]:

Linear Spline


In [5]:
n = 10
a, b = 0, 1
basis2 = Basis(LinParams(n, a, b))


Out[5]:
1 dimensional Basis on the hypercube formed by [0.0] × [1.0].
Basis families are BasisMatrices.Lin

In [6]:
xgrid = collect(linspace(a, b, 101))
ys = []
m = 9
for i in 1:m
    c = zeros(n)
    c[i] = 1
    y = funeval(c, basis2, xgrid)
    push!(ys, y)
end
ylims = (-1.1, 1.1)
plot(xgrid, ys, layout=m, ylims=ylims, leg=false)


Out[6]:

Cubic Spline


In [7]:
breaks = [a, b]
evennum = 7
k = 3
basis3 = Basis(SplineParams(breaks, evennum, k))


Out[7]:
1 dimensional Basis on the hypercube formed by [0.0] × [1.0].
Basis families are BasisMatrices.Spline

In [12]:
xgrid = collect(linspace(a, b, 101))
ys = []
m = 9
for i in 1:m
    c = zeros(basis3.n[1], basis3.n[1])
    c[i, i] = 1
    y = funeval(c, basis3, xgrid)
    push!(ys, y[:, i])
end
ylims = (-1.1, 1.1)
plot(xgrid, ys, layout=m, ylims=ylims, leg=false)


Out[12]:

In [ ]: