ode45 output variable problem

4 vues (au cours des 30 derniers jours)
TOMAS SERGIO ORTIZ COLCHON
Modifié(e) : Jan le 30 Avr 2021
I'm using ode45 to solve a differential equation system, nevertheless, I need to save some variables that are created during each step time. At first, I tried to keep those variables using global variables but ode sometimes failed during the integration and repeat some steps times which drives me to have components in my global variable that doesn't match with the definitive steps times of ode solution. I've been trying to use outputfcn however, ode calls this function for example with a time vector of 4 valid steps times instead of the last valid step time. I thought ode would only call outputfcn during the last valid step time so I would be able to keep this step time variable value but if ode calls with 4 step times I will loose 3/4 of the step times values of the variable I need.
The only solution I see to this problem is to repeat the calculation of this variable once ode has finished because to do that I only need ode time vector and solution vectors but it will extremely increase time cost because to calculate the variable I generate during ode step times I have to solve a 25 non linear equation system in each step time.
I attached the input function of my ode. I would like to have ''voltaje'', ''etaprop'' and ''I'' vectors but currently with global variable I only can kept them with the extra step times that ode use to solve the problem. Is there any way to know when a step time is not valid to don't kept this one ?.
[t2,y2,T2E,Y2E]=ode45(@(t,x)funTrans(t,x,rho_0,g,Mzf,S,mur,Vstall,Vlof,dV_dt,theta,pi_c,alpha,a_flaps,LG_up,R,avion,W,k_ciclo,repart,serie,paralelo,Vo,m,m2,A,B,Q,Ka,tsigm),tspan,x0,misops);
tspan=[tiempo1(end),tf2+tiempo1(end)]; x0=[y1(end,1);y1(end,2);0;y1(end,3);0.001];
function y=funTrans(t,x,rho_0,g,Mzf,S,mur,Vstall,Vlof,dV_dt,theta,pi_c,alpha,a_flaps,LG_up,R,avion,W,k_ciclo,repart,serie,paralelo,Vo,m,m2,A,B,Q,Ka,tf1)
global tiempo
global I
global delta_t
global voltaje
global L
global etaprop
mfuel2=x(1);V2=x(2);x_trans=x(4);h_trans=x(3); gamma=x(5);
tiempo(L,1)=t;
dh_dt2=V2*sin(gamma);
dX_dt2=V2*cos(gamma);
dV_dt2=dV_dt;
n=A+(Ka-A)/(1+Q*exp(-B*(t-tf1)))^(1/1);
dgamma_dt=(n-cos(gamma))/V2*g;
[cd_0,K,~,~,~,~]=drag(h_trans/0.3048,V2,a_flaps,LG_up,R,avion);
T2=0.5*rho_0*V2^2*S*cd_0+K*g^2*((Mzf+mfuel2)*n)^2/(1/2*rho_0*V2^2*S)+g*(Mzf+mfuel2)*sin(gamma)+(Mzf+mfuel2)*dV_dt;
Tcomb=T2*repart; Tbat=T2-Tcomb;
etabox=0.94;
if avion==5
[TSFC,etaprop(L,1)]=modelo_motor(h/0.3048,V,Tcomb);
elseif avion==3
[TSFC,etaprop(L,1)]=modelo_motorATR(h/0.3048,V,Tcomb);
elseif avion==1
[TSFC,etaprop(L,1)]=modelo_motorB200(h/0.3048,V,Tcomb);
end
Ph_bat=Tbat*V2/etabox/etaprop(L,1);
Pot_bat=generador_alternador_interp1(Ph_bat,0)*0.98;
if L==1
I(1)=((2.5*m2+Vo)-sqrt((2.5*m2+Vo)^2-4*Pot_bat/serie/paralelo*m2))/(2*m2);
voltaje(1)=Vo-m2*(I(1)-2.5);
else
delta_t(L-1,1)=(tiempo(L,1)-tiempo(L-1,1))/3600;
delta_t(L,1)=delta_t(L-1,1);
I(L,1)=((2.5*m2+Vo-m*sum(delta_t(1:L-1).*I(1:L-1)))-sqrt((2.5*m2+Vo-m*sum(delta_t(1:L-1).*I(1:L-1))).^2-4*Pot_bat/serie/paralelo*(m2+m*delta_t(L-1))))/(2*(m2+m*delta_t(L-1)));
voltaje(L,1)=Vo-m2*(I(L,1)-2.5)-m*sum(delta_t(1:L-1,1).*I(1:L-1,1));
end
L=L+1;
dM_dt2=-TSFC*Tcomb;
y=[dM_dt2;dV_dt2;dh_dt2;dX_dt2;dgamma_dt];
end
  2 commentaires
Stephen23
Stephen23 le 30 Avr 2021
"The only solution I see to this problem is to repeat the calculation of this variable once ode has finished..."
That is the simplest, easiest, and most common solution.
"Is there any way to know when a step time is not valid to don't kept this one ?"
Not easily. You could store all of the intermediate data and perform some kind of selection/interpolation of the data afterwards, but it would not be trivial. Note also that ODE solvers can choose arbitrarily small steps and even go backwards.
Jan
Jan le 30 Avr 2021
Modifié(e) : Jan le 30 Avr 2021
"I've been trying to use outputfcn however, ode calls this function for example with a time vector of 4 valid steps times instead of the last valid step time." - Why do you assume this? As far as I know, the ode integrators call the output function for each successful step.
Did you consider, that the ODE is evaluated multiple time for a successful step?
By the way, the IF command looks. like your function to be integrated is not smooth. ODE45 handles only smooth functions reliably. For discontinuities the step size controller can explode: The step size can get tiny, such that you get a huge number of steps together with a huge accumulated rounding error. Or the integrator skips the break and uses a bad derivative in this step. If you are lucky, ODE45 stops with a meaningful error message.
For a reliable result, you need an event function, which restarts the integrator at each discontinuity. Everything else does reply a number, but this is not a scientific way of using numerical software, but an expensive random number generator with a low entropy. Sorry for this critical opinion. I've seen to many publications based on such "simulations" without an control of the errors.

Connectez-vous pour commenter.

Réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by