1
$\begingroup$

I want this program to plot to be combined into one plot - just can't seem to make it appear in one plot. Any suggestions?

n = -1; While[n < 3, 
sol = NDSolve[{D[xtraj[t], 
  t] == -(Sinh[
     2 xtraj[t] (-t + 2)]/ 
      (Cosh[2 xtraj[t] (-t + 2)])), xtraj[0] == n}, 
xtraj[t], {t, -4, 8}]; n = n + 0.25; 
p = ParametricPlot[{t, xtraj[t]} /. sol, {t, -4, 8}, 
PlotRange -> All, PlotStyle -> {Blue, Full, Thick}, 
AxesStyle -> Thickness[.001], LabelStyle -> {Black, Medium}]; 
Print[p]]
$\endgroup$
2
  • $\begingroup$ Hello! Usually procedural programming is not the go-to tool when using Mathematica. More often than not problems can be solved using the functional paradigm of programming. Read the docs on ParametricNDSolve, ParametricNDSolveValue, Table and Evaluate $\endgroup$
    – Sektor
    May 24, 2018 at 2:56
  • $\begingroup$ Welp, since Bill already showed you one solution -- here's another onepfun = ParametricNDSolveValue[{D[f[t], t] == -(Sinh[2 f[t] (-t + 2)]/(Cosh[2 f[t] (-t + 2)])), f[0] == n}, f, {t, -4, 8}, {n}]; Plot[Evaluate[Table[pfun[n][t], {n, -1, 3, .25}]], {t, 0, 5}, PlotRange -> All] $\endgroup$
    – Sektor
    May 24, 2018 at 2:59

1 Answer 1

2
$\begingroup$

Try this

Show[Table[
  sol = NDSolve[{D[xtraj[t],t] == -(Sinh[2 xtraj[t] (-t + 2)]/
    (Cosh[2 xtraj[t] (-t + 2)])), xtraj[0] == n},xtraj[t], {t, -4, 8}];
  ParametricPlot[{t, xtraj[t]} /. sol, {t, -4, 8}, 
    PlotRange -> All, PlotStyle -> {Blue, Full, Thick}, 
    AxesStyle -> Thickness[.001], LabelStyle -> {Black, Medium}],
  {n,-1,3-1/4,1/4}]] 
$\endgroup$
1
  • $\begingroup$ Thank you! That works nicely, I have used Show[p1,p2..] etc, but didnt think of using it in this way. $\endgroup$
    – Betty
    May 24, 2018 at 2:53

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.