0

I am fairly new in R I am trying to plot a differential equation and further in same plot I have to do this around 4 time. I have tried following way

m = sum(C[])
s= 4   #number of time curve in single plot
for(i in 1:length(s)){
b = 0.1;X = sample(c(-1,1), replace=T,1); sig = runif(0.01, min= 0.001, max = 0.01);yini <- 1
derivs <- function(t,y, parms)
list(tanh(b*m)- X + sig)   #dx/dt = tanh(b*m)- X + sig (differential equation)


times <- seq(from = 0, to = 10, by = 0.5)

library(deSolve)
out <- ode(y = yini, times = times, func = derivs, parms = NULL)
}
head(out)
plot(out, main = "logistic growth", lwd = 2)

I am getting following error Error in checkFunc(Func2, times, y, rho) : The number of derivatives returned by func() (0) must equal the length of the initial conditions vector (1)

KR12
  • 131
  • 1
  • 1
  • 8
  • if you re-arrange/re-format your code it will become easier to follow. it looks like your loop stretches all the way to after `out <- ...` - that looks strange, because you are loading a library and `out` just keeps being overwritten without any usage in the following iteration. Also the definition of your `derivs` function looks strange. – RolandASc Feb 21 '18 at 09:25
  • What's `C` in the first line? It's generally not a good idea to use that as a variable name since `c` means combine (see `?c`). What are you trying to produce with `runif(0.01, min= 0.001, max = 0.01)`? Trying running it by itself – it produces nothing. Use `=` or `<-` for assignment and stick with it. Flip-flopping between the two is a recipe for confusion. A [style guide](http://style.tidyverse.org/) is helpful for this sort of thing. You are defining constants and loading libraries inside a loop. These commands don't need to be run several times, so take them out. – Dan Feb 21 '18 at 18:18
  • Your loop will only run once since `length(s)` is equal to 1. Each loop you would overwrite `out` and so would not retain all model solutions. You need a container that will hold all of the solutions. – Dan Feb 21 '18 at 18:21

0 Answers0