Solving 2nd Order Differential Equation Symbolically
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Share a link to this question
Hello,
I have the 2nd order differential equation: y'' + 2y' + y = 0 with the initial conditions y(-1) = 0, y'(0) = 0.
I need to solve this equation symbolically and graph the solution.
Here is what I have so far...
syms y(x)
Dy = diff(y);
D2y = diff(y,2);
ode = D2y + 2*Dy + y == 0;
ySol = dsolve(ode,[y(-1)==0,Dy(0)==0])
a = linspace(0,1,20);
b = eval(vectorize(ySol));
plot(a,b)
But I get the following output.
ySol = 

Error using eval
Unrecognized function or variable 'C1'.
I'd greatly appreciate any assistance.
Accepted Answer
Star Strider
on 15 Apr 2022
The
constants are the initial condition. They must be defined.
constants are the initial condition. They must be defined. syms y(x) y0
Dy = diff(y);
D2y = diff(y,2);
ode = D2y + 2*Dy + y == 0;
ySol(x,y0) = dsolve(ode,[Dy(0)==0,y(-1)==0,y(0)==y0])
ySol(x, y0) = 

% a = linspace(0,1,20);
% b = eval(vectorize(ySol));
figure
fsurf(ySol,[0 1 -1 1])
xlabel('x')
ylabel('y_0 (Initial Condition)')

.
16 Comments
Jordan Stanley
on 15 Apr 2022
Thank you for the reply.
I believe this is what I needed.
Quick question, do you know if the solution could be graphed in simply 2-D.
Star Strider
on 15 Apr 2022
It can, however that requires either a range of initial conditions, or to choose one.
ySol = @(x, y0) exp(-x) .* (y0 + x.*y0);
xv = linspace(0, 1, 20);
y0v = linspace(-5, 5, 11)
y0v = 1×11
-5 -4 -3 -2 -1 0 1 2 3 4 5
[X,Y0] = ndgrid(xv,y0v);
figure
plot(xv, ySol(X,Y0))
grid
xlabel('x')
ylabel('y_0')

The solutions all converge to zero at about x=10.
.
Jordan Stanley
on 15 Apr 2022
I see. Thank you for the response.
Star Strider
on 15 Apr 2022
As always, my pleasure!
Jordan Stanley
on 15 Apr 2022
Hello again,
Upon running your initially suggested code
syms y(x) y0
Dy = diff(y);
D2y = diff(y,2);
ode = D2y + 2*Dy + y == 0;
ySol(x,y0) = dsolve(ode,[Dy(0)==0,y(-1)==0,y(0)==y0])
% a = linspace(0,1,20);
% b = eval(vectorize(ySol));
figure
fsurf(ySol,[0 1 -1 1])
xlabel('x')
ylabel('y_0 (Initial Condition)')
I get the following error message.
Error using sym/subsindex
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression.
I have the following code in the section above. I'm not sure if this causes the issue.
% Finds solution to the DE
syms y(x)
Dy = diff(y);
D2y = diff(y,2);
ode = D2y + 2*Dy + y == 0;
ySol = dsolve(ode,[y(-1)==0,Dy(0)==0])
dySol = diff(ySol,x);
ySol = @(x) exp(-x).*(1+x);
dySol = @(x) -exp(-x).*x;
% 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([-4 4 -4 4]);
title('Direction Field')
% Prints the solution curve corresponding to the initial conditions.
hold on
plot(ySol(-4:0.5:4),dySol(-4:0.5:4),"LineWidth",2);
hold off
Thanks
Walter Roberson
on 15 Apr 2022
Works for me online here.
% Finds solution to the DE
syms y(x)
Dy = diff(y);
D2y = diff(y,2);
ode = D2y + 2*Dy + y == 0;
ySol = dsolve(ode,[y(-1)==0,Dy(0)==0])
ySol = 

