From: Alice Lesser on 29 Jul 2010 06:41 Hi group, I'm attempting to model a 3-dimensional recurrence relation with two variable 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[[5]]] ] 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 larger 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 Manipulate functionality (logically enough I suppose, since values of prob become 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 with, any comm ents on possible better ways to do this would be much appreciated! Many thanks! Alice
From: Alice Lesser on 2 Aug 2010 07:05 -----Original Message----- From: Daniel Lichtblau [mailto:danl(a)wolfram.com] Sent: den 29 juli 2010 18:16 To: Alice Lesser Subject: Re: 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[[5]]] > ] > 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[[5]]] 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
|
Pages: 1 Prev: How to remove cells with a style? Next: AxesLabel parallel to 3D axes? |