ode(t) =
Error while outputting graph
显示 更早的评论
Hello! Im currently running into a problem with getting my code to output my graph after updating to the most recent version of Matlabs and im unsure as to how to fix this as copilot seemingly is also not very helpful and im unsure how calling for subs works. Any help + explanation is greatly appreciated, thank you in advance!
Current Code:
syms y(t)
dy = diff(y)
dy2 = diff(dy)
ode = dy2 - dy == 2*t^2 - t - 5
ysol(t) = dsolve(ode)
c1 = 0;
c2 = 0;
ysol1(t) = subs(ysol(t) + c1*exp(t) + c2*t*exp(t))
c1 = -1;
c2 = -1;
ysol2(t) = subs(ysol + c1*exp(t) + c2*t*exp(t))
c1 = 3;
c2 = 3;
ysol3(t) = subs(ysol + c1*exp(t) + c2*t*exp(t))
figure
hold on
fplot(@(t) (ysol1(t)), [-2 2], '-', 'LineWidth', 1)
fplot(@(t) (ysol2(t)), [-2 2], ':', 'LineWidth', 1)
fplot(@(t) (ysol3(t)), [-2 2], '--', 'LineWidth', 1)
title('Problem One')
xlabel('t')
ylabel('Solutions')
grid on
ylim([-15 25])
legend('c1 = c2 =1 0','c1 = c2 = -1', 'c1 = c2 = 3')
Error:

采纳的回答
更多回答(2 个)
You were not calling subs correctly. I added the necessary additional arguments, and it now seems to work.
Try this ---
syms y(t) C1 C2
dy = diff(y)
dy2 = diff(dy)
ode = dy2 - dy == 2*t^2 - t - 5
ysol(t) = dsolve(ode)
c1 = 0;
c2 = 0;
ysol1(t) = subs(ysol(t) + c1*exp(t) + c2*t*exp(t), {C1,C2}, {0, 0})
c1 = -1;
c2 = -1;
ysol2(t) = subs(ysol + c1*exp(t) + c2*t*exp(t), {C1,C2}, {-1,-1})
c1 = 3;
c2 = 3;
ysol3(t) = subs(ysol + c1*exp(t) + c2*t*exp(t), {C1,C2}, {3,3})
figure
hold on
fplot(@(t) (ysol1(t)), [-2 2], '-', 'LineWidth', 1)
fplot(@(t) (ysol2(t)), [-2 2], ':', 'LineWidth', 1)
fplot(@(t) (ysol3(t)), [-2 2], '--', 'LineWidth', 1)
title('Problem One')
xlabel('t')
ylabel('Solutions')
grid on
ylim([-15 25])
legend('c1 = c2 =1 0','c1 = c2 = -1', 'c1 = c2 = 3')
.
E.g.
syms y(t)
dy = diff(y);
dy2 = diff(dy);
ode = dy2 - dy == 2*t^2 - t - 5;
ysol(t) = dsolve(ode)
s = symvar(ysol) % Now you know that C1 = s(2) and C2 = s(3)
ysol1(t) = subs(ysol,[s(2),s(3)],[0 0]) % And here you substitute C1 = 0 and C2 = 0
fplot(@(t) (ysol1(t)), [-2 2], '-', 'LineWidth', 1) % And here you plot the corresponding graph
类别
在 帮助中心 和 File Exchange 中查找有关 2-D and 3-D Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!








