How do you update variables in NDSolve for each step?

  • Thread starter xpress907
  • Start date
  • Tags
    Variables
In summary, the conversation discusses the need to update a variable, "rand", each time NDSolve takes a step. The desired outcome is for "rand" to have a different value for each step in the internal integration. The solution involves creating a function, "rand[x_?NumericQ]", that uses RandomReal[] and is parsed as a function of x in NDSolve. Using Evaluate[] may also be a viable option.
  • #1
xpress907
2
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
  • #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.
 
  • #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. 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]]
 
  • #4
You could also probably wrap it in an Evaluate[].
 
  • #5


To update a variable within NDSolve for each step, you can use the "WhenEvent" option. This option allows you to specify an event that will trigger a change in the variable value. In this case, we can use the "TimeStep" event which will trigger at each time step taken by NDSolve. The updated "rand" value can be generated within the "WhenEvent" function using RandomReal[].

Here is an example:

rand = RandomReal[];
Solution = y /. Flatten[NDSolve[{y'[x] == .25 rand, y[0] == 1,
WhenEvent[TimeStep[x], rand = RandomReal[]]}, y, {x, -10, 10}]];

Plot[Evaluate[Solution[x]], {x, -10, 10}, PlotRange -> {-1.5, 1.5}]

This will update "rand" at each time step and use the updated value in the differential equation. You can also specify different conditions for when the event should trigger, such as a specific time or value of the variable "x". For more information on the "WhenEvent" option, you can refer to the NDSolve documentation.
 

1. How do you update variables in NDSolve for each step?

In order to update variables in NDSolve for each step, you can use the "WhenEvent" function to specify when and how the variables should be updated. This function allows you to specify conditions and actions that should be taken when those conditions are met. Alternatively, you can also use the "DiscreteVariables" option in NDSolve to update variables at specific time steps.

2. Can I update multiple variables at a time in NDSolve?

Yes, you can update multiple variables at a time in NDSolve by using the "WhenEvent" function or the "DiscreteVariables" option. You can specify different conditions and actions for each variable, or update them simultaneously at specific time steps.

3. What happens if I don't update the variables in NDSolve?

If you don't update the variables in NDSolve, they will retain their initial values throughout the integration process. This can lead to inaccurate results, as the variables may not accurately reflect the changing conditions of the system.

4. Can I update variables in NDSolve based on the values of other variables?

Yes, you can update variables in NDSolve based on the values of other variables by using the "WhenEvent" function. This allows you to specify conditions for when the update should occur, including conditions involving other variables.

5. Is there a specific order in which variables are updated in NDSolve?

The order in which variables are updated in NDSolve depends on the specific conditions and actions specified in the "WhenEvent" function or the "DiscreteVariables" option. If multiple variables are updated at the same time, they will be updated simultaneously. Otherwise, the variables will be updated in the order specified by the conditions and actions.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Calculus
Replies
29
Views
734
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top