Reputation: 31
so I'm working on optimization with Julia and so far I could solve a Vector-ODE System with my following code. (Anything under bracket "ODE Model" is not relevant since its just my physical model, instead I have my focus on bracket "synthetic Data" and "cost function+Optimization")
using DifferentialEquations,DiffEqParamEstim,RecursiveArrayTools using Optim,Plots
###############################################
# ODE Model
###############################################
function f(du,u,p,t)
du[1] = dc_n = p[1]*u[1] -u[1]*u[2]
du[2] = dc_d = -p[2]*u[2]^2 + u[1]*u[2]
end
u0=[1.0,1.0]
tspan = (0.0,10.0)
p = [1.5,2.0,1.2]
prob = ODEProblem(f,u0,tspan,p)
#Lösung ODE Problem
sol = solve(prob,Tsit5())
plot(sol)
###############################################
# Synthetic Data
###############################################
t = collect(range(0,stop=10,length=30))
streu = VectorOfArray([(sol(t[i]) + .01randn(2))
for i in 1:length(t)])
data = convert(Array,streu)
###############################################
# loss function and Optimization
###############################################
cost_function = build_loss_objective(prob, Tsit5(), L2Loss(t,data),maxiters=10000,verbose=false)
initialGuess = ones(3)
result = optimize(cost_function, initialGuess, BFGS())
newprob = remake(prob, p =result.minimizer)
print(result.minimizer)
newsol = solve(newprob,Tsit5())
plot!(newsol)
So now under "synthetic Data" instead of givng the cost function data for both u[1] and u\with "data"(2x30Matrix), I want to give it data just for u[2] and let it run the optimization just off that.How do I need to implement that so it estimates the parameters succesfully?
Thanks in advance.
Upvotes: 2
Views: 203
Reputation: 19132
Set save_idxs
so that the solver only outputs the second index. I.e.:
sol = solve(prob,Tsit5(), save_idxs = 2)
in the datagen, and in the parameter estimation loss:
cost_function = build_loss_objective(prob, Tsit5(), L2Loss(t,data),maxiters=10000,verbose=false, save_idxs = 2)
Upvotes: 1