Initial conditions on ODE45 ?

Im trying to solve this IVP: e^y +(t*e^y - sin(y))*(dy/dt)=0 with the initial condition y(2)=1.5.
I was just not sure how to do it with the initial condition with Y(2)=1.5, iknow how to do it if it were y(0)=1.5:
f= @(t,y) (exp(y)+(t.*exp(y)-sin(y))); % This is the function.
[t,y]=ode45(f, [0.5,4], 1.5); % trange is from 0.5 to 4
plot(t,y)
can someone please help me out?

 Respuesta aceptada

Jan
Jan el 30 de Jul. de 2011

5 votos

This uses the initial value y(0.5)=1.5 ( not y(0)=1.5):
[t, y] = ode45(f, [0.5, 4], 1.5);
So for y(2)=1.5:
[t, y] = ode45(f, [2, 4], 1.5);
Note: The initial value problem starts at the inital point.
[EDITED]: The call to ODE45 is equivalent, if the problem is formulated in backward direction - an "final value problem": tspan is still [ti, tf], but now ti > tf.

9 comentarios

Robin
Robin el 30 de Jul. de 2011
ok thank you for that, but the problem also asks me to plot from the t interval 0.5 to 4....this means anything before t=2 does not show on the graph because I guess there are no values for x and y before that initial condition? how can I do this still using ODE45 and still wtih the initial condition y(2)=1.5?
Robin
Robin el 30 de Jul. de 2011
furthermore....I need to approximate what the value for y is at x=1, 1.5 and 3....
Robin
Robin el 30 de Jul. de 2011
...from what I have read I understand that ODE is a time step numerical solver....I really dont see how it could go backward in time?
Jan
Jan el 30 de Jul. de 2011
You have y(2)=1.5 and you want to get y(0.5). And you want to go backward in time. What about:
[t, y] = ode45(f, [2, 0.5], 1.5)
Take a look in the Runge-Kutta-Algorithm. You will find only linear dependencies to t. So you can run the integrator in reverse direction in theory. MATLAB's ODE45 is smart enough to allow this in practize also.
Another idea is to transform t -> -t in the ODE function and modify the integration limits accordingly.
If you want y to specific times, look at the tspan argument of ODE45. BTW. reading "help ODE45" is a good idea in every case.
Robin
Robin el 31 de Jul. de 2011
I am not exactly sure if you were getting at the same thing, when you say "run the integrator in reverse direction". But Basically this is what I was able to think of.
I have y'(t)=f(t,y(t)) with initial value y(2)=1.5.
I can define z(t)=y(-t).
Then I notice that z(-2)=y(2)=1.5 and z'(t) = -y'(-t)= -f(-t,y(-t))=-f(-t,z(t)).
That is z solves z'(t)=-f(-t,z(t)). Now, how do I use ode45 to find the solution to z'(t)=-f(-t,z(t)) with initial condition z(-2)=1.5 on the interval t=[-2,-1], so hen y(t) on [1,2] can be recovered via y(t)=z(-t)?
Jan
Jan el 31 de Jul. de 2011
@Robin: Did you read my comment above?! Did you read the help text of ODE45? There you find: "tspan: A vector specifying the interval of integration [t0, tf]. To obtain solutions at specific times (all increasing or decreasing), use tspan = [t0, t1, ..., tf]".
So simply use [2.0, 0.5] for tspan to get the y trajectory from t=2 to t=0.5. Then you can check if your t -> -t transformation lead to the same result (which is correct at first glance).
Robin
Robin el 31 de Jul. de 2011
I've figured it out. Thank you for your help :)
Walter Roberson
Walter Roberson el 26 de Jul. de 2017
Liu Langtian comments to Jan Simon
right
Example Code
Use ode45() to find the approximate values of the solution at t in the range of 1 to 3
function ydot = eqns(t,y)
ydot=(t-exp(-t))/(y+exp(y));
end
###################################
%%Code
[t1,y1]=ode45(@eqns,[1.5 1], 0.5);
hold on;
[t2,y2]=ode45(@eqns,[1.5 3], 0.5);
hold off
t=[t1;t2];
y=[y1;y2];
plot(t,y,'-o')

Iniciar sesión para comentar.

Más respuestas (1)

Subha Fernando
Subha Fernando el 26 de Oct. de 2011

0 votos

let say function is dy/dt = y (t-y).
If initial condition is given at y(1) = 0.5 not at y(0) then we define the RHS as
function output = funcRHS(t, y) output = y *(t-y); end
%then u can call
hold on ode45('funcRHS', [1, -1], 0.5) ode45('funcRHS', [1,5], 0.5)
%Here you can see and read the initial value at y(0) also

Preguntada:

el 30 de Jul. de 2011

Comentada:

el 22 de Jul. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by