MATLAB 和 R 之间的执行时间差异很大
Large differences in execution time between MATLAB and R
我正在尝试实现一个非常简单的 4th order Runge-Kutta Method,用于求解 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 = h*force(x + h , y[i] + k3 )
temp=y[i] + (1/6)*(k1 + 2*k2 + 2*k3 + k4)
y = c(y,temp)
x = x + h
i = i+1
}
t <- seq(from=0, to=L, by=h)
plot(t,y,type="l")
MATLAB 脚本:
%% Initialise Variables
y0 = 0;
x0 = 0;
L = 1; %Length of Domain of function (here y)
N = 100; %Number of steps to break the domain into
h = L/N; %Step Size
yi = y0; %intermediate value of y
xi = x0; %intermediate value of x to be ticked in algo
y = zeros(N,1); %store y values as components
x = 0:h:L; %just for plot later
%% Define Forcing Function
syms f(A,B)
f(A,B) = 15*exp(-A) - 16*B;
%% Execute Algorithm
for n = 1:1:N;
xi= h*(n-1);
k1= h*f(xi,yi);
k2= h*f(xi + h/2 , yi + k1/2);
k3= h*f(xi + h/2 , yi + k2/2);
k4= h*f(xi + h , yi + k3 );
yi= yi + (1/6)*(k1 + 2*k2 + 2*k3 + k4);
y(n,1)=yi;
end
%plot(x,y)
您遇到此问题的原因是您使用符号变量进行本应为纯数值的计算。
如果定义f
如下:
f = @(A,B)15*exp(-A) - 16*B;
循环几乎立即完成。更多注意事项:
- 上面的语法是用来定义一个function handle to an anonymous function.
- 生成的
x
和 y
向量的长度不同,因此您之后无法 plot
它们。
- 以后你应该
profile
你的代码来找出性能瓶颈。
P.S.
您在 R 中的函数定义的 MATLAB 等效项非常相似:
function out = force(x)
out = 15*exp( -x(1) ) - 16*x(2);
end
或
function out = force(x,y)
out = 15*exp(-x) - 16*y;
end
...取决于输入是否为向量。
我正在尝试实现一个非常简单的 4th order Runge-Kutta Method,用于求解 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 = h*force(x + h , y[i] + k3 )
temp=y[i] + (1/6)*(k1 + 2*k2 + 2*k3 + k4)
y = c(y,temp)
x = x + h
i = i+1
}
t <- seq(from=0, to=L, by=h)
plot(t,y,type="l")
MATLAB 脚本:
%% Initialise Variables
y0 = 0;
x0 = 0;
L = 1; %Length of Domain of function (here y)
N = 100; %Number of steps to break the domain into
h = L/N; %Step Size
yi = y0; %intermediate value of y
xi = x0; %intermediate value of x to be ticked in algo
y = zeros(N,1); %store y values as components
x = 0:h:L; %just for plot later
%% Define Forcing Function
syms f(A,B)
f(A,B) = 15*exp(-A) - 16*B;
%% Execute Algorithm
for n = 1:1:N;
xi= h*(n-1);
k1= h*f(xi,yi);
k2= h*f(xi + h/2 , yi + k1/2);
k3= h*f(xi + h/2 , yi + k2/2);
k4= h*f(xi + h , yi + k3 );
yi= yi + (1/6)*(k1 + 2*k2 + 2*k3 + k4);
y(n,1)=yi;
end
%plot(x,y)
您遇到此问题的原因是您使用符号变量进行本应为纯数值的计算。
如果定义f
如下:
f = @(A,B)15*exp(-A) - 16*B;
循环几乎立即完成。更多注意事项:
- 上面的语法是用来定义一个function handle to an anonymous function.
- 生成的
x
和y
向量的长度不同,因此您之后无法plot
它们。 - 以后你应该
profile
你的代码来找出性能瓶颈。
P.S.
您在 R 中的函数定义的 MATLAB 等效项非常相似:
function out = force(x)
out = 15*exp( -x(1) ) - 16*x(2);
end
或
function out = force(x,y)
out = 15*exp(-x) - 16*y;
end
...取决于输入是否为向量。