I am running v. 6.0.3.0
I am having some weirdness where I will do something like:
G=Array[f,100];
For[i=1, i<=100, i++,
f[i] = some calculation]
Then I will look at the result:
ListLinePlot[G]
The strange thing is, if I rerun this, but change 'some calculation'
to 'some other calculation', then rerun the plot, the plot stays the
same. If I rerun the loop again, then the plot will change as
expected. What's going on here? Why isn't it "taking" the first time?
David Bailey - 30 Oct 2008 07:59 GMT
> I am running v. 6.0.3.0
>
[quoted text clipped - 12 lines]
> same. If I rerun the loop again, then the plot will change as
> expected. What's going on here? Why isn't it "taking" the first time?
Wow - this is a muddle arising out of several misconceptions! First,
here is a correct version of this code:
G=Array[0&,100];
For[i=1, i<=100, i++,
G[[i]] = some calculation]
Note that G is an array, and you might as well assign its elements
directly. However, the syntax for array subscripts requires double
square brackets.
Now lets look at what happens when your code runs. For convenience, I
have reduced the number of points to 5, and simply examine the value of
G rather than plot it. I am using multiples of i to represent the
various different calculations.
G = Array[f, 100]
In[2]:= G = Array[f, 5]
Out[2]= {f[1], f[2], f[3], f[4], f[5]}
At this point G is an array of symbolic elements because the function f
is undefined, but then you provide an element by element definition of f:
In[5]:= For[i = 1, i <= 5, i++, f[i] = 2 i];
G
Out[6]= {2, 4, 6, 8, 10}
Here G has been fully evaluated and taken the point by point definition
of f.
Now the second time that you evaluate your code - to do something else -
f already has a value (the old one!), so G never passes through the
symbolic intermediate step:
In[7]:= G = Array[f, 5]
Out[7]= {2, 4, 6, 8, 10}
In[8]:= For[i = 1, i <= 5, i++, f[i] = 20 i];
G
Out[9]= {2, 4, 6, 8, 10}
Of course, the loop has changed the value of f, so that if you execute
the code a third time, you will get the second calculation!
Finally, lets do the job properly:
G=Table[5 i,{i,1,5}]
The Table command generates 5 answers by iterating through i and returns
an array result which gets assigned to G.
If in doubt, look at the intermediate values in a calculation! Also
distinguish clearly between an array and a function.
David Bailey
http://www.dbaileyconsultancy.co.uk
sjoerd.c.devries@gmail.com - 30 Oct 2008 08:00 GMT
Michael,
The result you obtained is not very suprising: you assign the old
values of f to G first and then generate new values for f. When you
plot G, you're plotting the old values of f.
With a fresh kernel, the first time you run this you'll see that the
plot won't work. That's because G has been assigned the value f which
the first time is omly a symbol and not a value at all.
Cheers -- Sjoerd
dh - 30 Oct 2008 08:03 GMT
Hi Michael,
Array returns a list of functions. Therefore, when you say G=Array[a,..]
and a[i] already has a value, the functions a[i] will be evaluated and
you end up with a list of numbers, not functions. To prevent this, Clear
a before using it.
hope this helps, Daniel
> I am running v. 6.0.3.0
> I am having some weirdness where I will do something like:
> G=Array[f,100];
> For[i=1, i<=100, i++,
> f[i] = some calculation]
> Then I will look at the result:
> ListLinePlot[G]
> The strange thing is, if I rerun this, but change 'some calculation'
> to 'some other calculation', then rerun the plot, the plot stays the
> same. If I rerun the loop again, then the plot will change as
> expected. What's going on here? Why isn't it "taking" the first time?

Signature
Daniel Huber
Metrohm Ltd.
Oberdorfstr. 68
CH-9100 Herisau
Tel. +41 71 353 8585, Fax +41 71 353 8907
E-Mail:<mailto:dh@metrohm.com>
Internet:<http://www.metrohm.com>
Michael Mandelberg - 31 Oct 2008 09:08 GMT
Very helpful answers, all of you. Thanks!