Use global variables in ODE45
19 views (last 30 days)
Show older comments
edoardo trabaldo
on 6 Oct 2015
Commented: edoardo trabaldo
on 6 Oct 2015
Hello everyone!
I need to solve a differential equation where one of the functions is defined by me. This function needs to know the previous value in input and output of the function itself to calculate the next point.
When I use ODE45 to solve the differential equation with my function in it the results are uncomprehensible at best, my question is: anyone knows if the declaration of global variable still works when using ODE45?
Thank you and best regards,
Edoardo.
0 Comments
Accepted Answer
Walter Roberson
on 6 Oct 2015
Yes, global works in the functions called by ode45.
I would point out that your method cannot work if you call it from a solver that can move backwards in time (not unless you are working entirely backwards in time, of course.) It would also point out that use of the method is doubtful unless the time step is fixed and your function is called only once per time step. ode45 is a variable step solver. Especially when there are multiple elements in the y vector, ode45 may call the function multiple times per time step. Your function should not be dependent on the order in which it is called with different y at the same time step but if you define it as being dependent upon the immediately previous function call then it is implicitly dependent on the order of y values for a given time step.
3 Comments
Walter Roberson
on 6 Oct 2015
When you say the function needs to know the previous value in input and output of the function, then which input is needed, and which output is needed?
My speculation is that you might be trying to do a calculation based upon delta t
More Answers (0)
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!