       Re: Dynamic programming and Manipulate

• To: mathgroup at smc.vnet.net
• Subject: [mg111464] Re: Dynamic programming and Manipulate
• From: Alice Lesser <alice.lesser at bwin.org>
• Date: Mon, 2 Aug 2010 07:05:32 -0400 (EDT)

```-----Original Message-----
From: Daniel Lichtblau [mailto:danl at wolfram.com]
Sent: den 29 juli 2010 18:16
To: Alice Lesser
Subject: [mg111464] Re: [mg111361] Dynamic programming and Manipulate

Alice Lesser wrote:
> Hi group,
>
> I'm attempting to model a 3-dimensional recurrence relation with two vari=
able parameters a and b as follows:
>
> discretemodel[a0_, b0_] :== Module[{a == a0, b == b0},
>   prob[1, 1, 1] == 1.0;
>   prob[1, x_, y_] == 0;
>   prob[t_, 0, y_] == 0;
>   prob[t_, x_, 0] == 0;
>   prob[t_, x_, y_] :== prob[t - 1, x - 1, y]*a + prob[t - 1, x, y - 1]*b =
+==
>  prob[t - 1, x, y]*(1 - a - b);
>   probabilities == Table[prob[t, x, y], {t, 1, 5}, {x, 1, 5}, {y, 1, 5}];
> (* some further manipulations of these t matrices of probabilities to be =
inserted here later *)
>   Grid[probabilities[]]
>   ]
> Manipulate[discretemodel[avalue, bvalue],
>  {{avalue, 0.2}, 0, 1, 0.1},
>  {{bvalue, 0.1}, 0, 1, 0.1}]
>
> This works fine for these small values of t,x and y, but aborts for large=
r values such as {t,1,20}. Surely a 20 x 5 x 5 matrix is not too large for =
Mathematica to handle?
>
> If I instead use the standard dynamic programming approach, so that line =
6 above becomes
>   prob[t_, x_, y_] :== prob[t,x,y] == prob[t - 1, x - 1, y]*a + prob[t - =
1, x, y - 1]*b + prob[t - 1, x, y]*(1 - a - b);
>
> then the calculation works fine for large values of t, but I lose the Man=
ipulate functionality (logically enough I suppose, since values of prob bec=
ome fixed).
> Is there a way to get around this problem?
>
> I'm new to Mathematica after many years with another system, and it may
> very well be the case that I'm not setting this model up right to begin w=
ith, any comm ents on possible better ways to do this would be much appreci=
ated!
>
>  Many thanks!
> Alice

Use dynamic programming inside your Module.

discretemodel[a_, b_] :== Module[
{prob,probabilities},
prob[1, 1, 1] == 1.0;
prob[1, x_, y_] == 0;
prob[t_, 0, y_] == 0;
prob[t_, x_, 0] == 0;
prob[t_, x_, y_] :==
prob[t, x, y] ==
prob[t - 1, x - 1, y]*a + prob[t - 1, x, y - 1]*b +
prob[t - 1, x, y]*(1 - a - b);
probabilities ==
Table[prob[t, x, y], {t, 1, 10}, {x, 1, 5}, {y, 1, 5}];
Clear[prob];
probabilities[]]

I'm not certain the Clear[prob] is needed (most likely not), but I put
it there just in case.

Daniel Lichtblau
Wolfram Research

----------------------

This worked perfectly, many thanks again Daniel! Reposting to group in case=
anyone else has similar trouble.
Cheers,
Alice

```

• Prev by Date: Re: Bare Bones Backup Button
• Next by Date: Re: Disable save in player pro
• Previous by thread: Re: Bare Bones Backup Button
• Next by thread: Re: Forcing Mathematica to use standard fonts