Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2003
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Incrementing a "For" loop - "For loop.nb" 5893 Bytes yEnc

  • To: mathgroup at smc.vnet.net
  • Subject: [mg39703] Re: Incrementing a "For" loop - "For loop.nb" 5893 Bytes yEnc
  • From: "John Doty" <jpd at w-d.org>
  • Date: Mon, 3 Mar 2003 04:24:57 -0500 (EST)
  • References: <b3rsiq$dma$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

In article <b3rsiq$dma$1 at smc.vnet.net>, twm145 at psu.edu wrote:

> I have an 11 x 11 matrix "A", and an 11 x 1 matrix "Told" I would like
> to find the dot product of A and Told and store this result as Tnew
> then, increment a variable deltat that appears in A to deltat + 30.
> 
> I would then like to designate the answer Tnew as Told and perform the
> calculation again, and repeat the procedure until deltat has reached
> 3600.
> 
> I have been trying to get a For loop to accomplish this but it appears
> that the incrementing of deltat and the reassignment of Tnew to Told is
> not occuring.

First, it is a bit hazardous to start names with capital letters since
built in functions use names of that style.

The built in function Fold is designed for your kind of problem. I'll do a
2D example for simplicity. First, define a function to perform a single
step:

In[1]:= matA={{1,-1},{1,deltat}};

In[2]:= f[vecT_,deltat_]=matA.vecT

Out[2]= {{1, -1}, {1, deltat}} . vecT

Now, start at {-1,1}, with deltat ranging from 0 to 90.

In[3]:= Fold[f,{-1,1},Range[0,90,30]]

Out[3]= {1952, -172859}

Simple, eh? If you want to see intermediate results, use FoldList:

In[4]:= FoldList[f,{-1,1},Range[0,90,30]]

Out[4]= {{-1, 1}, {-2, -1}, {-1, -32}, {31, -1921}, {1952, -172859}}

Remember that Mathematica is a very strange programming language: it's
based on recognizing patterns and making substitutions. This can mimic
either procedural programming (For loops, etc.) or functional programming
(as above). However, the subtleties of getting the substitutions to happen
in the way you intend are generally much less confusing in the functional
style. For me, procedural programming is a last resort, especially since
it's usually clearer, simpler and faster to use the functional style.

Go look up the definition of Fold. It's not really as mind-boggling as it
appears, so be patient with it.

Even in the functional style, you have to be a little careful. The
definition of f must use "=", not ":=". This is because the symbol deltat
must appear explicitly in the definition to receive the argument. With
":=", matA would be evaluated after argument substitution, so deltat
wouldn't be substituted.

-- 
| John Doty		"You can't confuse me, that's my job."
| Home: jpd at w-d.org
| Work: jpd at space.mit.edu


  • Prev by Date: Re: Help Providing a Module
  • Next by Date: Re: how use NDSolve with an ODE having parameters
  • Previous by thread: RE: Incrementing a "For" loop - "For loop.nb" 5893 Bytes yEnc
  • Next by thread: Re: signal detector question