How do you update variables in NDSolve for each step?

  • Thread starter Thread starter xpress907
  • Start date Start date
  • Tags Tags
    Variables
Click For Summary

Discussion Overview

The discussion focuses on how to update a variable within the NDSolve function in Mathematica for each integration step. Participants explore methods to ensure that a variable, specifically a random number, changes at each step of the integration process.

Discussion Character

  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant describes their initial attempt to use a static random variable "rand" in NDSolve, which does not change during the integration.
  • Another participant suggests generating a table of random points associated with x values and using an interpolated function to map these points, implying a potential solution to the problem.
  • A participant later proposes a solution involving the use of a function definition for "rand" that incorporates NumericQ to ensure a new random value is generated for each step in x.
  • Another suggestion is made to potentially wrap the random variable in Evaluate[] to achieve the desired behavior.

Areas of Agreement / Disagreement

Participants do not reach a consensus on a single method, as multiple approaches are proposed and discussed without clear agreement on which is optimal.

Contextual Notes

Some methods discussed depend on specific implementations and assumptions about the behavior of NDSolve and the handling of random variables within it. The effectiveness of the proposed solutions may vary based on the context of use.

Who May Find This Useful

This discussion may be useful for users of Mathematica who are working with NDSolve and need to incorporate dynamic or random variables into their differential equations.

xpress907
Messages
2
Reaction score
0
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:
rand=RandomReal[]
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:
Physics news on Phys.org
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.
 
yes, when i say step i mean each step in x inside of NDSolve.

I think i may have found an answer to this. I am 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]]
 
You could also probably wrap it in an Evaluate[].
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
9K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K