# Use "Plot" command inside "For" loop and superpose in same figure?

**URL:** <https://community.wolfram.com/t/use-plot-command-inside-for-loop-and-superpose-in-same-figure/12475>\
**Category:** Questions\
**Tags:** equation-solving, graphics-and-visualization\
**Created:** [May 16, 2018, 9:37am UTC](https://community.wolfram.com/t/use-plot-command-inside-for-loop-and-superpose-in-same-figure/12475 "2018-05-16T09:37:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sourabhlahiri](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@sourabhlahiri](https://community.wolfram.com/u/sourabhlahiri)\
**Post date:** [May 16, 2018, 9:37am UTC](https://community.wolfram.com/t/use-plot-command-inside-for-loop-and-superpose-in-same-figure/12475/1 "2018-05-16T09:37:21Z")

</div>

I wish to include a “Plot” command within a “For” loop. The loop numerically solves a differential equation in intervals (0,\tau), (\tau,2\tau) and so on, with the initial condition changing each time (the solution at \tau at the end of first iteration becomes initial condition for the second). In the attached file, the definitions of functions are all fine, but the main trouble comes while running the For loop: it produces the correct plots but in different figures. Can I superpose all these plots in the same figure, so as to get a single curve in the full range? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [May 16, 2018, 10:04am UTC](https://community.wolfram.com/t/use-plot-command-inside-for-loop-and-superpose-in-same-figure/12475/2 "2018-05-16T10:04:07Z")

</div>

This is one way:

```
Anstot1 = 
  NDSolveValue[{0.5 Derivative[1][\[Sigma]x][t] + (
      k[t] \[Sigma]x[t])/\[Gamma] == ftot[t], \[Sigma]x[0] == 
     ini}, \[Sigma]x, {t, 0, \[Tau]}];
Plot[Anstot1[Mod[t, \[Tau]]], {t, 0, 5 \[Tau]}]

```

---

<div class="post-metadata">

**Author:** ![sourabhlahiri](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@sourabhlahiri](https://community.wolfram.com/u/sourabhlahiri)\
**Post date:** [May 16, 2018, 11:48am UTC](https://community.wolfram.com/t/use-plot-command-inside-for-loop-and-superpose-in-same-figure/12475/3 "2018-05-16T11:48:10Z")

</div>

Hi Gianluca,  
Thanks for your response. This gives a slightly different plot, because the curve becomes periodic. Actually, as I had mentioned, the initial values is not the same for every iteration: \sigma[0] == ini for the first cycle/iteration. When the next iteration starts, the final value of \sigma[t] _at the end of first iteration should now become the initial value_, i.e. value of ‘ini’ now changes to \sigma[\tau]. However, due to some restrictions on the definition of ftot[t], I need to keep resetting the value of ‘t’ to zero at the end of each iteration, so that the initial condition “looks” same: \sigma[0] == ini for all iterations, with the value of ‘ini’ being extracted from the solution obtained at the end of the previous iteration.  
This procedure is embodied in the command

> ini = [Sigma]x[[Tau]] /. Anstot1;

in the third line of the loop.

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [May 16, 2018, 12:24pm UTC](https://community.wolfram.com/t/use-plot-command-inside-for-loop-and-superpose-in-same-figure/12475/4 "2018-05-16T12:24:39Z")

</div>

Ok, you can give a name to each plot and combine them with `Show`:

```
For[j = 0, j < 5, j++,
  Anstot1 = 
   NDSolve[{0.5 Derivative[1][\[Sigma]x][t] + (
       k[t] \[Sigma]x[t])/\[Gamma] == ftot[t], \[Sigma]x[0] == 
      ini}, \[Sigma]x, {t, 0, \[Tau]}]; 
  ini = \[Sigma]x[\[Tau]] /. Anstot1; 
  pl[j] = Plot[\[Sigma]x[Mod[t, \[Tau]]] /. Anstot1, {t, 
     j \[Tau], (j + 1) \[Tau]}]
  ];
Show[Table[pl[j], {j, 0, 4}], PlotRange -> All]

```

---

<div class="post-metadata">

**Author:** ![sourabhlahiri](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@sourabhlahiri](https://community.wolfram.com/u/sourabhlahiri)\
**Post date:** [May 17, 2018, 4:36am UTC](https://community.wolfram.com/t/use-plot-command-inside-for-loop-and-superpose-in-same-figure/12475/5 "2018-05-17T04:36:19Z")

</div>

Hi Gianluca, thanks a lot. Now I am getting the full curve.
