How to use DifferentialEquations with complicated equations

Let’s forget optimizing for a second and focus on getting it to run. There a a number of issues with this code, and I’ll second @ChrisRackauckas in encouraging you to work through some of the tutorials to get more familiar with how DifferentialEquations works. A few specific suggestions, though:

  1. Your notation and variable names are confusing (at least to me). For instance, your ODE function is called dNdT, which implies that the letter N represents biomass or population. However, you define your biomass vector as BioS above it, and then create a new variable pointing to the same vector called u0 below it. Additionally, you have two global constants called N1 and N2. I have a hunch some of your issues may be related to, or worsened by, unclear variable names.
  2. You are confusing state variables and parameters. State variables are the quanitities you are simulating; they change over the course of the model run. For DifferentialEquations they have to be in a single vector (this would be BioS, u, or whatever you decide to call it). Paramters are the model settings and constants which do not change during the simulation (D, K, N1, etc.). Here you have put some of both in the tuple p = (D, S, N1, N2, K, v1, v2, r, BioS).
    • Side note: because DifferentialEquations takes the parameters as an argument to ODEProblem, you don’t actually need to define them as global constants.
  3. Your main function dNdt doesn’t make sense to me. I think dN is supposed to be NumPlantSpecies + NumAnimalSpecies elements long…but you only update the first two elements, and the loop only goes over 1:NumPlantSpecies.
  4. K and r are only defined for the number of plant species. Is that what you want?
  5. r is based on a variable called BM which is not in this code snippet. Paste your code into a freshly started Julia REPL to make sure it runs as-is. Without a MWE, it is really difficult for us to figure out what your code is trying to do, or what isn’t working.
  6. You still haven’t explained exactly what you are trying to model. Is there a paper or reference where the equations you quoted above came from? Knowing that may help us to get you started.

Again, I’d really encourage you to work through a few tutorials. Make sure you understand what the equations mean before you try to transcribe them into code. Make sure your function dNdT works and produces the expected results when you call it on its own, before you define an ODEProblem. Start simple, make your model more complex in incremental steps, and only add new features or dynamics once you’ve got the simpler version working.

4 Likes