How do you plot the direction (vector) field of a second-order homogeneous ode using Matlab?

I've already used MATLAB to check the solution to the ode and I've tried to use tutorials online to plot the direction (vector) field, but haven't had any luck. Here's what I have done in MATLAB:

eqn1 = 'D2x + 5*Dx + 4*x = 0';
x = dsolve(eqn1, 't')

The above gives me the correct solution to the second-order ode, but isn't helpful for plotting the direction (vector) field. I'm new to MATLAB, so any help would be greatly appreciated.

  • $\begingroup$ I just corrected the spelling and formatting of your question. Still that question is pretty bad. No one will be able to help you, because you don't state and explain your problem. We don't know what you wanted to do, what you expected, what you did, and what the result was. Furthermore, this doesn't seem to be a math problem at all. $\endgroup$ – P. Siehr Oct 1 '17 at 22:59
  • $\begingroup$ If initial conditions are the problem, the Docu of the command will explain how to impose them. $\endgroup$ – P. Siehr Oct 1 '17 at 23:00
  • $\begingroup$ There, I've tried to be more straightforward about what I'm attempting to do. It's more of a Matlab question and how to go about doing something within Matlab than it is a straight-up math problem. $\endgroup$ – MatthewSpire Oct 1 '17 at 23:11
  • $\begingroup$ You cannot plot anything if you did not specify boundary conditions. Please indicate what you put in and how, what that resulted, so that arbitrary constants disappear before plotting. $\endgroup$ – Narasimham Oct 2 '17 at 12:18

The second order ODE $X'' +5X'+4X =0$ rewrites as a first-order ODE system. Indeed, setting $(x,y)=(X,X')$, one has \begin{aligned} \frac{\text{d}x}{\text{d}t} &= y \, ,\\ \frac{\text{d}y}{\text{d}t} &= -5y-4x \, . \end{aligned} The phase-space plot can then be obtained in a similar manner as described in this post, e.g. using Matlab's quiver function.

| cite | improve this answer | |

This is what I did, using help from above, in order to accomplish the task:

% set the domain and subintervals in each direction
xdom = linspace(-2,2,25);
ydom = linspace(-2,2,25);

% generate mesh of domain
[X,Y] = meshgrid(xdom,ydom);

% dx/dt
U = Y;

% dy/dt
V = (-5*Y - 4*X);


The second-order ode was rewritten as a system of first-order odes and then those were plotted.

| cite | improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.