julia - 如何在 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
推荐阅读
- haproxy - 重新加载 HAProxy 配置时如何保持会话值?
- colors - 如何将材质调色板转换为 Flutter 主题?
- mount - nfs mount导致服务器卡死
- sonarqube - 无法在 Sonarqube 中打开网页
- javascript - 加载 iframe 时网页滚动卡住
- html - 如何在移动端设置两行,在桌面设置单行?
- javascript - 在 Javascript toString 中获取价值
- spring - 从 Spring Boot 应用程序 MailSendException 发送电子邮件
- node.js - 在 node.js 或浏览器中为 Tensorflow.js 使用自定义图像
- python - 如何在 Python 中创建时间范围?