[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
ODEs: 3 approaches: which is best?
From: |
Jim H |
Subject: |
ODEs: 3 approaches: which is best? |
Date: |
Fri, 3 Oct 2008 14:39:59 -0700 (PDT) |
I know of at least 3 ways to solve odes using lsode and I did a simple
tic-toc comparison. I'd like to know what users think are the pro's and
con's of the different approaches.
Method 1: put the function to be integrated in one file and run it from
another file
Method 2: put the function in a file with the lsode invocation + put the
parameters inside the function
Method 3: Method 2, but pass the parameters to the function.
Simple Timings: average of 3
Method 1: 2.13 sec
Method 2: 2.19 sec
Method 3: 2.59 sec
Is (1) fastest because of compiling? Why is (2) faster than (3)? But, speed
is not everything; there is something to be said for having 1 file,
especially in the context of teaching modeling to biologists. Any opinions
on advantages and disadvantages?
-Jim
Here are the basic codes...
=======================
Method (1) Run file:
%Parameters (model specific)
global ALF = 1.5; % competition N1 on N2
global B = 0.01; % predation rate
global C = 0.5; % conversion Prey to Predator
global D = 1.0; % predator death rate
global EP = 0.009; % N2 predator avoidance advantage
global K = 800; % N(1)(2) carrying capacity
global R = 1.0; % prey percapita increase
global MODEL="OSimVanceFunc";
yo = [200; 200; 40];
t=linspace(0,1200,1201);
tic()
y=lsode(MODEL,yo,t);
toc()
Method (1) Function File:
function dydt = OSimVanceFunc(y,t)
global ALF; % competition N1 on N2
global B ; % predation rate
global C ; % conversion Prey to Predator
global D ; % predator death rate
global EP ; % N2 predator avoidance advantage
global K ; % N(1)(2) carrying capacity
global R ; % prey percapita increase
n1 = y(1)*(R - (R/K)*(y(1)+y(2)) - B*y(3));
n2 = y(2)*(R - (R/K)*(ALF*y(1)+y(2)) -(B-EP)*y(3));
n3 = y(3)*(C*B*y(1) + C*(B-EP)*y(2) - D);
dydt = [n1;n2;n3];
====================================
Method (2)
1; % dummy command so we can have a function defined in this file
function ret = f(x,t)
ALF = 1.5; % competition N1 on N2
B = 0.01; % predation rate
C = 0.5; % conversion Prey to Predator
D = 1.0; % predator death rate
EP = 0.009; % N2 predator avoidance advantage
K = 800; % N(1)(2) carrying capacity
R = 1.0; % prey percapita increase
% the model
n1 = x(1)*(R - (R/K)*(x(1)+x(2)) - B*x(3));
n2 = x(2)*(R - (R/K)*(ALF*x(1)+x(2)) -(B-EP)*x(3));
n3 = x(3)*(C*B*x(1) + C*(B-EP)*x(2) - D);
ret = [n1;n2;n3];
endfunction
x0 = [200;200;40]; % initial conditions as column vector
t=linspace(0,1200,1201)'; % solution times as column vector
tic()
x=lsode("f",x0,t); % solve
toc()
==================================================
Method (3)
1; % dummy command so we can have a function defined in this file
function ret = f(x,t, ALF,B,C,D,EP,K,R)
% the model
n1 = x(1)*(R - (R/K)*(x(1)+x(2)) - B*x(3));
n2 = x(2)*(R - (R/K)*(ALF*x(1)+x(2)) -(B-EP)*x(3));
n3 = x(3)*(C*B*x(1) + C*(B-EP)*x(2) - D);
ret = [n1;n2;n3];
endfunction
ALF = 1.5; % competition N1 on N2
B = 0.01; % predation rate
C = 0.5; % conversion Prey to Predator
D = 1.0; % predator death rate
EP = 0.009; % N2 predator avoidance advantage
K = 800; % N(1)(2) carrying capacity
R = 1.0; % prey percapita increase
x0 = [200;200;40]; % initial conditions as column vector
t=linspace(0,1200,1201)'; % solution times as column vector
g = @(x,t) f(x, t, ALF, B, C, D, EP, K, R);
tic()
x=lsode(g, x0, t);
toc()
--
View this message in context:
http://www.nabble.com/ODEs%3A-3-approaches%3A-which-is-best--tp19806022p19806022.html
Sent from the Octave - General mailing list archive at Nabble.com.
- ODEs: 3 approaches: which is best?,
Jim H <=