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

GROUPS:
 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. Attachments:
Answer
2 months ago
4 Replies
 Gianluca Gorni 1 Vote 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]}] 
Answer
2 months ago
 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.
Answer
2 months ago
 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] 
Answer
2 months ago
 Hi Gianluca, thanks a lot. Now I am getting the full curve.
Answer
2 months ago