How can I fix ODE45 returning a straight line when using specific `tspan` points, instead of showing dynamic behavior over the time range?
80 次查看(过去 30 天)
显示 更早的评论
How can I fix ODE45 returning a straight line when using specific `tspan` points, instead of showing dynamic behavior over the time range?
6 个评论
Torsten
2024-9-23,10:25
编辑:Torsten
2024-9-23,10:27
You ask me questions about the code that you made. How should I know better than you do ?
I set the initial condition for the new phase to the end value of the last phase. Maybe that's the problem.
for i = 1:numel(PhaseTimes)-1
PhaseTime = [PhaseTimes(i) PhaseTimes(i+1)];
% Define functions for forward and backward reaction rates
Kf_L = @(t) calculate_kf(t, PhaseTime, Kf_Max, RLC(i), TauKf_ON, TauKf_OFF);
Kb = @(t) calculate_kb(t, PhaseTime, Kb_Max, Kb_Min, RLC(i), TauKb_ON, TauKb_OFF);
[T,Y] = ode45(@(t,y)ode_LR(t,y,Kf_L,Kb),PhaseTime,initial_conditions, options);
initial_conditions = Y(end,:);
t = [t;T];
y = [y;Y];
end
Sam Chak
2024-9-23,11:33
Hi @Ehtisham
If you are unfamiliar with the material, it is advisable not to use AI-generated code (or code inherited from your PhD seniors or kind professor) to describe what you want to achieve, as the code may contain flaws of which you are unaware.
Instead, it will be clearer if you use the mathematical model of the RLC circuit to explain the problem and the parameters you wish to vary throughout the simulation. Click to insert image of the RLC math model.
Additionally, if the output responses are not as expected, consider applying a "divide-and-conquer" approach by simulating the RLC circuit in phases, individually. For example, if Phase 1 is successful, then introduce Phase 2 into the code, as Phase 2 depends on the results of Phase 1. Simulating all phases at once can complicate troubleshooting due to the various intertwined factors, including coding, human errors, and modeling issues.
回答(1 个)
Steven Lord
2024-9-20,12:56
Your ode_LR function is defined to accept four input arguments, the two required arguments plus two additional parameters.
function dydt = ode_LR(t, y, Kf_L, Kb)
How are you using it, based on what's displayed in the error message?
Error in SimfileNPhase>@(t,y)ode_LR(t,y,RLC(i)) (line 20)
[t, y] = ode45(@(t,y)ode_LR(t,y,RLC(i)),PhaseTime,initial_conditions, options);
Not only are you not calling it with enough input arguments (the anonymous function calls it with three inputs) the comment inside ode_LR implies that the Kf_L and Kb input arguments are function handles.
% Call the function handles for Kf_L and Kb
dNonActiveReceptor_dt = -Kf_L(t) * Non_Active_Receptor_concentration + Kb(t) * Active_Receptor_concentration;
Is RLC(i), the value that you pass into ode_LR as the third input argument, a function handle? No, it is not. It's a numeric array. And t (the value that ode45 passes into your function as the first required input) is not guaranteed to be a valid index into a numeric array.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Power Converters 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!