How can I use generated time vector in the main program in subprogram ?
显示 更早的评论
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 :)
3 个评论
Steven Lord
2021-10-8
Please show us the mathematical form of the system of ODEs that you're trying to solve.
In general you shouldn't be using information about the size of the timestep from ode45 as part of your ODE function. If you did that you would need to handle negative timesteps if ode45 decided to reject a step and take a smaller step at the next function evaluation.
Paulina Wienchol
2021-10-8
Steven Lord
2021-10-8
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.
回答(1 个)
Star Strider
2021-10-8
0 个投票
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.
.
4 个评论
Paulina Wienchol
2021-10-8
Star Strider
2021-10-8
I still have no idea what you are doing, or the problem you are having.
.
Paulina Wienchol
2021-10-8
Star Strider
2021-10-8
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).
.
类别
在 帮助中心 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!