1
$\begingroup$

I need to take two lists (each list contains the number of moles of a chemical). I have a function calledFraction that takes the each entry of the list and then calculates the mole fraction of Chloroform. Each entry in the new list should be the mole fraction calculated by taking
molesChloroform[[1]]/(molesChloroform[[1]] + molesAcetone[[1]]

Here is what I have tried:

molesChloroform = {0.125, 0.100, 0.0625, 0.0375}
molesAcetone = {0.027, 0.068, 0.0952, 0.136}
moleFraction[molesA_, molesB_] = molesA/(molesA + molesB)
{moleFraction[#1, #2]} & @@@ {molesChloroform[[1 ;;]], 
  molesAcetone[[1 ;;]]}
Out[23]= {{0.555556}, {0.284211}}

This is not the correct output.

$\endgroup$
2
  • $\begingroup$ Do you get the correct output if you change @@@ to @@? Or, better yet, if you use moleFraction[molesChloroform,molesAcetone ]? $\endgroup$
    – kglr
    Apr 5, 2013 at 5:04
  • $\begingroup$ yes I do. Thank you. I don't know why I didn't think your simpler solution wouldn't work. both give the same output. And i have verified it is correct. $\endgroup$
    – olliepower
    Apr 5, 2013 at 5:14

2 Answers 2

3
$\begingroup$

A solution to compute the ratio for any two lists of mole values, based on MapThread:

mf[mA_, mB_] := MapThread[#1/(#1 + #2) &, {mA, mB}]

mf[molesChloroform,molesAcetone]

(*{0.822368, 0.595238, 0.396322, 0.216138}*)
$\endgroup$
1
$\begingroup$

I changed

{moleFraction[#1, #2]} & @@@ {molesChloroform[[1 ;;]], 
 molesAcetone[[1 ;;]]}

to

{moleFraction[#1, #2]} & @@ {molesChloroform[[1 ;;]], 
 molesAcetone[[1 ;;]]}

And got this correct result:

{0.822368, 0.595238, 0.396322, 0.216138}

Also

moleFraction[molesChloroform, molesAcetone]

give the same result.

$\endgroup$
1
  • $\begingroup$ Yes, all sub-operations are Listable therefore the function moleFraction can be used directly. See Case #4 here. $\endgroup$
    – Mr.Wizard
    Apr 5, 2013 at 7:04

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.