如何在 Julia 的条件内传递参数?
How can I pass a parameter inside a condition in Julia?
假设页面中有多个墙的弹跳球示例1:
https://diffeq.sciml.ai/stable/features/callback_functions/
并考虑条件:
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
但是,假设我想要值“10.0”作为参数(例如“h”)。有没有办法像下面这样写?
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
稍后在您列出的同一页面上考虑代码:
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)
这里,condition(U, t, integrator)
依赖于dosetimes
。所以你应该也可以这样做:
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
假设页面中有多个墙的弹跳球示例1:
https://diffeq.sciml.ai/stable/features/callback_functions/
并考虑条件:
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
但是,假设我想要值“10.0”作为参数(例如“h”)。有没有办法像下面这样写?
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
稍后在您列出的同一页面上考虑代码:
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)
这里,condition(U, t, integrator)
依赖于dosetimes
。所以你应该也可以这样做:
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