# Plotting the solution of nonlinear 2-dimentional system of ODEs

I am new to Mathematica, and I have a 2-D system of ODEs:
dx/dt=y
dy/dt= -x+x^3-2my

Mathematica code:

s = NDSolve[{x'[t] == y[t], y'[t] == -x[t] + x[t]^3 - 0.2 *y[t] ,
x[0] == y[0] == 0}, {x, y}, {t, -1, 1}]

Plot[Evaluate[x[t] /. s], {t, -1, 1}, PlotRange -> All]
Plot[Evaluate[y[t] /. s], {t, -1, 1}, PlotRange -> All]


I want to plot the solution of the system X(t),y(t).
When I use this code nothing appears.
I'll appreciate your help, Warm Regards.

• Post Mathematica code? Sep 4, 2022 at 11:10
• I am sorry, I am trying to put the code, but it appears like plain text. How to include codes here ? Sep 4, 2022 at 11:48
• When we set {x[0],y[0]}={0,0}, the result is a segment on x-axis. Maybe change to {x[0],y[0]}={1,1} for example. Sep 4, 2022 at 12:21
• I guess you figured it out, but anyway: You can format inline code and code blocks by selecting the code and clicking the {} button above the edit window. The edit window help button ? is useful for learning how to format your questions and answers. You may also find the meta Q&A, How to copy code from Mathematica so it looks good on this site, helpful Sep 4, 2022 at 12:35
• It works. Thanks a lot. I have another question how can I plot x(t) vs t and y(t) vs t in 2 separate graphs? Sep 4, 2022 at 12:35

You do not need to solve the odes's to plot the solutions.

Let your first state variable be $$x_1=x$$ and the second be $$x_2=y$$. Then your odes are

\begin{align*} x_1' &= x_2 \\ x_2' &= -x_1 +x_1^3 - 0.2 x_2 \end{align*}

Therefore the command in Mathematica to plot the solution is

StreamPlot[{x2, -x1 + x1^3 - 0.2*x2}, {x1, -2, 2}, {x2, -2, 2},
Frame -> False, Axes -> True, AspectRatio -> 1/GoldenRatio,
AxesLabel -> {"x(t)", "y(t)"},
StreamPoints -> {{{{1, 1}, Red}, Automatic}},
StreamColorFunction -> None]


The red line shows the solution for initial conditions $$(1,1)$$. Change as needed.

How can I plot X(t) vs t and y(t) vs t in 2 separate graphs?

You can do

s = NDSolveValue[{x'[t] == y[t], y'[t] == -x[t] + x[t]^3 - 0.2*y[t],
x[0] == 0, y[0] == 1}, {x, y}, {t, 0, 3}]

opts = {GridLines -> Automatic, GridLinesStyle -> LightGray, ImageSize -> 300};
p1 = Plot[s[[1]][t], {t, 0, 3}, AxesLabel -> {"time (sec)", "x(t)"}, Evaluate@opts];
p2 = Plot[s[[2]][t], {t, 0, 3}, AxesLabel -> {"time (sec)", "y(t)"}, Evaluate@opts];
Grid[{{p1, p2}}, Frame -> All, Spacings -> {1, 1}]


• Thanks a lot for your help. How can I plot X(t) vs t and y(t) vs t in 2 separate graphs? Sep 4, 2022 at 12:33
• @Doaamahmoud updated. Are you sure you want time to run from -1 to 1? Time normally starts at t=0. But you can change the code above as you want. Sep 4, 2022 at 12:46
• Thanks a lot for your help. and I will change the time span to [0 1] Sep 4, 2022 at 12:51