I am trying to to write a wrapper function for the ode solver (under the package desolve) to enable it to take multivariate arrays. I know how to do it for 1 dimension arrays but my code breaks down when I try to do it for 2 dimensional arrays. Here is my code diffwrap<-function(t,y,mu)vdpol(t=t,A[1:3,1:4]<-y[1:12],B[1:12]<-y[13:24],mu=mu) vdpol<-function(t,A,B,mu) { list(c(mu, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, A[1,1], A[2,1], A[3,1], A[1,2], A[2,2], A[3,2], A[1,3], A[2,3], A[3,3], A[1,4], A[2,4], A[3,4]) ) } stiff<-ode(y=rep(0,24),times=c(0,1),func=diffwrap,parms=1) I get keep getting the error message variable A[1,1] not found.
David Winsemius
2012-Jul-03 12:30 UTC
[R] Wrapper function for multivariate arrays for ode
On Jul 2, 2012, at 10:09 PM, Tjun Kiat Teo wrote:> I am trying to to write a wrapper function for the ode solver (under > the package desolve) to enable it to take multivariate arrays. I know > how to do it for 1 dimension arrays but my code breaks down when I try > to do it for 2 dimensional arrays. Here is my code > > > diffwrap<-function(t,y,mu)vdpol(t=t,A[1:3,1:4]<-y[1:12],B[1:12]<- > y[13:24],mu=mu)I do not know whether this explains your problems , but it generally is very dangerous practice to have items in function argument lists like: A[1:3,1:4]<-y[1:12] and B[1:12]<-y[13:24] I've often received similar messages when I mistaken entered "<-" rather than "=" in that situation. The code runs with this definition (after loading pkg:deSolve rather than 'desolve'): diffwrap<-function(t,y,mu) vdpol(t=t, A = matrix(y[1:12], 3,4), B=y[13:24], mu=mu) Whether the output is sensible mathematically is beyond my knowledge, -- David> > vdpol<-function(t,A,B,mu) > { > list(c(mu, > 2, > 3, > 4, > 5, > 6, > 7, > 8, > 9, > 10, > 11, > 12, > A[1,1], > A[2,1], > A[3,1], > A[1,2], > A[2,2], > A[3,2], > A[1,3], > A[2,3], > A[3,3], > A[1,4], > A[2,4], > A[3,4]) > ) > } > > stiff<-ode(y=rep(0,24),times=c(0,1),func=diffwrap,parms=1) > > > I get keep getting the error message variable A[1,1] not found. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT