diff --git a/Project.toml b/Project.toml index 516f264..90b67fc 100644 --- a/Project.toml +++ b/Project.toml @@ -3,6 +3,14 @@ uuid = "ce1ef771-b552-5ca6-80e4-7358b8c578e2" authors = ["Florian Oswald "] version = "0.1.0" +[deps] +ApproXD = "f0e97480-066f-5960-9ba5-e027352bc8af" +ApproxFun = "28f2ccd6-bb30-5033-b560-165f7b14dc2f" +Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" +FastGaussQuadrature = "442a2c76-b920-505d-bb47-c5924d526838" +Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" +PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee" + [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/src/.HWfuncapp.jl.swp b/src/.HWfuncapp.jl.swp new file mode 100644 index 0000000..d758a40 Binary files /dev/null and b/src/.HWfuncapp.jl.swp differ diff --git a/src/HWfuncapp.jl b/src/HWfuncapp.jl index 732fe3f..4290786 100644 --- a/src/HWfuncapp.jl +++ b/src/HWfuncapp.jl @@ -9,110 +9,61 @@ import ApproXD: getBasis, BSpline using Distributions using ApproxFun using Plots +export TT, ChebyT, q1 + ChebyT(x,deg) = cos(acos(x)*deg) unitmap(x,lb,ub) = 2 .* (x .- lb) ./ (ub .- lb) .- 1 #[a,b] -> [-1,1] -function q1(n) - f(x) = x .+ 2x.^2 - exp.(-x) - - # without using PyPlot, just erase the `PyPlot.` part - PyPlot.savefig(joinpath(dirname(@__FILE__),"..","q1.png")) - return Dict(:error=>maximum(abs,err)) -end - -function q2(b::Number) - @assert b > 0 - # use ApproxFun.jl to do the same: - - Plots.savefig(p,joinpath(dirname(@__FILE__),"..","q2.png")) -end - -function q3(b::Number) - - # p is your plot - return (p,integral) -end - -# optinal -function q4() - - return fig -end - - -# I found having those useful for q5 -mutable struct ChebyType - f::Function # fuction to approximate - nodes::Union{Vector,LinRange} # evaluation points - basis::Matrix # basis evaluated at nodes - coefs::Vector # estimated coefficients - - deg::Int # degree of chebypolynomial - lb::Float64 # bounds - ub::Float64 - - # constructor - function ChebyType(_nodes::Union{Vector,LinRange},_deg,_lb,_ub,_f::Function) - n = length(_nodes) - y = _f(_nodes) - _basis = Float64[ChebyT(unitmap(_nodes[i],_lb,_ub),j) for i=1:n,j=0:_deg] - _coefs = _basis \ y # type `?\` to find out more about the backslash operator. depending the args given, it performs a different operation - # create a ChebyComparer with those values - new(_f,_nodes,_basis,_coefs,_deg,_lb,_ub) - end -end - -# function to predict points using info stored in ChebyType -function predict(Ch::ChebyType,x_new) +n=15 +map(x,lb,ub) = 0.5 .* (lb .+ ub) .+ 0.5 .* (ub .- lb) .* x - true_new = Ch.f(x_new) - basis_new = Float64[ChebyT(unitmap(x_new[i],Ch.lb,Ch.ub),j) for i=1:length(x_new),j=0:Ch.deg] - basis_nodes = Float64[ChebyT(unitmap(Ch.nodes[i],Ch.lb,Ch.ub),j) for i=1:length(Ch.nodes),j=0:Ch.deg] - preds = basis_new * Ch.coefs - preds_nodes = basis_nodes * Ch.coefs - - return Dict("x"=> x_new,"truth"=>true_new, "preds"=>preds, "preds_nodes" => preds_nodes) +function Cheby(x,deg) +len=length(x) +psi = ones(len,deg) +for i in 1:len +for j in 1:deg +psi[i,j] = ChebyT(x[i],j-1) end - -function q5(deg=(5,9,15),lb=-1.0,ub=1.0) - - runge(x) = 1.0 ./ (1 .+ 25 .* x.^2) - - - PyPlot.savefig(joinpath(dirname(@__FILE__),"..","q5.png")) - end - - - -function q6() - - # compare 2 knot vectors with runge's function - - PyPlot.savefig(joinpath(dirname(@__FILE__),"..","q6.png")) - +return psi end -function q7() - PyPlot.savefig(joinpath(dirname(@__FILE__),"..","q7.png")) +function q1(n=15) +lb, ub, inter, Nod = -3, 3, 100, gausschebyshev(n)[1]#Values for evaluation +x = range(lb,stop = ub,length = n)# +psi = map(Nod, lb, ub) #Evaluation + +f(x) = x .+ 2x.^2 - exp.(-x)#Function to evaluate +PSI=inv(Cheby(Nod,n)) +ev=f(psi) +ResEv=PSI*ev + +testX=range(lb, stop=ub, length=inter) +testY=f(testX) + +testMap=map(testX, lb, ub) +testPsi=Cheby(unitmap(testMap), n) + +ResEv=testPsi*PSI + +testError=ev-ResEv + +#PyPlot.plot() +p = Plots.plot(layout = 2, dpi = 400) +Plots.plot!(p[1],testX,testY,label = "Test Y",lw = 1,linecolor = "black") +Plots.plot!(p[1],testX,ResEv, label = "Result Evaluation", lw = 2,linestyle = :dot, linecolor = "pink") +Plots.plot!(p[2],testX,testError, label = "Test Error", lw = 1, linecolor = "blue") + +# without using PyPlot, just erase the `PyPlot.` part +PyPlot.savefig(joinpath(dirname(@__FILE__),"..","q1.png")) +return Dict(:error=>maximum(abs,err)) end - # function to run all questions -function runall() - @info("running all questions of HW-funcapprox:") - q1(15) - q2(3) - q3(10) - q4() - q5() - q6() - q7() -end +end # module -end # module