Solving 2nd Order Differential Equation Symbolically

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

The 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

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.
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.
.
I see. Thank you for the response.
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
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
@Walter RobersonThank you!
(I was away doing other things for a few minutes.)
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 ?
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.
I believe that I just need to graph the solution of the equation found symbolically but not necessilary on the directional field.
The title of your last query was "Plotting Solution Curve on Direction Field" ...
Correct.
One of the tasks that I am suppsed to complete is to...
  1. 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 ?
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.

Sign in to comment.

More Answers (1)

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

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!