dySol = diff(ySol,x);
ySol = @(x) exp(-x).*(1+x);
dySol = @(x) -exp(-x).*x;
% 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([-4 4 -4 4]);
title('Direction Field')
% Prints the solution curve corresponding to the initial conditions.
hold on
plot(ySol(-4:0.5:4),dySol(-4:0.5:4),"LineWidth",2);
hold off

Star Strider
on 15 Apr 2022
(I was away doing other things for a few minutes.)
Jordan Stanley
on 15 Apr 2022
It worked along with the code you suggested as well?
syms y(x) y0
Dy = diff(y);
D2y = diff(y,2);
ode = D2y + 2*Dy + y == 0;
ySol(x,y0) = dsolve(ode,[Dy(0)==0,y(-1)==0,y(0)==y0])
% a = linspace(0,1,20);
% b = eval(vectorize(ySol));
figure
fsurf(ySol,[0 1 -1 1])
xlabel('x')
ylabel('y_0 (Initial Condition)')
Do you want to see more solution curves in the plot of the direction field apart from the red one ?
The red curve is the one with initial condition y(0) = 1.
Or why do you want to use the code from above ?
Jordan Stanley
on 15 Apr 2022
The code for that was suggested for me to use for the question I asked above gave me an error when I ran it and I was just asking if maybe the error has somthing to do with the direction field and solution curve code that I had in the section above it.
I don't believe I need anymore solution curves.
You are told to solve the equation
y'' + 2y' + y = 0
with the initial conditions
y(-1) = 0, y'(0) = 0
symbolically and graph the solution.
We found out that there is not only one such solution, but that there are infinitly many, namely
ySol = exp(-x)*(C1 + C1*x)
for an arbitrary value of C1.
Now we took one of these solutions (red curve in plot), namely exp(-x)*(1+x) (C1=1) and integrated it in the direction field.
If you want to graph more solution curves therein, you can choose another value for C1 and include the graph similarly in the plot.
Jordan Stanley
on 16 Apr 2022
I believe that I just need to graph the solution of the equation found symbolically but not necessilary on the directional field.
Torsten
on 16 Apr 2022
The title of your last query was "Plotting Solution Curve on Direction Field" ...
Jordan Stanley
on 16 Apr 2022
Correct.
One of the tasks that I am suppsed to complete is to...
- Plot the direction field (phase portrait) of solutions to the equation (without initial conditions) in MATLAB. Print the graph to locate and trace the solution curve corresponding to the stated initial conditions.
Which I believe I have already done. The second task is to...
2. Use MATLAB to solve the equation symbolically and graph the solution.
This is what I'm currently seeking assistance for.
How should it be possible to solve 1. without 2. ? If you don't know the solution, you can't trace a solution curve. Or what's your opinion ?
Anyhow - I think your instructors overlooked that the equation together with its initial conditions does not only give one curve, but infinitly many. So "graphing the solution" will become difficult. But Star Strider's answer for this situation looks fine for me.
But you say you get an error. What's your code and what's the error message ?
Jordan Stanley
on 16 Apr 2022
Edited: Jordan Stanley
on 16 Apr 2022
Well I should mention that we were supposed to choose a 2nd order differential eqaution initial value problem from our textbook and maybe I just happened to choose a more complicated eqaution to use.
Ok, well interestingly enough upon running the code agin that Star Strider suggested I did not get an error message this time.
More Answers (1)
jatin
on 19 Sep 2024
clear all;
clc;
close all;
num = [0 10];
den= [0 0];
[t, y] = ode45(@ode_system,num,den)]
plot(t, y(:,1));
xlabel('Time t');
ylabel('Solution y(t)');
title('Solution of the second-order differential equation');
grid on;
end
1 Comment
Walter Roberson
on 19 Sep 2024
The user specifically asked for symbolic solution.
Categories
Find more on MATLAB in Help Center and File Exchange
Products
See Also
on 15 Apr 2022
on 19 Sep 2024
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)