Re: Problem with loops in Mathematica
- To: mathgroup at smc.vnet.net
- Subject: [mg49745] Re: [mg49693] Problem with loops in Mathematica
- From: "David Park" <djmp at earthlink.net>
- Date: Fri, 30 Jul 2004 06:01:59 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Kristofer, The following will substitute n in the expression AND in the iterator and then generate the array. nodevalues[n_] := Table[S u^j d^(n - j) p^j q^(n - j), {j, 0, n}] nodevalues[3] {d^3*q^3*S, d^2*p*q^2*S*u, d*p^2*q*S*u^2, p^3*S*u^3} You might also write the definition as Clear[nodevalues] nodevalues[S_, u_, d_, p_, q_][n_] := Table[S u^j d^(n - j) p^j q^(n - j), {j, 0, n}] Then you don't have to set S,u etc but just supply them when you call the function. nodevalues[j + 2, 3, 2, 2, 1][4] {32, 144, 576, 2160, 7776} David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ From: Kristofer Eklundh [mailto:kristofer_eklundh at spray.se] To: mathgroup at smc.vnet.net Hi all, I am trying to work out the value of S at different nodes in a binomial tree, using the following formula: Table[S u^j d^(n - j) p^j q^(n - j), {j, 0, n}] I would like to loop this in some way so that I don't have to assign new values to n all the time (S, d, p, q are given). I have tried For and Do, but when i print the result, Mathematica produces n lists that are equal to the final value of n, and not for n=0,1,..., n-1. Anyone who could help me with this?