From 5a1177385fd222e3f6048058f0b9afd45d3571ae Mon Sep 17 00:00:00 2001 From: Marie Date: Tue, 20 Mar 2018 19:33:17 +0100 Subject: [PATCH 1/4] Q1-Try1 with bug at phi --- main.jl | 6 ++++-- src/funcapp.jl | 39 ++++++++++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/main.jl b/main.jl index a4d7b3f..5a57ca7 100644 --- a/main.jl +++ b/main.jl @@ -2,5 +2,7 @@ include("src/funcapp.jl") -funcapp.runall() - +#funcapp.runall() +f(x) = x+2x^2-exp(-x) +n = 15 +q1(n) diff --git a/src/funcapp.jl b/src/funcapp.jl index a85eb99..be102c4 100644 --- a/src/funcapp.jl +++ b/src/funcapp.jl @@ -1,19 +1,37 @@ - +using Plots +using FastGaussQuadrature +using Gadfly +using ApproxFun +using ApproXD module funcapp - # use chebyshev to interpolate this: function q1(n) - - return Dict(:err => 1.0) - + deg=n-1 + upb = -3 + lowb = 3 + nodes = gausschebyshev(n) + z = 0.5(upb+lowb) + 0.5(upb-lowb)nodes[:1] + Phi = Matrix{Float64}(n,deg) + for i=1:n, j=1:(deg+1) + Phi[i,j]=cos(acos(nodes[:1][i])*(j-1)) + end + y = f(z) + c = Phi\y + yhat = vec(Phi*c) + plot(y) + !plot(yhat) + return 0 end +q1(n) + + function q2(n) - + end @@ -26,7 +44,7 @@ module funcapp unitmap(x,lb,ub) = 2.*(x.-lb)/(ub.-lb) - 1 #[a,b] -> [-1,1] type ChebyType - f::Function # fuction to approximate + f::Function # fuction to approximate nodes::Union{Vector,LinSpace} # evaluation points basis::Matrix # basis evaluated at nodes coefs::Vector # estimated coefficients @@ -45,7 +63,7 @@ module funcapp 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) @@ -65,12 +83,12 @@ module funcapp function q4b() - + end function q5() - + end @@ -87,4 +105,3 @@ module funcapp end - From f0e33e5dcc7e5c39ca380ee97e3adb11f9aaaa94 Mon Sep 17 00:00:00 2001 From: Marie Date: Sat, 21 Apr 2018 21:15:06 +0200 Subject: [PATCH 2/4] finished q1 and q2 --- src/funcapp.jl | 59 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/src/funcapp.jl b/src/funcapp.jl index e35879e..e03607d 100644 --- a/src/funcapp.jl +++ b/src/funcapp.jl @@ -1,30 +1,60 @@ - -module funcapp + using Plots + using FastGaussQuadrature + using ApproxFun + using Base.Test + #using ApproXD + function f(x) + return(x + 2 * x^2 - exp(-x)) + end # use chebyshev to interpolate this: function q1(n) - + #deg = n-1 + a = -3 + b = +3 + n = 15 + nodes = gausschebyshev(n)[1] + z = 0.5 * (a+b) + 0.5 * (b-a) * nodes + values = f.(z) + Phi = [cos((n-i+0.5) * (j-1) * π/n) for i = 1:n, j = 1:n] + c = Phi\values + function prediction(x) + node = 2 * (x - a)/(b - a) -1 + Phi = [cos(acos(node)*j) for j = 0:n-1] + return(transpose(c)*Phi) + end + new_n = 50 + new_nodes = [a + (i-1)/(new_n -1)*(b-a) for i in 1:new_n] + y = f.(new_nodes) + yhat = prediction.(new_nodes) + global e = y - yhat + return(plot(new_nodes, e, title="Deviation in approximation from true f")) end function q2(n) - + nodes = Chebyshev(-3..3) + grid = points(S,n) + values = f.(grid) + predictions = Fun(S,ApproxFun.transform(nodes,values)) + new_n + x = linspace(-3,3,new_n) + yhat = predictions.(x) + y = f.(x) + e = y - yhat + return(plot(x,e,title="Deviation in approximation from true f using ApproxFun")) end - # plot the first 9 basis Chebyshev Polynomial Basisi Fnctions function q3() - end - - ChebyT(x,deg) = cos(acos(x)*deg) unitmap(x,lb,ub) = 2.*(x.-lb)/(ub.-lb) - 1 #[a,b] -> [-1,1] type ChebyType - f::Function # fuction to approximate + f::Function # fuction to approximate nodes::Union{Vector,LinSpace} # evaluation points basis::Matrix # basis evaluated at nodes coefs::Vector # estimated coefficients @@ -43,7 +73,7 @@ module funcapp 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) @@ -63,12 +93,12 @@ module funcapp function q4b() - + end function q5() - + end @@ -76,13 +106,14 @@ module funcapp function runall() println("running all questions of HW-funcapprox:") q1(15) + @test maximum(e) < 1e-9 q2(15) q3() q4a() q4b() q5() - end + end -end +end From b0486ef2db65e7cafc7e1e55091fd02f4b4172e2 Mon Sep 17 00:00:00 2001 From: Marie Date: Thu, 26 Apr 2018 08:37:05 +0200 Subject: [PATCH 3/4] Q1 - Q3 done --- run.jl | 4 --- src/funcapp.jl | 88 ++++++++++++++++++++++++++++++++++---------------- 2 files changed, 60 insertions(+), 32 deletions(-) delete mode 100644 run.jl diff --git a/run.jl b/run.jl deleted file mode 100644 index 4ea89f1..0000000 --- a/run.jl +++ /dev/null @@ -1,4 +0,0 @@ - - -include("src/HW_int.jl") # load our code -HW_int.runall() \ No newline at end of file diff --git a/src/funcapp.jl b/src/funcapp.jl index e03607d..0783351 100644 --- a/src/funcapp.jl +++ b/src/funcapp.jl @@ -1,11 +1,11 @@ - +module funcapp using Plots using FastGaussQuadrature using ApproxFun using Base.Test - #using ApproXD + using ApproXD function f(x) return(x + 2 * x^2 - exp(-x)) @@ -32,32 +32,41 @@ y = f.(new_nodes) yhat = prediction.(new_nodes) global e = y - yhat - return(plot(new_nodes, e, title="Deviation in approximation from true f")) + global q1_plot = plot(new_nodes, e, title="Q1 - Deviation in approximation from true f") end function q2(n) nodes = Chebyshev(-3..3) - grid = points(S,n) + grid = points(nodes,n) values = f.(grid) - predictions = Fun(S,ApproxFun.transform(nodes,values)) - new_n + predictions = Fun(nodes,ApproxFun.transform(nodes,values)) + new_n =100 x = linspace(-3,3,new_n) yhat = predictions.(x) y = f.(x) e = y - yhat - return(plot(x,e,title="Deviation in approximation from true f using ApproxFun")) + global q2_plot = plot(x,e,title="Q2 - Deviation in approximation from true f using ApproxFun") end + ChebyT(x,deg) = cos(acos(x)*deg) + unitmap(x,lb,ub) = 2.*(x.-lb)/(ub.-lb) - 1 #[a,b] -> [-1,1] # plot the first 9 basis Chebyshev Polynomial Basisi Fnctions function q3() + plot_total = Dict() + x = [-1 + (i-1)/(100 -1)*2 for i in 1:100] + for deg in 0:8 + y = ChebyT.(x, deg) + plot_total[deg] = plot(x, y, ylim = [-1.0, 1.0],labels = ["degree = $deg"]) + end + global q3_plot = plot(title = "Q3 - Chebyshev basis functions", plot_total[0], plot_total[1], plot_total[2], plot_total[3], plot_total[4], plot_total[5], plot_total[6], plot_total[7], plot_total[8], layout = (3,3)) + end - unitmap(x,lb,ub) = 2.*(x.-lb)/(ub.-lb) - 1 #[a,b] -> [-1,1] type ChebyType - f::Function # fuction to approximate - nodes::Union{Vector,LinSpace} # evaluation points - basis::Matrix # basis evaluated at nodes - coefs::Vector # estimated coefficients + f::Function # fuction to approximate + nodes::Union{Vector,LinSpace} # evaluation points + basis::Matrix # basis evaluated at nodes + coefs::Vector # estimated coefficients deg::Int # degree of chebypolynomial lb::Float64 # bounds @@ -66,7 +75,7 @@ # constructor function ChebyType(_nodes::Union{Vector,LinSpace},_deg,_lb,_ub,_f::Function) n = length(_nodes) - y = _f(_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 ChebyType with those values @@ -77,7 +86,7 @@ # function to predict points using info stored in ChebyType function predict(Ch::ChebyType,x_new) - true_new = Ch.f(x_new) + 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 @@ -86,32 +95,55 @@ return Dict("x"=> x_new,"truth"=>true_new, "preds"=>preds, "preds_nodes" => preds_nodes) end - function q4a(deg=(5,9,15),lb=-1.0,ub=1.0) - - - end - - function q4b() - - + function runge(x) + return(1/(1+25*x^2)) end - function q5() - + function q4a(deg=(5,9,15),lb=-5.0,ub=5.0) + deg=(5,9,15) + lb=-5.0 + ub=5.0 + x = [lb + (i-1)/(50 -1)*(ub-lb) for i in 1:50] + unif_y = Array[] + for j in 1:3 + n = 1 + deg[j] + unif_nodes = [lb + (i-1)/(n -1)*(ub-lb) for i in 1:n] + unif_cheby = ChebyType(unif_nodes, n, lb, ub, runge) + push!(unif_y, predict.preds(unif_cheby, x)) + end + for el in deg + n = el + 1 + cheby_nodes = gausschebyshev(n) + cheby_cheby = ChebyType(cheby_nodes, n, lb, ub, runge) + cheby_y[el] = predict(cheby_nodes,x) + end end + # + # function q4b() + # + # + # end + # + # function q5() + # + # + # end # function to run all questions function runall() println("running all questions of HW-funcapprox:") q1(15) - @test maximum(e) < 1e-9 + display(q1_plot) + # @test maximum(e) < 1e-9 q2(15) + display(q2_plot) q3() - q4a() - q4b() - q5() + display(q3_plot) + # q4a() + # q4b() + # q5() end From a1b6b9e73b1dbf455baea6e9ca3587dade00fbea Mon Sep 17 00:00:00 2001 From: Marie Date: Thu, 26 Apr 2018 08:48:46 +0200 Subject: [PATCH 4/4] Fixed bugs from merge conflict --- main.jl | 5 +---- src/funcapp.jl | 4 +--- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/main.jl b/main.jl index 5a57ca7..75ea136 100644 --- a/main.jl +++ b/main.jl @@ -2,7 +2,4 @@ include("src/funcapp.jl") -#funcapp.runall() -f(x) = x+2x^2-exp(-x) -n = 15 -q1(n) +funcapp.runall() diff --git a/src/funcapp.jl b/src/funcapp.jl index b3617e0..dfb62f8 100644 --- a/src/funcapp.jl +++ b/src/funcapp.jl @@ -16,7 +16,6 @@ module funcapp #deg = n-1 a = -3 b = +3 - n = 15 nodes = gausschebyshev(n)[1] z = 0.5 * (a+b) + 0.5 * (b-a) * nodes values = f.(z) @@ -35,8 +34,6 @@ module funcapp global q1_plot = plot(new_nodes, e, title="Q1 - Deviation in approximation from true f") end -q1(n) - function q2(n) nodes = Chebyshev(-3..3) @@ -54,6 +51,7 @@ q1(n) ChebyT(x,deg) = cos(acos(x)*deg) unitmap(x,lb,ub) = 2.*(x.-lb)/(ub.-lb) - 1 #[a,b] -> [-1,1] # plot the first 9 basis Chebyshev Polynomial Basisi Fnctions + function q3() plot_total = Dict() x = [-1 + (i-1)/(100 -1)*2 for i in 1:100]