简体   繁体   English

使用R求解ODE

[英]Using R to solve an ODE

I'm trying to solve an ODE using R as I dont have access to matlab 我正在尝试使用R解决ODE,因为我无法访问matlab

The equations are 方程是

dh/dt = 0.1*v/(pi*(2*10*h-h^2))

v^2 = (-0.1*v/(pi*(2*10*h-h^2))^2) + 2*9.81*h))

vdpol <- function (h, v, t) ( 
      list(c (
        -0.1*v/(pi*(2*10*h-h^2)),
          (v^2 = (-0.1*v/(pi*(2*10*h-h^2))^2) + 2*9.81*h))

              ))
library(deSolve)
yini <- (c(h = 20, v=0))
nonstiff <- ode(y = yini, func = vdpol,
                times= seq(0, 30, by = 0.01),
                parms = 1) 

The issues that pops up is: 弹出的问题是:

The number of derivatives returned by func() (4) must equal the length of the initial conditions vector (2) func()(4)返回的导数的数量必须等于初始条件向量(2)的长度

I'm not why it suggests that 4 derivatives have been inputted when i only out two 我不是为什么当我只输入两个时就暗示输入了4个导数

IIUC, ode expects a function with a given format. IIUC, ode期望具有给定格式的功能。 Refer to here 请参考这里

Hence, if you change your vdpol() function to meet the expected format, it should run. 因此,如果您更改vdpol()函数以使其符合期望的格式,则它将运行。 The general format is func(t, state, parameters) where state controls for your variables and parameters for other parameters. 通用格式为func(t, state, parameters) ,其中state控制变量,而parameters控制其他参数。

vdpol <- function (t, state, parameters) ( 
    with(as.list(c(state)), {
    return(list(c (-0.1*v/(pi*(2*10*h-h^2)),
                  (-0.1*v/(pi*(2*10*h-h^2))^2) + 2*9.81*h)))
    })
)

state = c(h = 10, v = 0)
times= seq(0, 30, by = 0.01)
out <- ode(y = state, times = times, func = vdpol, parms = c())
plot(out)

绘图输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM