Plot several solution curves to y'(t) = y(t) - t

26 visualizaciones (últimos 30 días)
Carla Roos
Carla Roos el 30 de Nov. de 2022
Comentada: Carla Roos el 30 de Nov. de 2022
Hi!
I want to plot several solution curves to an ODE using ode45. My main problem is that I do not understand how to write y(t) - t.
The whole equation is as following:
y'(t) = y(t) - y, 0 <= t <= 2, with the initial conditions y(0) E [-2, 2].
I have tried solving it by using syms y(t) without any success...
Best regards

Respuesta aceptada

Sam Chak
Sam Chak el 30 de Nov. de 2022
You can create an anonymous function of t and y.
fun = @(t, y) y - t; % <--- Type like this
% solve for y(0) = 2
[t, y] = ode45(fun, [0 2], 2);
plot(t, y), hold on
% solve for y(0) = -2
[t, y] = ode45(fun, [0 2], -2);
plot(t, y), hold off,
grid on, xlabel('t'), ylabel('y(t)'),
legend('y(0) = 2', 'y(0) = –2')
  1 comentario
Carla Roos
Carla Roos el 30 de Nov. de 2022
Thank you very much!
Hope you have a great day! :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by