Generate DifferentialEquations ifrom a vector in Julia

你说的曾经没有我的故事 提交于 2020-01-16 08:40:34

问题


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)"

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")

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")

回答1:


As @jverzani mentioned,

sprintf("function f(du,u,p,t)\n%s\nend", paste(Combine, collapse="\n"))

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.



来源:https://stackoverflow.com/questions/58704190/generate-differentialequations-ifrom-a-vector-in-julia

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!