Info
This question is closed. Reopen it to edit or answer.
How to code an ODE System with paramters depending on variables
1 view (last 30 days)
Show older comments
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?
0 Comments
Answers (1)
Alan Stevens
on 19 Jul 2020
Edited: Alan Stevens
on 19 Jul 2020
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 Comments
Alan Stevens
on 19 Jul 2020
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).
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!