Re: lists of variables
- To: mathgroup at smc.vnet.net
- Subject: [mg58255] Re: lists of variables
- From: Paul Abbott <paul at physics.uwa.edu.au>
- Date: Sat, 25 Jun 2005 01:56:14 -0400 (EDT)
- Organization: The University of Western Australia
- References: <d9630u$q1t$1@smc.vnet.net><d9gb4h$sud$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
In article <d9gb4h$sud$1 at smc.vnet.net>, "Ray Koopman" <koopman at sfu.ca> wrote: > dh wrote: > > Hi Heath, > > You already got many answers, but I think the solution to create > > a variable name as a string and using ToExpression is not the > > appropriate way to do this. > > Array[..] is made for this purpose. > > I agree that using ToExpression to construct code is awkward and tends > to obscure what the code is, but for functions such as Minimize, that > require names of variables in their calling sequence, how else would > you handle problems in which the number of variables is not fixed? I'm not sure that I follow you. You can pass the necessary variables to such functions. For example, here is a function that generates a random polynomial in n variables: f[x_List] := Plus @@ Apply[Times, Outer[Plus, x, Table[Random[Integer, {-5, 5}], {Length[x]}]], {1}] For example, SeedRandom[1] g = f[{x,y}] h = f[{x,y,z}] Here is a routine that finds the mimimum over a specified domain. Minimize[{g, Thread[-2 < Variables[g] < 2]} , Variables[g]] Minimize[{h, Thread[-2 < Variables[h] < 2]} , Variables[h]] And here is a routine for numerical minima: Nmin[f_, {x_List, a_List}] := FindMinimum[f, Evaluate[Sequence @@ Transpose[{x, a}]]] Nmin[g, {Variables[g], {0,1}}] Nmin[h, {Variables[h], {0,1,2}}] Of course, you cannot use Variables if there are other parameters in the functions. Cheers, Paul -- Paul Abbott Phone: +61 8 6488 2734 School of Physics, M013 Fax: +61 8 6488 1014 The University of Western Australia (CRICOS Provider No 00126G) AUSTRALIA http://physics.uwa.edu.au/~paul http://InternationalMathematicaSymposium.org/IMS2005/