[R-sig-dyn-mod] ode with variable parameters

Stephen Paul Ellner spe2 at cornell.edu
Thu Feb 9 15:47:51 CET 2017


A method I often use, is to use approxfun() or splinefun() to define time-varying parameters as continuous functions of time by interpolation. The function that computes the values of the derivatives then uses those continuous functions to compute parameters at any time points that the numerical integrator wants.  A simple example to illustrate:  

require(deSolve); 
tvals=seq(0,50,by=1); Kvals=exp(-0.01*tvals); # parameter value at discrete time points
Kfun = approxfun(tvals,Kvals); # parameter value at any time by linear interpolation 

# Logistic population growth in a declining environment
MWE = function(t,y,parms) {
	dy = y*(1-y/Kfun(t)) 
	return(list(dy))
}

out=ode(y=c(y=0.01),time=tvals,fun=MWE); plot(out);

This assumes that your discrete-time values of v and C come from data; if they are defined from functions, you just want to use those functions in the function that computes the time-derivatives. 

Steve

Stephen P. Ellner (spe2 at cornell.edu)
Department of Ecology and Evolutionary Biology
E339 Corson Hall, Cornell University, Ithaca NY 14853-2701
Phone (607) 254-4221    FAX (607) 255-8088

----------------------------------------------------------------------
Message: 1
Date: Mon, 6 Feb 2017 11:09:42 +0000
From: <Thomas.Hoffmann at bafg.de>
To: <r-sig-dynamic-models at r-project.org>
Subject: [R-sig-dyn-mod] ode with variable parameters
Message-ID: <08EB42B2E68E5C4080B590BCD274F9210103771C at S-MILE1.bafg.de>
Content-Type: text/plain; charset="us-ascii"

Dear all,

I would like to solve a simple diffusion-advection equation: dC/dt = v * C + gamma dC/dx

In which v and gamma changes through time. Basically I have a dataframe, which gives v and gamma at the time-steps that I use to solve the above equation with ode.1d.

How do I solve this problem dynamically with variable parameters.


Thanks for your help
Kind regards
Thomas



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