Changing the initial variables when passing into an ode45 solver

11 views (last 30 days)
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?
I am currently doing modifiction based on this particular file found on file exchange.
Appreciate your help!

Accepted Answer

Steven Lord
Steven Lord on 13 Jan 2021
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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!