Message Boards Message Boards

0
|
1941 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
Share this post:

No output from Do

Posted 1 year ago

Hi, relatively new to Mathematica, but have been reading up. I'm trying to plot graphs of f1 over the continuous interval {x,0,10} , for t values that vary from t=0 to t=20, in increments of 0.2. My code isn't doing anything, and have tried different variations but it doesn't seem to fix the problem. If anyone could explain to me why I'm not getting results i would be very grateful. The code I'm using is ,

ClearSystemCache[]

f1=Sum[(8/(n^2Pi^2))(2Sin[n Pi/4]-Sin[n Pi/2])Sin[n Pi x/10]Cos[n Pi t/10],{n,1,40,1}];

f2= Do[Plot[f1,{x,0,10}],{t,0,20,.2}];

f3=Show[f2]

f4=Animate[f3]
POSTED BY: Lewis Jones
4 Replies
Posted 1 year ago

Thanks so much. Ive gotten my graphs to display, using the following code, even with a Do loop, like this:

ClearAll[y,x,t,framelist,nextframe]
h=1; l=10; v=1;
y[x_,t_]=Sum[(8h/(n^2Pi^2))(2Sin[n Pi/4]-Sin[n Pi/2])Sin[n Pi x/l]Cos[n Pi v*t/l],{n,1,40,1}];
framelist={};
Do[nextframe= Plot[y[x,t],{x,0,10}, AxesLabel->{"x","y[x,t]"},PlotLegends->StringJoin["t=",ToString[t]]]; AppendTo[framelist,nextframe],{t,0,2l/v,.02l/v}];
framelist; ListAnimate[framelist]; Part[framelist,{1,16,26,36}]

I have a question. As you'll notice here, the above code is capable of displaying the full frame list, as the line in my code, which says, "framelist", will give as its output, if I remove the semicolon. Lets say I were to do that, and the output displays as the full list. My question is, is there a way to select the entire outputted list, like all the graphics at once, then click somewhere and get these graphics to produce an animation? I've read that there is a way to do this, but you have to click on something called Animate Graphics in the Cell menu, and I keep being unable to find the Cell menu in Wolfram. (As you'll notice here, I used another methodm ListAnimate, to animate my list, but would like to also practice the method of selecting the figures)

POSTED BY: Lewis Jones

Hi Lewis,

I am unaware of a way to create an animation from a selection of Graphics or cells.

However, I would suggest learning to avoid procedural constructs like For, and Do in favor of functional constructs. In general, they will perform better. In the Wolfram Language, there are very few functions that mutate data. AppendTo is not appending to a list (that would be a mutation), it makes a copy of the list and appends to the copy. For large lists, this can have a significant performance impact. Not really an issue in your example, generating the plot is by far the most expensive operation. It also avoids polluting the namespace with unnecessary names, and the need to set framelist to an empty list, ... This is much cleaner

framelist =
 Table[
  Plot[y[x, t], {x, 0, 10},
   AxesLabel -> {"x", "y[x,t]"},
   PlotLegends -> "t=" <> ToString[t]],
  {t, 0, 2 l/v, .02 l/v}]
POSTED BY: Rohit Namjoshi
Posted 1 year ago

Thank you so much, Eric Rimbey! I tried the above changes and it fixed my problem.

POSTED BY: Lewis Jones
Posted 1 year ago

The result (as in the "output value" or the thing that it computes to) of a Do is Null. So, your f2 is Null and nothing after that will do what you want. What you could do is generate a List of plots:

f2 = Table[Plot[f1, {x, 0, 10}], {t, 0, 20, .2}]

To see all of these plots combined, you can still use Show:

f3 = Show[f2]

Be aware that the result of Show is a Graphics object, which won't work in an Animate expression. The way to animate a list of things is with ListAnimate:

f4 = ListAnimate[f2]
POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract