Skip to content

Instantly share code, notes, and snippets.

@aa25desh
Last active March 26, 2019 15:31
Show Gist options
  • Save aa25desh/a8c9155b89276e570d4988c060eac4d3 to your computer and use it in GitHub Desktop.
Save aa25desh/a8c9155b89276e570d4988c060eac4d3 to your computer and use it in GitHub Desktop.
using DifferentialEquations
function f(du,u,p,t)
du[1] = dx = p[1]*u[1] - u[1]*u[2]
du[2] = dy = -3*u[2] + u[1]*u[2]
end
u0 = [1.0;1.0]
tspan = (0.0,10.0)
p = [1.5]
prob = ODEProblem(f,u0,tspan,p)
sol = solve(prob,Tsit5())
t = collect(range(0,stop=10,length=400))
using RecursiveArrayTools
randomized = VectorOfArray([(sol(t[i])) for i in 1:length(t)])
data = convert(Array,randomized)
x=Vector{Float64}(undef,400)
y=Vector{Float64}(undef,400)
for i in range(1,stop=400)
x[i]=data[1,i]
end
for i in range(1,stop=400)
y[i]=data[2,i]
end
using BasisFunctionExpansions
rbf = UniformRBFE(t,10, normalize=true)
bfa_x = BasisFunctionApproximation(x,t,rbf,1)
bfa_y = BasisFunctionApproximation(y,t,rbf,1)
x_hat = bfa_x(t)
y_hat = bfa_y(t)
using Plots
scatter(t,x, lab="x (ODE slo)")
scatter!(t,x_hat, lab="x_hat (RBF apporx.)")
scatter!(t,y,lab="y (ODE slo)")
scatter!(t,y_hat, lab="y_hat (RBF apporx.)")
nv=2:100
lcurve_x = map(nv) do n
rbf = UniformRBFE(t, n, normalize = true)
bfa = BasisFunctionApproximation(x,t,rbf,1)
(x-bfa(t))
end
lcurve_y = map(nv) do n
rbf = UniformRBFE(t, n, normalize = true)
bfa = BasisFunctionApproximation(y,t,rbf,1)
(y-bfa(t))
end
i=1
j=1
x_err = Array{Float64}(undef, 99)
for i in range(1,stop=99)
d=0
for j in range(1,stop=400)
if lcurve_x[i][j]>0
d=lcurve_x[i][j]+d
else
d=-lcurve_x[i][j]+d
end
x_err[i]=d
end
end
y_err = Array{Float64}(undef, 99)
for i in range(1,stop=99)
d=0
for j in range(1,stop=400)
if lcurve_y[i][j]>0
d=lcurve_y[i][j]+d
else
d=-lcurve_y[i][j]+d
end
y_err[i]=d
end
end
scatter(nv,x_err,lab="x_err")
scatter!(nv,y_err,lab="y_err")
function f(d)
i=1
j=2
h=999
for i in range(1,stop=99)
if d[i]<=h
h=d[i]
j=i
end
end
return j
end
min_x=f(x_err)
min_y=f(y_err)
rbf_x = UniformRBFE(t,min_x, normalize=true)
rbf_y = UniformRBFE(t,min_y, normalize=true)
bfa_x = BasisFunctionApproximation(x,t,rbf_x,1)
bfa_y = BasisFunctionApproximation(y,t,rbf_y,1)
x_hat_new = bfa_x(t)
y_hat_new = bfa_y(t)
scatter(t,x, lab="x (ODE slo)")
scatter!(t,x_hat_new, lab="x_hat (RBF apporx.)")
scatter!(t,y,lab="y (ODE slo)")
scatter!(t,y_hat_new, lab="y_hat (RBF apporx.)")
i=1
j=1
x_err = Array{Float64}(undef, 99)
for i in range(1,stop=99)
d=0
for j in range(1,stop=400)
d=(lcurve_x[i][j])^2+d
x_err[i]=(d^0.5)
end
end
y_err = Array{Float64}(undef, 99)
for i in range(1,stop=99)
d=0
for j in range(1,stop=400)
d=(lcurve_y[i][j])^2+d
y_err[i]=(d^0.5)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment