[R] odesolve: lsoda vs rk4

Setzer.Woodrow@epamail.epa.gov Setzer.Woodrow at epamail.epa.gov
Tue Jun 15 17:46:50 CEST 2004

lsoda doesn't pass along the names attribute of the state vector, y.  If
you want to use the names of the state vector in your code, you need to
reassign it inside your ode function.  I should either fix this in the
code for lsoda, or at least document it!  For example, I can run the
following modification of your code:

func2 <- function(t, y, p)
  names(y) <- c("A","B","C","D")  ### Here put the names attribute back
  Ad <- p["p2"]*(p["p1"]*y["A"]*y["D"])/(p["p2"]+p["p3"]) +
    p["p6"]*(p["p4"]*y["B"]*p["p10"])/(p["p5"]+p["p6"]) -
  Bd <- p["p3"]*(p["p1"]*y["A"]*y["D"])/(p["p2"]+p["p3"]) +
    p["p5"]*(p["p4"]*y["B"]*p["p10"])/(p["p5"]+p["p6"]) -
  Cd <- (p["p1"]+p["p7"])*y["A"]*y["D"] -
  Dd <-p["p9"]*y["C"] - p["p7"]*y["A"]*y["D"]
  list(c(Ad, Bd, Cd, Dd))

out2 <- lsoda(y, times, func2, parms)

> out2[c(1:10,1000),]
       time            A            B            C             D
 [1,]  0.00 2.500000e-06 2.500000e-06 1.700000e-06  5.700000e-07
 [2,]  0.05 2.432561e-06 2.500379e-06 1.671689e-06  5.312515e-07
 [3,]  0.10 2.368078e-06 2.499286e-06 1.639363e-06  4.980014e-07
 [4,]  0.15 2.306621e-06 2.496841e-06 1.603709e-06  4.697523e-07
 [5,]  0.20 2.248381e-06 2.493370e-06 1.566611e-06  4.451401e-07
 [6,]  0.25 2.193360e-06 2.488923e-06 1.528312e-06  4.239702e-07
 [7,]  0.30 2.141477e-06 2.483726e-06 1.489881e-06  4.053217e-07
 [8,]  0.35 2.092710e-06 2.477833e-06 1.451555e-06  3.889875e-07
 [9,]  0.40 2.046809e-06 2.471378e-06 1.413729e-06  3.744582e-07
[10,]  0.45 2.003632e-06 2.464438e-06 1.376627e-06  3.614428e-07
[11,] 49.95 2.675890e-06 5.563057e-08 1.595362e-09 -7.457989e-11

In the event the results of lsoda and rk4 differ, I would trust the
lsoda result over that of rk4 (see the warnings in the help file for

On June 10, 2004, Chris Knight wrote:

[snip ...]
I'm trying to use odesolve for integrating various series of coupled 1st
order differential equations (derived from a system of enzymatic
catalysis and copied below, apologies for the excessively long set of

The thing that confuses me is that, whilst I can run the function rk4:

out <- rk4(y=y,times=times,func=func, parms=parms)

and the results look not unreasonable:

for (i in 2:(dim(out)[2]))plot(out[,1],out[,i], pch=".", xlab="time",

If I try doing the same thing with lsoda:

out <- lsoda(y=y,times=times,func=func, parms=parms, rtol=1e-1, atol=

I run into problems with a series of 'Excessive precision requested'
warnings with no output beyond the first time point.

Fiddling with rtol and atol doesn't seem to do very much.

What is likely to be causing this (I'm guessing the wide range of the
absolute values of the parameters can't be helping), is there anything I
can sensibly do about it and, failing that, can I reasonably take the
rk4 results as being meaningful?

Any help much appreciated,
Thanks in advance,


[code deleted...]

Dr. Christopher Knight                               Tel:+44 1865 275111
Dept. Plant Sciences                                     +44 1865 275790
South Parks Road
Oxford     OX1 3RB                                   Fax:+44 1865 275074
` · . , ,><(((º>

R. Woodrow Setzer, Jr.                        Phone: (919) 541-0128
Experimental Toxicology Division             Fax:  (919) 541-4284
Pharmacokinetics Branch
NHEERL B143-01; US EPA; RTP, NC 27711

More information about the R-help mailing list