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:

 Akzeptierte Antwort

John D'Errico
John D'Errico vor 11 Minuten
Bearbeitet: John D'Errico vor 10 Minuten

0 Stimmen

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]')

Weitere Antworten (2)

Star Strider
Star Strider vor 10 Minuten

1 Stimme

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 vor 8 Minuten
Verschoben: Torsten vor 8 Minuten

0 Stimmen

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

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2026a

Tags

Gefragt:

vor etwa 21 Stunden

Verschoben:

vor etwa 21 Stunden

Community Treasure Hunt

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

Start Hunting!

Translated by