Message Boards Message Boards

0
|
1728 Views
|
1 Reply
|
2 Total Likes
View groups...
Share
Share this post:

How to print specific graphs from a list of graphs?

Posted 1 year ago

I created my graphs using the following code,

ClearAll[x,y,t]
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}];
y2=Table[Plot[y[x,t],{x,0,10}, AxesLabel->{"x","y[x,t]"},PlotLegends->StringJoin["t=", ToString[t]]],{t,0,2l/v,.02l/v}];
ListAnimate[y2]

Was wondering how I'd print separately (from the table), the graphs of, for example, t=0, t=3, t=5, t=7. I tried this, but no luck:

If[t=(0||3||5||7), Print[y2]]
POSTED BY: Lewis Jones
Posted 1 year ago

Let's first look at what you tried:

If[t=(0||3||5||7), Print[y2]]

Several issues here:

  • You don't use Print to produce an output. Print puts things in a special sort of stream, and it's typically used for generating error messages or providing any sort of meta-information (for debugging, for example). So you would just do If[t=(0||3||5||7), y2]. But the output here would be what y2 evaluates to, which is your entire list (we'll fix this later).
  • The expression t = (0 || 3 || 5 || 7) is an assignment statement. All you've done here is assign the expression Or[0, 3, 5, 7] to the symbol t. If expects a True or False, so this ends up doing nothing at all.

We could keep discussing If, but this actually isn't the direction you want to take at all. You have a list of things stored in y2 and you want to extract some elements. One way to do that is with Part (which is usually used in the short form symbol[[index]]. So, the 5th element of y2 could be fetched like this

y2[[5]]

You can fetch several at the same time by providing a list:

y2[[{5, 6, 7}]]

There's also a fancy iterator-ish notation, but we don't need to bother with that. So, if you knew where your desired plots were (what their position/index was) you could use Part to fetch them. But you want to get the plots for specific values of t, so it'd probably be easier to just regenerate those plots with the specific t values you're interested in:

Table[
  Plot[
    y[x, t], {x, 0, 10}, 
    AxesLabel -> {"x", "y[x,t]"}, 
    PlotLegends -> StringJoin["t=", ToString[t]]], 
  {t, {0, 3, 5, 7}}]

Typically, iterators can be given a list of explicit values to use in addition to the normal {iterator, <first>, <last>, <step>} specification.

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