I am trying to scan a parameter space of varying numbers of parameters subject to some constraints (I am interested in any number of constraints just out of curiosity, but in reality no more than 2 constraints in the actual). So it would look something like :


Where the ci are constraints and the list_i's take the form

 {x_i,x_i_initial, x_i_final, x_i_increment} 

that will be fed to a Do loop.

The problem is that I want to change the actual value of the x_i so that the c_j's (which are a function of x_i) know that x_i has changed value.

For example I might have a constraint like


I want to scan over values of y and z, solve for the corresponding values of x, then store all the possible solutions in a list. By putting the variables I want to scan over in an array it seems easier since then I can have Mathematica see the size of the array, and run the proper number of loops.

Obviously if I just put the variables in a function like

 TestScan[c1,...,cm,x_i,x_i_initial, x_i_final, x_i_increment,....]

and then I would call it like

      TestScan[c[x1,x2],x1,x1initial, x1final, x1increment,x2,x2initial, x2final, x2increment]

it works since know Mathematica knows that the argument of c is that same thing as the x1 that appears elsewhere, but I would have to change the code every time I change the number of variables in the constraints. My naive straightforward generalization of the just above does't work when I try to pass all

 {x_i,x_i_initial, x_i_final, x_i_increment} 

in a list. In this case mathematica might replace the 1 element in the list with something else, but I need to be able to change the value of x_i itself.

  • $\begingroup$ Sorry, but I don't understand how do you plan to link the c's with the corresponding x_i's if you don't declare them explicitly $\endgroup$ – Dr. belisarius Mar 21 '12 at 15:23
  • $\begingroup$ If I call it like TestScan[c[x1,x2],{x1,x1initial, x1final, x1increment}] they should be linked no? $\endgroup$ – DJBunk Mar 21 '12 at 15:29
  • 4
    $\begingroup$ It's usually better for questions like these to focus on explaining what the problem is, and not focusing on your idea how to solve it. The latter might help, the first will leave everyone the freedom of unbiased thinking about how to solve things. For example, your suggestion of a Do loop is often considered bad practice in Mathematica. $\endgroup$ – David Mar 21 '12 at 15:32

I would start by creating a function that returns a value based on your equation, i.e. the solution of your example $x^2+y^2+z^2=1$:

findSolutions[y_, z_] := Module[{x},
    x /. Solve[x^2 + y^2 + z^2 == 1, x]

The output looks like this:

findSolutions[1, 2]
{-2 I, 2 I}

Next, set up the table of $(y,z)$ values you want to feed that function, for example by generating random numbers or by using some formula:

sampleData = Table[{y, z}, {y, -2, 2, 1/2}, {z, -1, 1, 1/3}];
sampleData = Flatten[sampleData, 1]
{{-2, -1}, {-2, -2/3}, {-2, -1/3}, ...}

The second line is necessary since Table creates an additional nested layer for each variable it cycles through, so that the data initiall looks like {{{ ... }}}. The Flatten gets rid of this (here) unnecessary layer.

Alright, let's apply our function to the data,

findSolutions @@@ sampleData
{{-2 I, 2 I}, {-((I Sqrt[31])/3), (I Sqrt[31])/3}, ...}

findSolutions @@@ sampleData applies findSolutions to every sublist of sampleData, the result is a list of all results of the function based on the data provided. You can now do additional stuff with that, for exmple use Union (will sort the list as well) or DeleteDuplicates (won't do that) to get rid of double entries; you may also want to flatten the result, since findSolutions returns tuples of all possible solutions for a given $(y,z)$, etc. For example // Flatten // Union yields

{-1, 0, -I/3, I/3,  ...}
| improve this answer | |
  • $\begingroup$ Hi Dave, Thanks for all your time - this definitely helps a lot and puts me on a good track. In working through this, I had another question which which I posted separately. Thanks again. $\endgroup$ – DJBunk Mar 22 '12 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.