# Defining ODEs depending on function's input

**URL:** https://discourse.julialang.org/t/defining-odes-depending-on-functions-input/40416
**Category:** New to Julia
**Tags:** question, diffeq
**Created:** [May 29, 2020, 2:28pm UTC](https://discourse.julialang.org/t/defining-odes-depending-on-functions-input/40416 "2020-05-29T14:28:48Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![br500](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@br500](https://discourse.julialang.org/u/br500)
#### Post date: [May 29, 2020, 2:28pm UTC](https://discourse.julialang.org/t/defining-odes-depending-on-functions-input/40416/1 "2020-05-29T14:28:48Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [May 30, 2020, 5:02am UTC](https://discourse.julialang.org/t/defining-odes-depending-on-functions-input/40416/2 "2020-05-30T05:02:35Z")

</div>

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

```

is a continuous function for the values you want.

---

<div class="post-metadata">

### Author: ![br500](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@br500](https://discourse.julialang.org/u/br500)
#### Post date: [May 30, 2020, 9:56am UTC](https://discourse.julialang.org/t/defining-odes-depending-on-functions-input/40416/3 "2020-05-30T09:56:32Z")

</div>

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…

---

<div class="post-metadata">

### Author: ![DR59](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dr59/32/10087_2.png) [@DR59](https://discourse.julialang.org/u/DR59)
#### Post date: [May 30, 2020, 10:33am UTC](https://discourse.julialang.org/t/defining-odes-depending-on-functions-input/40416/4 "2020-05-30T10:33:20Z")

</div>

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!

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

```

---

<div class="post-metadata">

### Author: ![br500](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@br500](https://discourse.julialang.org/u/br500)
#### Post date: [May 30, 2020, 11:12am UTC](https://discourse.julialang.org/t/defining-odes-depending-on-functions-input/40416/5 "2020-05-30T11:12:13Z")

</div>

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

---

<div class="post-metadata">

### Author: ![br500](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@br500](https://discourse.julialang.org/u/br500)
#### Post date: [May 30, 2020, 3:11pm UTC](https://discourse.julialang.org/t/defining-odes-depending-on-functions-input/40416/6 "2020-05-30T15:11:02Z")

</div>

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