1
$\begingroup$

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.

$\endgroup$
7
  • 1
    $\begingroup$ Post Mathematica code? $\endgroup$ Commented Sep 4, 2022 at 11:10
  • $\begingroup$ I am sorry, I am trying to put the code, but it appears like plain text. How to include codes here ? $\endgroup$ Commented Sep 4, 2022 at 11:48
  • $\begingroup$ 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. $\endgroup$
    – cvgmt
    Commented Sep 4, 2022 at 12:21
  • $\begingroup$ 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 $\endgroup$
    – Michael E2
    Commented Sep 4, 2022 at 12:35
  • $\begingroup$ 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? $\endgroup$ Commented Sep 4, 2022 at 12:35

1 Answer 1

5
$\begingroup$

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]

Mathematica graphics

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}]

Mathematica graphics

$\endgroup$
3
  • $\begingroup$ Thanks a lot for your help. How can I plot X(t) vs t and y(t) vs t in 2 separate graphs? $\endgroup$ Commented Sep 4, 2022 at 12:33
  • $\begingroup$ @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. $\endgroup$
    – Nasser
    Commented Sep 4, 2022 at 12:46
  • $\begingroup$ Thanks a lot for your help. and I will change the time span to [0 1] $\endgroup$ Commented Sep 4, 2022 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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