# Can I plot the solution to an ODE without using dsolve?

I have the following ODE

$\qquad x'= -x^3+\sin(t)$

I want to find numerically the initial condition $x(0)$ corresponding to the $2\pi$ periodic solution.

I tried to use DSolve, but it did not work. It gives me

Solve::ifun: Inverse functions are being used by Solve, so some solutions may not be found; use Reduce for complete solution information".

I actually tried different ODEs and was able to solve them. Is there any way for me to plot the solution without solving the ODE first?

• "plot the solution without solving the ODE first"? That doesn't make sense. Do you really mean that you would accept a numerical solution? And what do you mean by "And I want to find numerically initial condition $x(0)$ correspond to $2\pi$ periodic solution."? Do you mean that you are trying to find a solution which has period $2\pi$? please provide a little more information. – march Mar 31 '16 at 22:35
• You can plot a derivative field. For every point (t,x) you draw a little segment slanted according to the value of the derivative x'(t). That's your right hand side of the equation. You are basically plotting the solution without solving the equation (well, when you plot those little segments, you'll understand...) Then from that derivative field you can gain insights on how your solutions would behave. Ty to see if you can make out a solution that has the same value at t=0 and t=2pi. – Peltio Apr 1 '16 at 2:55
• Try using Fourier series to represent the formal solution. – Silvia Apr 1 '16 at 6:30

To answer the question in the title, yes it is definitely possible to plot the solution without solving the differential equation, by using StreamPlot.

Borrowing from MichaelE2's solution here, you can use Manipulate to find the solution graphically for a given initial condition

Manipulate[
Show[
StreamPlot[{1, -x^3 + Sin[t]}, {t, -4 π, 4 π}, {x, -2, 2},
StreamPoints -> Fine],
StreamPlot[{1, -x^3 + Sin[t]}, {t, -4 π, 4 π}, {x, -2, 2},
StreamPoints -> {p0}, StreamStyle -> Directive[Thick, Red]]]
, {{p0, {0, 0}}, {-4 π, -2}, {4 π, 2}, Locator}]


If you are satisfied with a numerical approach, this is one of the problems where ParametricNDSolve[] is called for:

pf = ParametricNDSolveValue[{x'[t] == Sin[t] - x[t]^3, x[0] == a}, x, {t, 0, 2 π}, a,
Method -> "StiffnessSwitching", WorkingPrecision -> 25];

(* bracket obtained from a preliminary plot of pf[a][2 π] *)
aopt = a /. FindRoot[pf[a][2 π] - a, {a, -1, 1}, WorkingPrecision -> 25]
-0.7156846197712976536871740

xs = NDSolveValue[{x'[t] == Sin[t] - x[t]^3, x[0] == aopt}, x, {t, -2 π, 4 π}];

Plot[xs[t], {t, -2 π, 4 π}]


xs[π Range[-2, 4]]
{-0.7156821841853735, 0.7156848515290952, -0.7156846197712976,
0.7156846139367274, -0.7156846712483197, 0.7156846219921508,
-0.7156846049890743}


As a first step I you can find numerical solutions.

Let us first repeat: the ODE is

eq = x'[t] == -x[t]^3 + Sin[t];


Mathematica does not find an analytic solution

DSolve[eq, x[t], t]


During evaluation of In[13]:= Solve::ifun: Inverse functions are being used by Solve, so some solutions may not be found; use Reduce for complete solution information. >>

(* Out[13]= DSolve[Derivative[1][x][t] == Sin[t] - x[t]^3, x[t], t] *)


So now the numerical solution with which Mathematica has no difficulty:

Let's take 5 periods

tmax = 5 (2 \[Pi]);


The inital vaule can be chosen freely.

y0 = 0; (* other examples to try: 1,-1,5,-5 *)


The solution

sol = NDSolve[eq && x[0] == y0, x[t], {t, 0, tmax}];

xx[t_] = x[t] /. sol[[1]];


Plot of the soluton, together with a pure sine for comparison

Plot[{Sin[2 \[Pi] t], xx[2 \[Pi] t]}, {t, 0, tmax/(2 \[Pi])},
PlotLabel ->
"Numerical solution of an ODE\nblue curve -> solution\nyellow curve -> pure \
sine"]


EDIT

It seems that the solution has the general form

f[t] = d[t] + p[t]


where d[t] is a function decaying with time and p[t] is a "universal" function with period 2 Pi. The initial conditions show up only in d[t].