Group Abstract Group Abstract

Message Boards Message Boards

0
|
16K Views
|
11 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Create a list that values are pulled from during a loop?

Posted 9 years ago

So what I am trying to do is to create a list of terms that will be plotted on separate graphs in a for loop. What I want to do is create a table of values and for each i there is a specific value. So basically I want to do this:

For[

variables in a table {i=1,i=2,i=3,i=4}

Plot some function with value associated to value in the table for this i value until you reach the last i value given in the list

POSTED BY: Nicholas Breslin
11 Replies

What is the code you have now? Some example code and expected output would be nice...

POSTED BY: Sander Huisman

I tried this:

table = {Sin[x], Cos[x], Cosh[x], Sinh[x], x^2};

Manipulate[Plot[table[[i]], {x, -2 \[Pi], 2 \[Pi]}], {i, 1, 5, 1}]

Manipulate[] works!

But, the For[] loop doesn't work:

For[i = 1, i <= 5, i++, Print[i]; 
 Plot[table[[i]], {x, -2 \[Pi], 2 \[Pi]}]]

1

2

3

4

5

Why?

For does not store the values of each 'loop'. You can use the Print statement to explicitly tell it to print the Plot.

Print[Plot[...........]]

But there are many alternatives:

Plot[#, {x, -2 \[Pi], 2 \[Pi]}] & /@ table
Scan[Print[Plot[#, {x, -2 \[Pi], 2 \[Pi]}]] &, table]
Do[Print@Plot[f, {x, -2 \[Pi], 2 \[Pi]}], {f, table}]
Table[Plot[f, {x, -2 \[Pi], 2 \[Pi]}], {f, table}]

Basically what I'm saying is, use Map, Scan, Do, Table and stay away from For as it is very error prone, generally slower, harder to write.

POSTED BY: Sander Huisman

Thank you, Sander! My code appears as the result of constructing an example for Nicholas. So, I found something important for myself.

Dear Sander,

Could we say that the code

table = {Sin[x], Cos[x], Cosh[x], Sinh[x], x^2};

For[i = 1, i <= 5, i++, Plot[table[[i]], {x, -2 \[Pi], 2 \[Pi]}]]

is an example of major bug for the For[] loop?

No! The code is completely OK and does what it does. Again: For does not store nor print any values! It just executes the loop, but does not print or return the results.

Remark:

  1. For can return a single value by using Return, but calling Return aborts the loop immediately.
  2. Use Print to show the output of the body for each iteration
  3. Use Sow and Reap to save any data

Let me repeat, try to stay away from For if you can replace it with Do.

POSTED BY: Sander Huisman

Thank you, Sander! So, we can conclude that the For[] loop is a function that doesn't have side effects as the For[] loop in the other procedural languages. Do I do the correct conclusion?

It can have side effects (Print, Sow...) It just does not store/print the results of the body. Use Table to do that.

For loop very similar in behavior like C (or any other language for that matter). It just executes the loop but does not store anything without an explicit print (printf) statement.

See Matlab, C, (visual) basic, python, ...... none of them stores the results of the body for each iteration. In all those languages you need an explicit print statement to see the results. Same for Mathematica

POSTED BY: Sander Huisman

I found why I misunderstood this example. I have thought that the Plot[] function works exactly as the Print[] function! So, the problem was not in the For[] loop, but in the effect which have the Plot[] function. It must be supplemented by Print[] to have the needed result!

So, the final code is:

table = {Sin[x], Cos[x], Cosh[x], Sinh[x], x^2};

For[i = 1, i <= 5, i++, 
 Print[Plot[table[[i]], {x, -2 \[Pi], 2 \[Pi]}]]]

enter image description here

This was almost exactly what I was trying to do, thank you very much!

POSTED BY: Nicholas Breslin

It was my pleasure!

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard