From 0cc811062fc157403c392b8c89f6e586f3a43687 Mon Sep 17 00:00:00 2001 From: nicolas_ghio Date: Sat, 13 Apr 2019 18:05:24 +0200 Subject: [PATCH] HW --- Project.toml | 8 +++ src/.HWfuncapp.jl.swp | Bin 0 -> 12288 bytes src/HWfuncapp.jl | 131 +++++++++++++----------------------------- 3 files changed, 49 insertions(+), 90 deletions(-) create mode 100644 src/.HWfuncapp.jl.swp 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 0000000000000000000000000000000000000000..d758a40aa98a045023e617b6c86d6a8fb074fb3d GIT binary patch literal 12288 zcmeI2O^6&t7=|lB(N&|O7rlL%5$xXd^iEF$+}wrfX(u?CI|9 zs-E;5RPgE_2p%E`@!(nTBaV_j z>#ZLcwmZ$0#nW`yZ8CiBW$g3YK6v)x6Z3lpM;SXKy0LElug~dYOO1dxba?1XS9S%D zx?DDr*zKjeI=`qR=GqL17HUHem7&^f-k|R;4=6GTmsL4HLwgG1P_3{;J1B@eGR?}~KC*aQ!Ozv01O;7{-e_zC<7egK6(H{tvm$;JWWfN{V$U>q(-pwmggo>zlN@Lx**gb&J<-*M13k#IW)|q zBA)t^lNxY3Up?YHC4QKDHuz-P@?IU=?vmJ*>n^6*Mya=5rx-%d?`kcgWUf)2=-U$wgH+p}a1f z;Rc>o*s*No>te$hHawe{&O3x8^5z5HFRG%fLc8vYUNIJ}g{p%Ho1xGy#|1Zcgp^L_ zWen41kZHGHrRB$g(~Ri#?tcjS8mT1gMn#z5~qrmmz~NThqgRcoi}>J zSLH{OB#m<{lB#|}NR^6Cre{GiF)FeYL8~X`sn69hUkd5av9pQZn$wKMVTdJPCu~I= zZ(&H1a~g4qdx}T2mHDCQO`tU3#p$2GwUcR) z0uj(C>kg2Ygwj(wJv|5d{dzcD9H}RLsZQy2_hjY=sgDDl [-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