I am solving with ODE45 diff equations system. How can i change a paramter in time for 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...

 Risposta accettata

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.

7 Commenti

so i put in my function [dz] = dYfdYdXdTgdTp(t,z)
alfaa = 23950.*(z(2)>0.32) - ((abs(t)+eps) + 106989).*(z(2)<=0.32);
dz(1)=alfa*....z(1)....z(4) dz(2)=z(1).. dz(3)=alfaa...z(3)...z(4) dz(4)=alfaa*.... z(3)...z(4)
so this alfaa works but after a reach 0.32 the solving stuck and try to go forwards but around 328 s ...cant reach 329
the ode time step cahnges to too low value?
tspan=[0 500];
[t,z]=ode45(@dYfdYdXdTgdTp,tspan,[y0 X0 Tg0 Tp0]);
so the value of time is disp it and stuck around 328....and i cant reach my tspan 500 ....so doesnt run... what could be the problem?
otherwise if i add alfaa=5 the program run for 500 or whatever i put in tspan.... the problem is inside ode45?
so it started the changing as you say but cant run ....
alfaa = 23950.*(z(2)>0.32) - (11782*log(abs(t)+eps) + 106989).*(z(2)<=0.32);
intresting if i put:
alfaa = 23950.*(z(2)>0.32) + 2792.27 + 35100.4*exp(-0.000846445*t).*(z(2)<=0.32);
so i put a + .....the program run......not good but run....and i changed the
- (11782*log(abs(t)+eps) + 106989);
to
2792.27 + 35100.4*exp(-0.000846445*t)
You know your problem better than I do. (I cannot follow the code you posted.)
I simply Answered what I believed your Question was about ‘alfa’ and how to get it to produce the result you want at various values of ‘z(2)’.
If your code works, go with it!
thanks!! so mushc help! the thing you written is working!!!
sorry for the questions....but i have another one......what if i want to put this in the function
for example
i want to make an alpha so if ...i reach a certain value want to calculate
alfaa = 23950.*(z(2)>0.32) + 2792.27 + 35100.4*exp(-0.000846445*t).*(z(2)<=0.32);
BUT not t but z(1) for example
so i want to calculate each new alpha with a new ode45 solved value
alfaa = 23950.*(z(2)>0.32) + 2792.27 + 35100.4*exp(-0.000846445*z(1)).*(z(2)<=0.32);
This is not working properly for me....how can i put this in to ode45 for each step?
My pleasure!
I am not certain that I understand ‘not t but z(1)’
If you want to use ‘z(1)’ in p;lace of ‘t’, use the ‘alfaa’ assignment as you wrote it.
That it ‘is not working properly for me’ does not give me any information I can use to help you.
I have no idea what you are doing, so you may simply have to experiment with it until you get the result you want.
I cannot guess what you want to do.
Thank you very much! Working you are awesome. Pls keep help others too! :)
As always, my pleasure!
I will do my best!

Accedi per commentare.

Più risposte (1)

how can i pass alfaa inside ode45 to a workspace vector?

1 Commento

You wouldn’t have to pass it from ode45 to your workspace. Just use the values of ‘z(:,2)’ and time to calculate in your workspace. The values of ‘z(:,2)’ and time should be the same, so just feed the two vectors to an anonymous function version of ‘alfa’ to calculate the value at corresponding values of the two variables.
For example:
alfa = @(z,t) 23950.*(z>0.32) - (11782*log(abs(t)+eps) + 106989).*(z<=0.32);
alfaa = alfa(z(:,2), t);
I would try that first.
Alternatively, you could write it to a .mat file from within your ODE function and then load it into your workspace later. The problem is that ode45 is an adaptive solver, so saving what you calculated from within the workspace of your ODE function to your workspace directly may not produce the results you want.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by