2nd function inside ode45 function

3 views (last 30 days)
Nader
Nader on 17 Apr 2014
Answered: Star Strider on 17 Apr 2014
Hi everyone,
I really appreciate if someone could help me with my MATLAB problem. I'm using ode45 to solve a system of equations. The part of code I have a question about is below. I need to call a function named "kinematics" inside my ode function. In order to feed the correct displacement and speed values to this function, I need to know the index of time "t" in the input of ode45. Do you know how I can control and see the index of time in the input?
>function dy = ODEpiston(t,y)
>...
>%i = find(t);
>KIN = kinematics(displacement(i),speed(i));
>...
>end
--------------------------------
I appreciate your help. Nader

Answers (1)

Star Strider
Star Strider on 17 Apr 2014
The value of your independent variable, in your description t, that the ODE solver passes to it is available inside your ODE function. It is a single scalar at each iteration of your ODE function, so you do not have to assign it a dimension. (Giving it a dimension might actually throw an error.)
The find call should probably go inside your kinematics function if it has to look up something to do at a particular value of t. Pass it the value of t as an argument.
If displacement and speed are also calculated by your ODE function (which seems logical), you can pass them at each iteration as well, without the subscript indices. I don’t know how your functions work, so you may have to experiment with them to get the result you want, but passing values that are passed to or generated by your ODE function to kinematics should not be a problem.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!