Group Abstract Group Abstract

Message Boards Message Boards

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

Posted 8 years ago
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

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.

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