Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #6095

Re: delay evaluation inside RecurrencTable?

From Dana DeLouis <dana01@me.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: delay evaluation inside RecurrencTable?
Date 2011-11-01 05:08 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <j8nurl$eh7$1@smc.vnet.net> (permalink)

Show all headers | View raw


Hi.   Your question:  Why does Table behave as I want, 
but not RecurrenceTable?  Are both behaviors intentional? 

I may be wrong, but a recursive equation tries to give the next value based on previous values.
The introduction of a Random Value makes this impossible.
Working with a Machine Number that Random returns add to the unlikely event of solving a recursive equation.

Here's what you have:

rr:=RandomReal[{-1,  1}];
tbl1=RecurrenceTable[{a[n]==0.8*a[n-1]+rr, a[0]==100}, a, {n, 0, 10}]
{100., 80.4106, 64.7392, 52.202, 42.1722, 34.1484, 27.7294, 22.5942, 18.486, 15.1994, 12.5702}

We solve for this particular constant random value by looking at just the first 3 values.
(I'll keep a copy in variable kk)

{a, b, c}=%[[1;;3]]
{100., 80.4106, 64.7392}

kk=rdm/.Solve[.8*a+rdm==b, rdm]//Last
0.410648

rdm/.Solve[.8*b+rdm==c, rdm]//Last
0.410648

Hmmm!  Yep,  it used the same value for the random number.
I'll use k to represent the random value.

What RecurrenceTable  was really solving was:

FullSimplify[Rationalize[
RSolve[{a[n]==0.8 a[n-1]+k, a[0]==100}, a[n], n]
]]

a[n]-> 5 (k-(k-20) (4/5)^n)

Notice that k (The random value) shows up twice,  which adds to the difficulty of calling it once.

If we substitute the same constant Random number back in:

tbl2=Table[5 (k-(k-20) (4/5)^n), {n, 0, 10}]/. k->kk ;

Which generates the same table as above:

tbl1==tbl2
True


Variations along this theme might be:

fx[n_]:= 5 (k-(k-20) (4/5)^n) /. k-> RandomReal[{-1, 1}]

fx[0]
100

fx[1]
79.2259

fx[10]
8.29887

fx[10]
13.0158

fx[10]
12.9732

Just out of curiosity, the 10th value can range between 6.27 and 15.20.
This is not apparent from the initial use of RecurrenceTable.

gx[n_]:=5 (k-(k-20) (4/5)^n) 

gx[10] /.k->-1.
6.27429

gx[10] /.k->+1.
15.2005

= = = = = = = = = =
HTH   :>)
Dana DeLouis
$Version
8.0 for Mac OS X x86 (64-bit) (February 23, 2011)

To understand recursion, one must first understand recursion.
= = = = = = = = = =

 


On Oct 29, 7:08 am, Alan <alan.is...@gmail.com> wrote:
> My first attempt to simulate an AR(1) process was
> RecurrenceTable[{a[n] == 0.8*a[n - 1] + RandomReal[{-1, 1}],
>   a[0] == 100}, a, {n, 1, 20}]
> which of course failed because RandomReal was only evaluated once.
> 
> Is there some way to get a new evaluation of RandomReal for each n (*inside* RecurrenceTable)?
> 
> Thanks,
> Alan Isaac
> 
> PS Sorry if this repeats. I posted this question days ago and it has not yet appeared.




Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: delay evaluation inside RecurrencTable? Dana DeLouis <dana01@me.com> - 2011-11-01 05:08 +0000

csiph-web