How can I pass a parameter inside a condition in Julia?

Viewed 77

Let's suppose the example 1 of the bouncing ball with multiple walls in the page:

https://diffeq.sciml.ai/stable/features/callback_functions/

And consider the condition:

  function condition(out,u,t,integrator) # Event when event_f(u,t) == 0
   out[1] = u[1]
   out[2] = (u[3] - 10.0)u[3]
  end

But, suppose I want the value "10.0" as a parameter (e.g., "h"). Is there way to write like the following?

  function condition(out,u,t,integrator) # Event when event_f(u,t) == 0
   out[1] = u[1]
   out[2] = (u[3] - h)u[3]
  end
1 Answers

Consider the code later on on the same page you listed:

dosetimes = [4.0,8.0]
condition(u,t,integrator) = t ∈ dosetimes
affect!(integrator) = integrator.u[1] += 10
cb = DiscreteCallback(condition,affect!)
sol = solve(prob,Tsit5(),callback=cb,tstops=dosetimes)
plot(sol)

Here, condition(U, t, integrator) depends on dosetimes. So you should be able to likewise do this:

const h = [10.0] # h is a global const, but h[1] can be changed if needed
function condition(out,u,t,integrator) # Event when event_f(u,t) == 0
    out[1] = u[1]
    out[2] = (u[3] - h[1])u[3]
end
Related