Changing the initial variables when passing into an ode45 solver
1 次查看(过去 30 天)
显示 更早的评论
Hello,
Based on this link, I learnt that the initial variables can be modified when passing into a solver. However, I have only been able to make use of "Y = mx + C" equations. Hence I have been only been able to increase or decrease the length linearly. How do I code such that the variable can modelled to be increasing linearly to plateu to become a constant and finally decrease back to its original initial value? (i.e shape of a trapezium )
So far i have created a function that holds the equation of the trapezium where the variable increases until a certain time step after which it remains constant before decreasing to its initial value.
function length_output = length_equation(t,x)
%parameters
L_0 = x(5); %initial length
HS = x(7); %rate of change of length
%increasing portion
if t<= 20
L = L_0 + HS*t;
elseif (t>20) && (t<40)
L = L + HS*t;
else
L = L - HS*t;
end
length_output = L;
The problem that I am currently facing is that the Previous L that was calculated is not stored hence, I keeping coming into errors that tells me that L is not defined at time step 21
Is there such a way that ode45 can accept changing variables that are not linear in nature?
Appreciate your help!
0 个评论
采纳的回答
Steven Lord
2021-1-13
I would call the ODE solver three times.
First, solve over the time span [0, 20].
Next, use the final value from that first ODE solver call as the initial condition for the second call that solves over the time span [20, 40].
Third, do the same as the second call but for the time span [40, your_end_time].
If you don't know when the changes will occur but have some way to detect when they should occur, use an events function. See the ballode example for an illustration of this technique.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!