In [1]:
using BasisMatrices
using Plots
In [2]:
n = 10
a = 0
b = 1
xgrid = collect(linspace(a, b, 101))
Out[2]:
In [3]:
basis = Basis(ChebParams(n, a, b))
ys = []
m = 9
for i in 1:m
c = zeros(n)
c[i] = 1
y = funeval(c, basis, xgrid)
push!(ys, y)
end
ylims = (-1.1, 1.1)
plot(xgrid, ys, layout=m, ylims=ylims, leg=false)
Out[3]:
In [4]:
basis = Basis(LinParams(n, a, b))
ys = []
m = 9
for i in 1:m
c = zeros(n)
c[i] = 1
y = funeval(c, basis, xgrid)
push!(ys, y)
end
ylims = (-0.1, 1.1)
plot(xgrid, ys, layout=m, ylims=ylims, leg=false)
Out[4]:
In [5]:
breaks = [a, b]
evennum = 7
k = 3
m = 9
basis = Basis(SplineParams(breaks, evennum, k))
ys = []
for i in 1:m
c = zeros(m)
c[i] = 1
y = funeval(c, basis, xgrid)
push!(ys, y)
end
ylims = (-0.1, 1.1)
plot(xgrid, ys, layout=m, ylims=ylims, leg=false)
Out[5]:
In [6]:
f1(x) = exp(-x)
f2(x) = 1 ./ (1 + 25x .^ 2)
f3(x) = abs(x).^ 0.5
funcs = [f1, f2, f3]
degrees = [10, 20, 30]
a = -5
b = 5
xgrid = collect(linspace(a, b, 1001))
Out[6]:
In [7]:
for (i, f) in enumerate(funcs)
max_errors = Float64[]
for d in degrees
basis = Basis(ChebParams(d, a, b))
S, = nodes(basis)
psi = BasisMatrix(basis, Expanded(), S, 0)
y_true = f(S)
c = psi.vals[1] \ y_true
y_estimated = funeval(c, basis, xgrid)
y_true =f(xgrid)
abs_err = abs(y_true - y_estimated)
push!(max_errors, maximum(abs_err))
end
print("function: f$i, max of absolute errors, width degree 10, 20 and 30: ")
println(max_errors)
end
In [8]:
for (i, f) in enumerate(funcs)
max_errors = Float64[]
for d in degrees
basis = Basis(LinParams(d, a, b))
S, = nodes(basis)
psi = BasisMatrix(basis, Expanded(), S, 0)
y_true = f(S)
c = psi.vals[1] \ y_true
y_estimated = funeval(c, basis, xgrid)
y_true =f(xgrid)
abs_err = abs(y_true - y_estimated)
push!(max_errors, maximum(abs_err))
end
print("function: f$i, max of absolute errors, width degree 10, 20 and 30: ")
println(max_errors)
end
In [9]:
breaks = [a, b]
k = 3
for (i, f) in enumerate(funcs)
max_errors = Float64[]
for d in degrees
basis = Basis(SplineParams(breaks, d+1-k, k))
S, = nodes(basis)
psi = BasisMatrix(basis, Expanded(), S, 0)
y_true = f(S)
c = psi.vals[1] \ y_true
y_estimated = funeval(c, basis, xgrid)
y_true =f(xgrid)
abs_err = abs(y_true - y_estimated)
push!(max_errors, maximum(abs_err))
end
print("function: f$i, max of absolute errors, width degree 10, 20 and 30: ")
println(max_errors)
end
In [ ]: