# How can I use generated time vector in the main program in subprogram ?

1 view (last 30 days)
Paulina Wienchol on 8 Oct 2021
Commented: Star Strider on 8 Oct 2021
Hello,
I have a problem using the ode45 solver, namely, with a time step.
I have the main program, in which using an ode45 solver I solve a system of differential equations, defined in another subprogram.
In the subprogram I need to use in my equations time step (dt) generated by ode45, however, when I define variable 't' in the subprogram, Matlab see it as a scalar, not a vector, and does not use dt (derivative of time), but the specific time. So my question is, it is possible to call the time vector in the subprogram and use a derivative of time to indirectly calculate the system of ODE?
I hope that it is clear for you. Please, if you have any idea how I can fix it, write me :)
Steven Lord on 8 Oct 2021
The code is useful to see, but it shows how you think the problem that you're trying to solve should be solved. It doesn't show us the actual problem that you're trying to solve, so we can't tell how well what you've written matches what you want to do. As an analogy:
• If you hand me a plate of delicious cookies and you were trying to bake cookies, you've succeeded.
• If you hand me that same plate of cookies but you were trying to bake an apple pie, you haven't succeeded.
So now that you've shown us your plate of cookies (your code), show us the mathematical equations that you're trying to solve, like so we can tell what you were trying to bake.

Star Strider on 8 Oct 2021
I do not understand the question you are asking.
It is certainly possible to pass a time vector to a different function that the numerical differential equation solvers can use. See for example Parameter Estimation for a System of Differential Equations where the time vector is passed to the objective function, ode45 integrates the system of differential equations using it, and returns a matrix that is then used to fit the data.
It is also possible to use a time vector that the numerical differential equation solvers create, in other functions and assignments.
.
Star Strider on 8 Oct 2021
The numerical differential integration funcitons use adaptive time steps, however they will output their results at specific times if ‘tspan’ is a vector of more than 2 elements, as the code I linked to demonstrates. The derivatives are as ‘process’ here creates them, and the time instants are supplied by ode45 here. There is no need to do anything with the time argument unless it is used in the calculation of the differential equations (and it does not appear to be here).
.

R2020a

### Community Treasure Hunt

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

Start Hunting!