如果在Julia中生成矢量,则生成微分方程 [英] Generate DifferentialEquations ifrom a vector in Julia
问题描述
我创建了一个向量,该向量将微分方程的字符串连接起来,格式正确,可用于茱莉亚的差分运算法则(即f(du,u,p,t):
I have created a vector which concatenates strings of differential equations that are in the correct format to be used be the differeq ode sovler in Julia (i.e, f(du,u,p,t):
Combine <- c("du[1] = - 1*0.4545*(u[1]^1) - 1*27000000*(u[4]^1)*(u[1]^1)",
"du[2] = - 1*3100000000*(u[2]^1)*(u[4]^1)", "du[3] = - 1*33000*(u[3]^1)*(u[4]^1)",
"du[4] =2*0.4545*(u[1]^1) - 1*3100000000*(u[2]^1)*(u[4]^1) - 1*33000*(u[3]^1)*(u[4]^1) - 1*27000000*(u[4]^1)*(u[1]^1) - 1*8500000*(u[4]^1)*(u[5]^1) - 1*390000000*(u[4]^1)*(u[6]^1)",
"du[5] = - 1*8500000*(u[4]^1)*(u[5]^1)", "du[6] = - 1*390000000*(u[4]^1)*(u[6]^1)"
我的问题是如何使用JuliaCall:Julia_eval评估这些表达式?我知道,如果我明确包含表达式,则ODE求解器可以正常工作,但是如果我仅使用Combine [i],则会遇到错误,因为我正在调用全局变量.
My question is how can I evaluate these expressions using JuliaCall:Julia_eval? I know that if I explicitly include the expressions the ODE solver works correctly, but if I just use Combine[i] I run into an error because I am calling a global variable.
f <- JuliaCall::julia_eval("
function f(du,u,p,t)
du[1] = - 1*0.4545*(u[1]^1) - 1*27000000*(u[4]^1)*(u[1]^1)
du[2] = - 1*3100000000*(u[2]^1)*(u[4]^1)
du[3] = - 1*33000*(u[3]^1)*(u[4]^1)
du[4] = 2*0.4545*(u[1]^1) - 1*3100000000*(u[2]^1)*(u[4]^1) - 1*33000*(u[3]^1)*(u[4]^1) - 1*27000000*(u[4]^1)*(u[1]^1) - 1*8500000*(u[4]^1)*(u[5]^1) - 1*390000000*(u[4]^1)*(u[6]^1)
du[5] = - 1*8500000*(u[4]^1)*(u[5]^1)
du[6] = - 1*390000000*(u[4]^1)*(u[6]^1)
end")
有什么办法可以解决此问题?我尝试取消列出"Combine"的列表,并在每行末尾添加\ n来创建单个字符串,但这未成功.理想的情况是,我可以从全局环境中调用向量,并由ODE求解器对其进行分析,如下所示(我知道这不能像这样工作):
Is there any way to work around this? I have tried unlisting "Combine" and creating a single string with \n included at the end of each line but this was unsuccessful. It would be ideal if I could call in the vector from the global environment and have that analyzed by the ODE solver, something like below ( i know this cant work like this):
f <- JuliaCall::julia_eval("
function f(du,u,p,t)
Combine[i]
end")
推荐答案
如@jverzani所述,
As @jverzani mentioned,
sprintf("function f(du,u,p,t)\n%s\nend", paste(Combine, collapse="\n"))
在R中构建所需的字符串.如果您JuliaCall::julia_eval
那,那么您就完成了.其他任何事情都完全过分复杂,并且无论如何也不会进行优化,因此,我不建议您尝试在此处采用的其他路径.
builds the string in R that you want. If you JuliaCall::julia_eval
that, then you're done. Anything else is completely over-complicated, and will also be less optimized anyways, and so I would no recommend the other paths you are trying to take here.
这篇关于如果在Julia中生成矢量,则生成微分方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!