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

17 次查看(过去 30 天)
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

采纳的回答

Sam Chak
Sam Chak 2022-11-30
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')

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by