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:

 채택된 답변

John D'Errico
John D'Errico 2026년 9월 11일 23:36
편집: John D'Errico 대략 16시간 전

0 개 추천

I'm pretty sure you want to substitute in different values of the undetermined constants, then plot each corresponding curve, but I'm often wrong.
syms y(t)
dy = diff(y);
dy2 = diff(dy);
ode = dy2 - dy == 2*t^2 - t - 5
ode(t) = 
ysol(t) = dsolve(ode)
ysol(t) = 
Now we can use subs.
ysol1 = subs(ysol,{'C1','C2'},[0,0])
ysol1(t) = 
Does that make sense? I told it to replace C1 and C2, with 0 and 0 respectively. Now do the same for the other choices of C1 and C2. Each time I'll create a new version of ysol. You can see MATLAB even knows ysol1 is a function of t, so I do not need to tell it that. This is because ysol itself was a function of t, and all I did was replace the undetermined constants.
ysol2 = subs(ysol,{'C1','C2'},[-1,-1])
ysol2(t) = 
ysol3 = subs(ysol,{'C1','C2'},[3,3])
ysol3(t) = 
fplot(ysol1,'r')
hold on
fplot(ysol2,'g')
fplot(ysol3,'b')
legend('[0,0]','[-1,-1]','[3,3]')

추가 답변 (2개)

Star Strider
Star Strider 2026년 9월 11일 23:37

1 개 추천

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)
dy(t) = 
dy2 = diff(dy)
dy2(t) = 
ode = dy2 - dy == 2*t^2 - t - 5
ode(t) = 
ysol(t) = dsolve(ode)
ysol(t) = 
c1 = 0;
c2 = 0;
ysol1(t) = subs(ysol(t) + c1*exp(t) + c2*t*exp(t), {C1,C2}, {0, 0})
ysol1(t) = 
c1 = -1;
c2 = -1;
ysol2(t) = subs(ysol + c1*exp(t) + c2*t*exp(t), {C1,C2}, {-1,-1})
ysol2(t) = 
c1 = 3;
c2 = 3;
ysol3(t) = subs(ysol + c1*exp(t) + c2*t*exp(t), {C1,C2}, {3,3})
ysol3(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')
.
Torsten
Torsten 2026년 9월 11일 23:39
이동: Torsten 2026년 9월 11일 23:39

0 개 추천

E.g.
syms y(t)
dy = diff(y);
dy2 = diff(dy);
ode = dy2 - dy == 2*t^2 - t - 5;
ysol(t) = dsolve(ode)
ysol(t) = 
s = symvar(ysol) % Now you know that C1 = s(2) and C2 = s(3)
s = 
ysol1(t) = subs(ysol,[s(2),s(3)],[0 0]) % And here you substitute C1 = 0 and C2 = 0
ysol1(t) = 
fplot(@(t) (ysol1(t)), [-2 2], '-', 'LineWidth', 1) % And here you plot the corresponding graph

카테고리

도움말 센터File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품

릴리스

R2026a

태그

질문:

2026년 9월 11일 23:21

편집:

2026년 9월 13일 13:04

Community Treasure Hunt

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

Start Hunting!

Translated by