3
$\begingroup$

I work on problem that need to define function with two list of data as follow

a = {0.1, 0.233, 0.65, 1.56};
b = {0.99, 0.8, 0.9, 1.9};

ELCDM1[z_?NumericQ, om_?NumericQ] := 1/Sqrt[om*(1 + z)^3 + 1 - om];

angdist[zmin_?NumberQ, zmax_?NumberQ, om_?NumberQ] := (3*10^5)/(

   1 + zmax) NIntegrate[ELCDM1[z, om], {z, zmin, zmax}];

"a" is list of lower bound of integrate and b is upper bound. The true outcome for my function "angdis" is {100707., 71762., 25625.6, 13508.}. For me I should put first list "a" to my function and at the end of my program put list "b" so as I did as follow the outcome is different with {100707., 71762., 25625.6, 13508.}.

angdist3[x_, om_] := (3*10^5)/(1 + #) NIntegrate[ELCDM1[z, om], {z, x, #}] & /@b;

angdist3[#, 0.3] & /@ a


here is the outcome :

{{100707., 92199.9, 97142.2, 111441.}, {82220.6, 71762., 77779.9, 
  98755.}, {32424.9, 16710.2, 25625.6, 
  64584.9}, {-42008.8, -65580.4, -52333.9, 13508.}}

I want to define function such as angdist3[x_, om_] at the end and give me : {100707., 71762., 25625.6, 13508.} but give me a wrong list .

Any suggestions would be greatly appreciated.

$\endgroup$
6
  • 1
    $\begingroup$ Perhaps you are looking for MapThread[angdist[#1, #2, 0.3]&, {a, b}] $\endgroup$ – Simon Woods Oct 25 '20 at 16:15
  • $\begingroup$ Thanks for replying.It's true when I put both list to first function but as I mentioned I should define in two- steps..first put 'b' that I define angdist3 and the last step put 'a'. $\endgroup$ – Soroush Najafian Oct 25 '20 at 17:01
  • 1
    $\begingroup$ In "angdist3[#, om] & /@ a" om has no value. angdist3 then feeds om to ELCDM1 that can therefore not be evaluated, neither can the integral. Is this what you want? $\endgroup$ – Daniel Huber Oct 25 '20 at 17:44
  • $\begingroup$ thanks ..it should be 0.3 $\endgroup$ – Soroush Najafian Oct 25 '20 at 17:58
  • 1
    $\begingroup$ How about Diagonal[angdist3[#, 0.3] & /@ a] $\endgroup$ – Simon Woods Oct 25 '20 at 20:35
4
$\begingroup$

If you need to compute all 16 (4×4) integrals and then extract the diagonal elements, use Diagonal:

Diagonal[angdist3[#, 0.3] & /@ a]

{100707., 71762., 25625.6, 13508.}

You could instead compute only those 4 results. MapThread will map over both a and b simultaneously:

MapThread[angdist[#1, #2, 0.3] &, {a, b}]

{100707., 71762., 25625.6, 13508.}

Alternatively you can give angdist the Listable attribute, and simply pass a and b as arguments:

SetAttributes[angdist, Listable]
angdist[a, b, 0.3]

{100707., 71762., 25625.6, 13508.}
$\endgroup$

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.