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

18 vues (au cours des 30 derniers jours)
Carla Roos
Carla Roos le 30 Nov 2022
Commenté : Carla Roos le 30 Nov 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

Réponse acceptée

Sam Chak
Sam Chak le 30 Nov 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 commentaire
Carla Roos
Carla Roos le 30 Nov 2022
Thank you very much!
Hope you have a great day! :)

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by