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

34 views (last 30 days)
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

Accepted Answer

Sam Chak
Sam Chak on 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')

More Answers (0)

Community Treasure Hunt

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

Start Hunting!