We will use a package called ApproxFun
in this tutorial. ApproxFun
is a Julia package that is based on the Matlab package called chebfun
. The chebfun
package was written by Lloyd N. Trefethen (Nick Trefethen) and Zachary Battles in early 2004, then greatly extended starting around 2007 and is currently under active development. The idea is to use good function bases to represent functions numerically on the computer, instead of symbolically.
using Pkg
Pkg.add("ApproxFun") # Add the package we'll need
using ApproxFun # use it!
using Plots
using LinearAlgebra
# We can use ApproxFun to build up functions
x = Fun(identity,-1..1)
plot(x)
What is x
here? x
is a representation of the identify function $f(x) = x$ on the interval $[-1,1]$. It is represented in a basis of Chebyshev polynomials. You don't need to know much about that this point (we'll cover it in class), but this is a particularly useful basis. What matters is that we can compute with $f$ really easily!
plot(sin(100*x))
x^5
coefficients(sin(100*x))
Not only can we manipulate x
, we can also find roots and derivatives!
f = sin(10*x)^2
g = cos(x)
h = f+g-1
r = roots(h)
rp = roots(h') # compute the zeros of the derivate of h
plot(h)
scatter!(r,h.(r);color=:green)
scatter!(rp,h.(rp);color=:red)
f = Fun(x -> exp(-x), 1..5)
@show sum(f)
@show exp(-1) - exp(-5.);
g = Fun(x -> exp(x), 1..5)
plot(f*g) # everything is a numerical approximation
extrema(f*g)
@show norm(f-g,1)
@show norm(f-g,2)
@show norm(f-g,Inf); # why is this negative???
plot(abs(g-f))
x = Fun(identity,-1..1)
f = sign(x)
plot(f, ylims=(-1.5,1.5))
@show norm(f-0.5,1);
@show norm(f-0.5,2);
@show norm(f-0.5,Inf);
# ApproxFun can be useful to generate inner-product matrices and function bases
B = [(0.0.*x + 1.);x;x^2;x^3]
plot(B, xlims=(-1,1))
# this isn't the gram-schmidt process for functions!
# we can orthogonalize B
Pi = B # start with the basis
for j=2:length(B)
for k=1:j-1
Pi[j] .= Pi[j] - sum(B[j]*Pi[k])/sum(Pi[k]*Pi[k])*B[k]
end
end
display(plot(Pi,xlims=))
sum(Pi[1]*Pi[3])
Pi[3]