I am importing a set of differential and stochastic equations from an external package, and want to use Mathematica to analyse this.

I am struggling building the set of equations that I can use with NDSolve, especially because I cannot stop one equation from evaluating prematurely.

A simple explanation; the below set of functions would be the result from fitting a empirical dataset and transforming this fit into something suitable for use with NDSolve:

  a[t] -> Length[Select[RandomVariate[NormalDistribution[x[t], 2],10], #1 > .3&]],
  b[t] -> Length[Select[RandomVariate[NormalDistribution[a[t], 10],10], #1 > .3 &]]

I do however never arrive at this stage, as the rhs will evaluate into 0 or 1 so I use Hold to keep the above rhs from evaluating into something incorrect as the variables in Length, RandomVariate and NormalDistribution are not defined in this context (only within the NDSolve context).

I changed the transformation to result into:

inject = {
   Hold[a[t]] -> Hold[Length[Select[RandomVariate[NormalDistribution[x[t], 2],10], #1 > .3 &]]], 
   Hold[b[t]] -> Hold[Length[Select[RandomVariate[NormalDistribution[a[t],10],10], #1 > .3 &]]]

What I want to achieve is to build something like the following expression (in reality it will be a much higher order):

  {x'[t] == b[t] Sin[a[t] t], x[0] == 0, b[0] == 1, a[0] == 0,
   WhenEvent[Mod[t, .01] == 0,
     a[t] -> Length[Select[RandomVariate[NormalDistribution[x[t], 2], 10], # > .3 &]], 
     b[t] -> Length[Select[RandomVariate[NormalDistribution[a[t]], 10], # > .3 &]]
    }]}, x, {t, 100}, DiscreteVariables -> {a, b}]

I was naive enough to hope something like the below would work, which seems not to be the case as the events are not dynamically updating the states ....

  {x'[t] == b[t] Sin[a[t] t], x[0] == 0, b[0] == 1, a[0] == 0,
   WhenEvent[Mod[t, .01] == 0,
    }]}, x, {t, 100}, DiscreteVariables -> {a, b}]

Is there a way to "inject" this into NDSolve? What should I change when using ReleaseHolddoes not work, or is changing the structure of the expression to be injected the way to go?

I tried using Strings but that creates a lot of complexities in the variuous transformations I have to do, to arrive at the expression to be injected.

Many thanks!

  • 3
    $\begingroup$ Have you tried RuleDelayed, a[t] :> ... etc. without all the Hold stuff? $\endgroup$ – Michael E2 Apr 27 '16 at 12:13
  • $\begingroup$ @Michael E2 Thanks for considering my question, the a[t]:> rhs construction would lead to the same issue, being that the rhs will evaluate to 0 or 1 before injecting. So I would inject a[t]->1 in my case and a[t]:>1 in your suggestion. Probably my question should be phrased more generic, however I am able to solve some scoping problems and I am struggling with this specific one. So I do not understand what is the scoping problem I am dealing with in the specific WhenEvent case, hence a more specific question. $\endgroup$ – Sander Apr 28 '16 at 0:30
  • $\begingroup$ @Michael E2 I update my question to put more emphasis on this. $\endgroup$ – Sander Apr 28 '16 at 0:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.