Let me solve an ODE .
dy/dt=y.
y(0)=1.
Now i want to just solve the ODE for 5 seconds. Let y(5) denote the value of state 'y' at time =5.
At t=5 I want to update the initial condition as: y(5)=0.5*y(5) and again solve the given ODE for next 10 seconds.
I want to use while statement for this work. Can I do that ? If yes, please provide some outline of the code.
Thanks

2 comentarios

James Tursa
James Tursa el 30 de Nov. de 2024
What have you done so far? What specific problems are you having with your code? Can you code up the first part, without any while loop, to just solve for the first 5 seconds?
Arnab
Arnab el 30 de Nov. de 2024
Actually I was solving solving some different problem where I need this concept. Can you please solve the above ODE that will help me out, u think.

Iniciar sesión para comentar.

 Respuesta aceptada

Cris LaPierre
Cris LaPierre el 30 de Nov. de 2024

0 votos

One of your inputs to ode45 is tspan. If I were going to do this, I would look at building is so that my loop calls ode45 with the desired inputs rather than trying to do it inside the odefcn.
Here is an outline (untested)
y0 = 0;
tspan = [0 5;5 15]
y_out = [];
for ts = 1:sizse(tspan,1));
[t,y] = ode45(odefcn,tspan(ts,:),y0);
y0 = o.5*y(end);
y_out = [y_out;y];
end

9 comentarios

Arnab
Arnab el 30 de Nov. de 2024
Well, at first thanks.
Sir, can you please provide the whole code to solve the above ODE? That will give me some more insight to the problem and implementing somewhere in my original work.
Thanks
Torsten
Torsten el 30 de Nov. de 2024
Editada: Torsten el 30 de Nov. de 2024
If you choose to integrate from 0 to 5 and afterwards from 5 to 15, the solution becomes that large that your plot doesn't reveal what's happening. So I chose to integrate from 0 to 1 and from 1 to 2 for clarity.
y0 = 1;
odefcn = @(t,y) y;
tspan = [0 1;1 2];
y_out = [];
t_out = [];
for ts = 1:size(tspan,1);
[t,y] = ode45(odefcn,tspan(ts,:),y0);
y0 = 0.5*y(end);
t_out = [t_out;t];
y_out = [y_out;y];
end
plot(t_out,y_out)
Arnab
Arnab el 1 de Dic. de 2024
Thank you very much @torsten. Can you please elaborate a bit how it's working, it will help me to learn the concept.
By the way I was trying something to apply this in my main code.
Should I provide the main code and ask the problem there only?
Walter Roberson
Walter Roberson el 1 de Dic. de 2024
Any one call to ode45() expects the implementing equations to be consistent, and to have continuous second derivatives.
In order to implement a discontinuity, it is necessary to stop the ode45 call at the boundary of the discontinuity, and adjust the boundary conditions, and then to call ode45 again to keep going.
If your implementing code had something like
if t == 5
y = y./2;
end
then that would be a point of discontinuity in the first and second derivatives. The ode45() code would, in such a case, fail with a message about being unable to meet boundary conditions if you were lucky. If you are unlucky it would not notice and would silently return the wrong answer.
Actually, considering that ode45() uses adaptive step sizes, it is unlikely to happen to probe at exactly t == 5 unless 5 is one of the two tspan boundaries. So nearly all of the time it would simply produce the wrong answer.
The simplest form of discontinuity is if the discontinuity happens to occur at a strict pre-determined time boundary. Which is the case here -- the discontinuity occurs at time 5, which is pre-determined. In this particular case, you can "simply" ask ode45() to process up to time 5 during one call, then change the boundary conditions outside of ode45(), and then call ode45 again with an updated tspan.
In more complicated cases, discontinuities occur at unpredictable times. An example is a bouncing ball: the exact time it is going to hit the floor or an outside wall is difficult to predict. In such cases, you have to specify "event" functions through the ode options structure. The events functions would detect the boundary and set its outputs to signal whether or not a boundary had been it, and what to do if it is hit; most often the "what to do" is to terminate ode45() execution. (Termination of execution is not immediate; instead ode45() probes the boundary conditions and integrates right up to the boundary.)
Arnab
Arnab el 1 de Dic. de 2024
Yah, thanks. I genraly use event function .
By the way I was trying to solve something lik following:
I have to solve ODE given by:
dy/dt= y- (|y|^0.5)*signum(y).
And T1=2; is some given constant.
Where event points are generated by
s_(k+1)={ t >=t and t<T1_k: y(t)=e^-(t_k)}
where t_k= s_k+ r_k, r_k is the delay term . And at t=t_k, y(t) should update the initial condition as
y(t_k)=y((t_k)^)+ 0.5*|y(s_k)|/(1+0.5*)).
And for t>=T1 we should just solve the given ODE and there is event condition for t>=T1.
And we should plot t vs y(t) for 4 seconds.
Can u please help me out?
Thanks
Arnab
Arnab el 1 de Dic. de 2024
Yes, thanks.
Torsten
Torsten el 1 de Dic. de 2024
As you are a beginner in MATLAB, use MATLAB Onramp to learn the basics of the new language:
Concerning your question, it will help to study the "ballode" example here:
Arnab
Arnab el 1 de Dic. de 2024
Ok, thank u for the help.

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 30 de Nov. de 2024

Comentada:

el 1 de Dic. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by