Defining ODEs depending on function's input

I’ve seen an example of solving Lorenz equations using DifferentialEquations:

g = @ode_def LorenzExample begin
dx = σ*(y-x)
dy = x*(ρ-z) - y
dz = x*y - β*z
end σ ρ β

u0 = [1.0;0.0;0.0]
tspan = (0.0,1.0)
p = [10.0,28.0,8/3]
prob = ODEProblem(g,u0,tspan,p)
sol = solve(prob)


I would like to have a function that returns sol for function given as an argument, something like: get_sol((x, y, z)->[y, -x, x*z]) so in this case dx = y, dy = -x, dz = x*z. I’ve no clue how to define equations depending on input… Maybe using macro @ode_def isn’t the way to do it.

function fsol(t)
u = sol(t)
[u[2],-u[1],u[1]*u[3]
end


is a continuous function for the values you want.

Thank you for the answer! Unfortunately I don’t understand it quite yet, I’m a total newbie… Let’s suppose I have a big function “solver” and within that function I need to solve ODEs based on solver’s input. For example I expect solver((x, y)->[x, -y]) to return solutions for dx = x, dy = -y. I’m getting stuck in parsing input. Maybe your answer is a solution to my problem, but I have no clue how to use it…

Is this what you mean? you can also define u0 and p outside the function. Not sure what context this coding pattern would be useful though…maybe a higher level reason as to why you want this would help. Good luck!

function solver(anon_func)
u0 = #something
p = #smoething
prob = ODEProblem(anon_func, u0, tspan, p)
return solve(prob)
end

1 Like

Exactly, thanks! Could you give a working example with anon_func similar to one I’ve written before? I’m still doing something wrong

Nevermind, I’ve found the mistake :)) Thank you