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:

 Réponse acceptée

John D'Errico
John D'Errico le 11 Sep 2026 à 23:36
Modifié(e) : John D'Errico il y a environ 6 heures
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]')

Plus de réponses (2)

Star Strider
Star Strider le 11 Sep 2026 à 23:37
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 le 11 Sep 2026 à 23:39
Déplacé(e) : Torsten le 11 Sep 2026 à 23:39
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

Catégories

En savoir plus sur 2-D and 3-D Plots dans Centre d'aide et File Exchange

Produits

Version

R2026a

Tags

Question posée :

le 11 Sep 2026 à 23:21

Modifié(e) :

le 13 Sep 2026 à 13:04

Community Treasure Hunt

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

Start Hunting!

Translated by