MATLAB: I am solving with ODE45 diff equations system. How can i change a paramter in time for ODE45

solve ode45 paramter function equation change parameter in ode45

hello,
I am solving with ODE45 diff equations system. How can i change a paramter in time for ODE45?
I am solving for z, 1,2,3,4—–and i got a paramter in this equations alfa……after a while i need a different alfa which changes with: alfa=-11782*log(t) + 106989; t is time……so i want to solve ode if my z(2) is a certain calue after that i need alfa t change…..how can i solve this HELP me pls?
% if (z(2)>0.32)
% alfa=23950;
% else
% % alfa=-11782*log(t) + 106989;
% end
tspan=[0 500];
[t,z]=ode45(@dYfdYdXdTgdTp,tspan,[y0 X0 Tg0 Tp0]);
alfa is in this @dYfdYdXdTgdTp function..
so i need something that after z(2) value reaching a certain value…..change my alfa in each step…whith an equation…

Best Answer

If I understand correctly what you want to do, defining ‘alfa’ in your ‘dYfdYdXdTgdTp’ ODE function as:
alfa = 23950.*(z(2)>0.32) - (11782*log(abs(t)+eps) + 106989).*(z(2)<=0.32);
should work.
I added ‘(abs(t)+eps)’ to avoid ‘alfa’ throwing errors if ‘t’ is negative or zero. (Note than with the ODE solvers, negative and zero values for ‘t’ are permitted.) It will not affect your results.
Related Question