[R-sig-dyn-mod] Simple doubt about how to use function ode

Thomas Petzoldt Thomas.Petzoldt at TU-Dresden.de
Sat Feb 26 10:25:06 CET 2011


Hello Augustos,

there is nothing wrong with your examples or with the used packages, at 
least not in a technical sense. The problem is indeed your mathematical 
understanding. Your observation simply shows the difference between the 
discrete (function logistico) and a continuous solution (function 
Logistic with ode).

As an example, you may compare an animal population that reproduces once 
a year (= discrete) with a bacterial population with millions of 
individuals that seem to reproduce continuously. The discrete model is 
also called a "difference equation" and the continuous form a 
"differential equation". Package deSolve is intended for solving 
differential equations, and it can be also used for difference equations 
by using the "euler" method.

Read sections 1.4, 3.1 and 3.2 of Henry Stevens book for the details.

A few additional comments are interspersed below.


On 2/25/2011 4:53 PM, Augusto Ribas wrote:
> Hello.
> I'm a ecology grad student in brazil.
> Well i'm trying to figure out how to use the function ode.
> I read the manual on pack page and tryed to copy the example to solve de
> logistc growth model to test if it work(work i mean i understood ^^) but i
> cant make it right.
> Then i got to this maillist by the pack page.
>
> So first i solved with a  function:
>
> logistico<- function(k,r,N0,t){
>   N<-c(N0,numeric(t))
>   for(i in 1:t) N[i+1]<-{
>   N[i] + r * N[i] * (1 - N[i]/k)
>   }
>   return(N)
>   }
>
> then i get the results for a k=100 and r=1 in 20 times starting with a pop
> of 2 individuals
>
> Nts<- logistico(100,1,2,20)
>
> to here its ok to me
> now i tryed to make a function to use with desolve as following:
>
> Logistic<-function(t, state, parameters) {
>   with(as.list(c(state, parameters)),{
>   dN<- r*n*(1-n/k)
>   list(c(dN))
>   })
>   }
>
> then tryed to solve
>
> library(desolve)

Please respect capitalization, i.e. library(deSolve)

> parameters<- c(r = 1, k = 100)
> state<- c(n = 2)
> times<-seq(0,20,by=1)
> out<- ode(y = state, times = times, func = Logistic, parms = parameters)
>
> but the results are different

this is natural, because Nts contains a discrete and out a continuous 
solution. Try the following for a discrete solution with deSolve:

out<- ode(y = state, times = times, func = Logistic, parms = parameters, 
method="euler")

And don't forget to consult the book of Stevens and a book about 
differential calculus to understand the difference.

> out<-cbind(out,Nts)
> matplot(x=out[,1],y=out[,2:3],xlab=c("Times"),ylab=c("Pop. Density"),
> type="l")
> legend(x=15,y=90,legend=c("Desolve","Function"),pch =19, col=c(1, 2))
>
>
> On the book Primer of ecology with R Stevens use desolve on levins
> metapopulation model a little bit different, takeing out the state on
> function like...
>
> Logistic<-function(t, state, parameters) {
> n<-state[1]   #<-------------------------------why this?
>   with(as.list(parameters),{
>   dn<- r*n*(1-n/k)
>   return(list(dn))
>   })
>   }

In programming there are always different ways to do the same thing. 
Both methods are intended to unpack the content of "state" and 
"parameters" so that their content can be used by their names (e.g. r, 
n, k).

While the first form:

with(as.list(c(state, parameters)), .....

unpacks both using the "with" function, the second form uses:

n<-state[1]

to "unpack" the state and

with(as.list(parameters), ......

to unpack the parameters. Both have their pros and cons if used for 
bigger models, and the use of either the one or the other for the 
logistic is only a matter of style.

Hope it helps

ThPe

-- 
---------------------------------------------
Dr. Thomas Petzoldt
Technische Universitaet Dresden
Faculty of Forest, Geo and Hydro Sciences
Institute of Hydrobiology
01062 Dresden, Germany

E-Mail: thomas.petzoldt at tu-dresden.de
http://tu-dresden.de/Members/thomas.petzoldt



More information about the R-sig-dynamic-models mailing list