Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How do you update variables in NDSolve for each step?

  1. May 22, 2012 #1
    Im using NDSolve. I need to be able to update a variable inside NDSolve each time NDSolve takes a step. "rand" is the variable I want to update each step. I want it to be a different number each step. The below is an example that illustrates what I have currently happening ("rand" does not update for each step).
    Code (Text):

    Solution = y /. Flatten[ NDSolve[{y'[x] == .25 rand, y[0] == 1}, y, {x, -10, 10}]]
    Plot[Evaluate[Solution[x]], {x, -10, 10}, PlotRange -> {-1.5, 1.5}]
    Instead of picking 1 random variable for "rand" for the entire x domain, I want "rand" to be different for each step i.e. a different "rand" for x=1, x=2, x=3, etc.
    Last edited: May 22, 2012
  2. jcsd
  3. May 22, 2012 #2
    When you say each step, do you mean each step in x i.e step in the internal integration?

    If so then the only way I can think of at the moment (not even sure if this is what you want) is to generate a table of random points, each associated with an x value and parse that as a function of x, i.e.

    y'[x] = 0.25 f(x) where f(x) maps to your interpolated random set.
  4. May 22, 2012 #3
    yes, when i say step i mean each step in x inside of NDSolve.

    I think i may have found an answer to this. Im posting it incase anyone else should ever find this and need help.

    The ?NumericQ prevents the expression simply getting replaced by RandomReal[].

    rand[x_?NumericQ] := RandomReal[];
    Solution = y /. Flatten[NDSolve[{y'[x] == .25 rand[x], y[0] == 1}, y, {x, -10, 10}, PrecisionGoal->0]]
  5. May 22, 2012 #4
    You could also probably wrap it in an Evaluate[].
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook