Group Abstract Group Abstract

Message Boards Message Boards

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

Posted 8 years ago

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:
POSTED BY: Sourabh Lahiri
4 Replies
Posted 8 years ago

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

POSTED BY: Sourabh Lahiri

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]
POSTED BY: Gianluca Gorni

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]}]
POSTED BY: Gianluca Gorni
Posted 8 years ago
POSTED BY: Sourabh Lahiri
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard