I have a few expressions $f(\theta,x)$, $g(\theta,x)$, $h(\theta,x)$ in terms of two variables, an angle and a length. The angle goes from $-\pi/2$ to $\pi/2$ and the length from 200 to 1500. This is my domain.

I would like to first generate a list of the domain values of time and length, say, in 10m increments (200, 210, ..., 1490, 1500) and say 30 equally spaced values from $-\pi/2$ to $\pi/2$ for the angle.

I would like to then run $f$, $g$ and $h$ for each datapoint in the domain and generate a list of the output values of each function (the total "range"). I have been reading up on Map and Thread and that sort of thing but am struggling with generating the domain lists I want and with threading multiple variables. Any help would be great!


2 Answers 2


Here is an alternative approach using Table. I find this more readable, although the one using Outer is more compact:

list = Table[
   Through[{f, g, h}[theta, x]],
   {theta, Subdivide[-Pi/2, Pi/2, 30]},
   {x, 200, 1500, 10}

This will provide the list as triplets of $f,g,h$ values, as follows:

(* Out: {{f[-(π/2), 200], g[-(π/2), 200], h[-(π/2), 200]}, 
         {f[-(π/2), 210], g[-(π/2), 210], h[-(π/2), 210]}, 
         {f[-(π/2), 220], g[-(π/2), 220], h[-(π/2), 220]}, ... 
         {f[π/2, 1490], g[π/2, 1490], h[π/2, 1490]}, 
         {f[π/2, 1500], g[π/2, 1500], h[π/2, 1500]}} *)

If you would rather have lists of $f$ values, followed by a list of $g$ values, etc, you can Transpose the list:


(* Out: {{f[-(π/2), 200], f[-(π/2), 210], f[-(π/2), 220], ..., f[π/2, 1490], f[π/2, 1500]},
         {g[-(π/2), 200], g[-(π/2), 210], ...}
  • $\begingroup$ I really appreciate the detailed answer - this solves my problem as well as giving me some insight into how Through works. A couple quick follow-ups - how do I use items from this list, i.e. - call list items in some other function, i.e. somefunction(fval,gval,hval) for each triplet? - scan items from the list such as using NDSolve with initial conditions f[0] = fval, g[0] = gval, etc and run this for each triplet of values Basically I really like the list of triplets, but not sure how to access these, i.e., telling a function which takes fval, gval...to scan across triplets Thanks! $\endgroup$ Jun 5, 2018 at 18:21
  • $\begingroup$ +1 for teaching me the function Subdivide, where I always did it manually. $\endgroup$
    – SPPearce
    Jun 5, 2018 at 19:11
  • $\begingroup$ @SarahThompson I am glad you found my answer helpful! From your description of the desired operation, it sound like you could Apply an appropriately constructed function to list at level 1, I.e. f @@@ list. Try that with an undefined f on a shorter list to see how it works. In your case, here is a made up usage example with NDSolve, where I use each triplet as a boundary condition: NDSolve[{someEquations, f[0] == #1, g[0] == #2, h[0] == #3}, {f, g, h}, {x, someVal, someOtherVal}]& @@@ list. $\endgroup$
    – MarcoB
    Jun 6, 2018 at 2:46
Outer[f, Range[-15, 15] Pi/30, Range[200, 1500, 10]]

(Ok, that's actually 31 values for the angle, but that seems cleaner -- seems like you'd want zero to be one of the angles.)

For all three:

Outer[{f@##, g@##, h@##}&, Range[-15, 15] Pi/30, Range[200, 1500, 10]]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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