ODE时代Matlab vs R [英] ODE Times Matlab vs R

查看:160
本文介绍了ODE时代Matlab vs R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在matlab中使用诸如ODE45之类的可变时间步长求解器-我将为输出定义一个时间跨度,即times = [0 50],并且matlab将在0到50之间的各个时间步长返回结果.

If using a variable time step solver such as ODE45 in matlab - I would define a time span for the outputs, i.e. times = [0 50], and matlab would return results at various time steps between 0 and 50.

但是,在R中,似乎必须定义ODE返回结果的时间点,即,如果我给出times = 0:50,它将在0,1,2, ... 50返回51个结果.否则,我必须提供一个序列,例如times = seq(0,50,0.1).

However in R it appears I have to define the time points at which I want the ODE to return the results, i.e if I gave times = 0:50, it would return 51 results at 0,1,2, ... 50. Other wise I have to supply a sequence such as , times = seq(0,50,0.1).

我的功能在开始时会迅速变化,然后会逐渐变化.在MATLAB中,输出结果通过返回的82个时间步长反映了这一点,其中49个在时间步长0和1之间.

I have a function which changes rapidly at the beginning and then much more gradually. In MATLAB the output results reflects this with 82 time steps returned in the results, of which 49 are between the time step 0 and 1.

我想知道是否有一种方法可以使R以与MATLAB相同的方式返回结果,所以在没有我预先指定希望返回结果的时间点的情况下.

I want to know if there is a way to get R to return the results in the same manner as MATLAB, so without me having the pre-specify the time points I wish the results returned at.

推荐答案

R -包deSolve通过以下方式描述了使用的参数times:

The R-Package deSolve describes the used parameter times in this way:

需要输出的时间顺序;

time sequence for which output is wanted;

丹尼斯喜欢的文档还有一个重要的句子:

The document Dennis liked to has another important sentence:

我们注意到,对于某些实现, 需要输出的向量 times 定义了该方法所处的网格 执行其步骤,因此解决方案的准确性很大程度上取决于输入 向量时间.

We note that, for some implementations, the vector times at which the output is wanted defines the mesh at which the method performs its steps, so the accuracy of the solution strongly depends on the input vector times.

下面是一个简单的示例,这是关于包装deSolve的书中提到的Lorenz方程:

A simple example is the following one, the Lorenz equations, mentioned in the book about the package deSolve:

library(deSolve)

parameters <- c(
  a = -8/3,
  b = -10,
  c =  28)

state <- c(
  X = 1,
  Y = 1,
  Z = 1)

# ---- define function in R
Lorenz <- function(t, state, parameters) {
  with(as.list(c(state, parameters)),{
    # rate of change
    dX <- a*X + Y*Z
    dY <- b * (Y-Z)
    dZ <- -X*Y + c*Y - Z

    # return the rate of change
    list(c(dX, dY, dZ))
  })   # end with(as.list ...
}

times_1 <- seq(0, 100, by = 1)
out_1 <- lsoda(y = state, times = times_1, func = Lorenz, parms = parameters)

times_2 <- seq(0, 100, by = 0.01)
out_2 <- lsoda(y = state, times = times_2, func = Lorenz, parms = parameters)

tail(out_1)

       time        X          Y          Z
 [96,]   95 30.54833  11.802554  12.445819
 [97,]   96 21.26423   4.341405   4.785116
 [98,]   97 33.05220  13.021730  12.934761
 [99,]   98 21.06394   2.290509   1.717839
[100,]   99 10.34613   1.242556   2.238154
[101,]  100 32.87323 -13.054632 -13.194377

tail(out_2)

           time        X          Y         Z
 [9996,]  99.95 17.16735  -7.509679 -12.08159
 [9997,]  99.96 17.66567  -7.978368 -12.77713
 [9998,]  99.97 18.26620  -8.468668 -13.47134
 [9999,]  99.98 18.97496  -8.977816 -14.15177
[10000,]  99.99 19.79639  -9.501998 -14.80335
[10001,] 100.00 20.73260 -10.036203 -15.40840

您可以看到在t = 100时结果的差异.这来自定义不同的times.

You can see the differences in the results at t = 100. This comes from the different defined times.

关于,
J_F

Regards,
J_F

这篇关于ODE时代Matlab vs R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