Plotting Solution Curve on Direction Field
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jordan Stanley
am 13 Apr. 2022
Kommentiert: Jordan Stanley
am 15 Apr. 2022
Hello,
I have the second-order differential equation with initial conditions: y'' + 2y' + y = 0, y(-1) = 0, y'(0) = 0
I need to plot the direction field of the solution to the equation and trace the solution curve corresponding to the initial conditions.
I have created the direction field but I'm not sure how to plot the solution curve over the direction field.
Using the plot() function is giving errors and the ezplot() function doesn't seem to represent what the direction field is showing.
Here is what I have so far
% Finds solution to the DE
syms y(x)
Dy = diff(y);
D2y = diff(y,2);
ode = D2y + 2*Dy + y == 0;
ySol1 = dsolve(ode, y(-1)==0); % Solution to DE applying first initial conditions.
ySol2 = dsolve(ode,Dy(0)==0); % Solution to DE applying second initial conditions.
% Sets up directional field
[x,y]=meshgrid(-4:0.5:4,-4:0.5:4);
u = y; % x1' = y'
v = - 2*y - x; % x2' = y'' = - 2*y' - y
u1 = u./sqrt(u.^2+v.^2);
v1 = v./sqrt(u.^2+v.^2);
quiver(x, y, u1, v1, 0.6)
xlabel('x-axis')
ylabel('y-axis')
axis on
axis([-3.5 3.5 -3.5 3.5]);
% Prints the solution curve corresponding to the initial conditions.
hold on
plot(ySol1)
plot(ySol2)
hold off
Any help is greatly appreciated.
0 Kommentare
Akzeptierte Antwort
Torsten
am 13 Apr. 2022
ySol = dsolve(ode, [y(-1)==0,Dy(0)==0]);
dySol = diff(ySol,x);
instead of
ySol1 = dsolve(ode, y(-1)==0); % Solution to DE applying first initial conditions.
ySol2 = dsolve(ode,Dy(0)==0); % Solution to DE applying second initial conditions.
and
plot(double(subs(ySol,x,-4:0.5:4)),double(subs(dySol,x,-4:0.5:4)))
instead of
plot(ySol1)
plot(ySol2)
16 Kommentare
Torsten
am 14 Apr. 2022
Then the following code should work (without prescribing ySol as I did before, but the result should be the same):
syms y(x)
Dy = diff(y,x);
D2y = diff(y,x,2);
ode = D2y + 2*Dy + y == 0;
ySol(x) = dsolve(ode,[y(-1)==0,Dy(0)==0])
ySol = subs(ySol,C1,1);
dySol = diff(ySol,x);
ySol = matlabFunction(ySol);
dySol = matlabFunction(dySol);
% Sets up directional field
[x,y]=meshgrid(-4:0.5:4,-4:0.5:4);
u = y; % x1' = y'
v = - 2*y - x; % x2' = y'' = - 2*y' - y
u1 = u./sqrt(u.^2+v.^2);
v1 = v./sqrt(u.^2+v.^2);
quiver(x, y, u1, v1, 0.6)
xlabel('x-axis')
ylabel('y-axis')
axis on
axis([-3.5 3.5 -3.5 3.5]);
% Prints the solution curve corresponding to the initial conditions.
hold on
plot(ySol(-4:0.5:4),dySol(-4:0.5:4));
hold off
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Equation Solving finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!