小编Haf*_*fez的帖子

MATLAB和R之间的执行时间差异很大

我想实现一个非常简单的4 阶龙格库塔方法,解决ODE y'=f(x,y).

我已经在R和MATLAB中实现了算法(见下文),但由于某种原因,在MATLAB中运行需要几分钟,在R中运行几毫秒.

我的问题是为什么?

似乎唯一的区别是初始化,并且玩这个似乎没有什么区别.


R脚本:

# Initialise Variables ----------------------------------------------------

L  = 1 #Domain of solution function
h  = 0.01#step size
x0 = 0
y0 = 0
x  = x0
y  = y0

# Define Forcing Function -------------------------------------------------

force = function(x,y){
  -16*y + 15*exp(-x)
}


# Compute Algorithm -------------------------------------------------------

for(i in 0:(L/h)){

  k1 = h*force(x,y[i])
  k2 = h*force(x + h/2, y[i] + k1 /2)
  k3 = h*force(x + h/2, y[i] + k2 /2)
  k4 …
Run Code Online (Sandbox Code Playgroud)

performance matlab r numerical-methods runge-kutta

3
推荐指数
1
解决办法
114
查看次数

标签 统计

matlab ×1

numerical-methods ×1

performance ×1

r ×1

runge-kutta ×1