Info

この質問は閉じられています。 編集または回答するには再度開いてください。

How to code an ODE System with paramters depending on variables

2 ビュー (過去 30 日間)
Aleks Boehler
Aleks Boehler 2020 年 7 月 19 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hey guys!
I'm trying to simulate a system of differential equations. For that I made the function:
function dYdt = System(t,Y)
i_L = Y(1);
u_cp = Y(2);
u_cs = Y(3);
u_A = Y(4);
C_A = 28e-9;
C_p = 251e-12;
C_s = 390e-12;
R = 400;
L = 370e-6;
I_Load = 4;
di_Ldt = (u_e-u_cs-u_cp)/L;
du_cpdt = (1/C_p)*(i_L-a*((i_L*C_A+C_p*(I_Load+(u_A/R))*sign(u_cp))/(C_p+C_A)));
du_csdt = i_L/C_s;
du_Adt = abs(i_L)*a*(C_A/(C_A+C_p))-(I_Load-(u_A/R))*(1-a*(C_A/(C_A+C_p)));
dYdt = [di_Ldt; du_cpdt; du_csdt; du_Adt];
end
So far so good, but as you might have noticed the variables a and u_e are not declared anywhere.
The problem is, that 'a' depends on 'i_L', 'u_cp' and 'u_cs' and 'u_e' depends on 'i_L'. For that i made functions for each variable.
function aa = a_Function(i_L, u_cp, u_cs)
if i_L == 0
aa = 0;
end
if abs(u_cp) == u_cs
aa =1;
end
end
and
function u_e = u_e_Function(i_L)
if i_L >= 0
u_e = 400;
else
u_e = -400;
end
end
I am not sure where to put those functions. Do I put those inside the System- function or I declare them in the main file, where I execute the Simulation. Because to get the variables a and u_e I need first to put the ODE- System through a Solver (for example ode45), but for that die ODE's need a and u_e.
Does anyone have any suggestions?

回答 (1 件)

Alan Stevens
Alan Stevens 2020 年 7 月 19 日
編集済み: Alan Stevens 2020 年 7 月 19 日
You can include the body of each "extra" function directly in the main function:
function dYdt = System(t,Y)
i_L = Y(1);
u_cp = Y(2);
u_cs = Y(3);
u_A = Y(4);
C_A = 28e-9;
C_p = 251e-12;
C_s = 390e-12;
R = 400;
L = 370e-6;
I_Load = 4;
if i_L == 0
a = 0;
end
if abs(u_cp) == u_cs
a =1;
end
if i_L >= 0
u_e = 400;
else
u_e = -400;
end
di_Ldt = (u_e-u_cs-u_cp)/L;
du_cpdt = (1/C_p)*(i_L-a*((i_L*C_A+C_p*(I_Load+(u_A/R))*sign(u_cp))/(C_p+C_A)));
du_csdt = i_L/C_s;
du_Adt = abs(i_L)*a*(C_A/(C_A+C_p))-(I_Load-(u_A/R))*(1-a*(C_A/(C_A+C_p)));
dYdt = [di_Ldt; du_cpdt; du_csdt; du_Adt];
end
  10 件のコメント
Alan Stevens
Alan Stevens 2020 年 7 月 19 日
One other thing I've noticed:
Your u_cp and u_cs are both floating point values. It's best not to do equality tests for these because of small rounding (or other) errors. Best to replace the absolute value of the difference with a small tolerance.
i.e. replace
if abs(u_cp) == u_cs
a = 1;
end
with
if abs(u_cp-u_cs) < tol
a = 1;
end
where you replace the word 'tol' with a small number (e.g. 10^-3 or 10^-6 or ... whatever is appropriate for your system).
Alan Stevens
Alan Stevens 2020 年 7 月 19 日
I didn't get the 'not enough input parameters' message.

この質問は閉じられています。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by