Changing the initial variables when passing into an ode45 solver

조회 수: 1 (최근 30일)
Hariharan MK
Hariharan MK 2021년 1월 13일
댓글: Hariharan MK 2021년 1월 14일
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!

채택된 답변

Steven Lord
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 CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by