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:

 Risposta accettata

John D'Errico
John D'Errico 7 minuti fa
Modificato: John D'Errico 6 minuti fa
Not very clear exactly what you are trying to do. 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.
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]')

Più risposte (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)
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 4 minuti fa
Spostato: Torsten 4 minuti fa
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

Categorie

Scopri di più su 2-D and 3-D Plots in Centro assistenza e File Exchange

Prodotti

Release

R2026a

Tag

Richiesto:

il 11 Set 2026 alle 23:21

Spostato:

il 11 Set 2026 alle 23:39

Community Treasure Hunt

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

Start Hunting!

Translated by