1
$\begingroup$

How can I print the values of the function expere in the table of the function exper ... Note that I use this code

exp := Subscript[x, 2] Subscript[x, 3] + 
  Subscript[x, 1] Subscript[x, 2] Subscript[x, 3] Subscript[x, 4]

exper := Subscript[x, 1] Subscript[x, 3] + 
  Subscript[x, 1] Subscript[x, 2] Subscript[x, 3] Subscript[x, 4]

variables = exp // Variables // Sort;

Module[{expValue, experValue, varValues}, 
 With[{nbrOfResults = 10}, 
    Table[Catch[
       Do[If[0 <= (experValue = 
            Round[exper /. 
              Thread[variables -> (varValues = 
                  Round[RandomReal[{0, 1}, Length[variables]], .1])], 
             0.01]) < 1, 
         Throw[{NumberForm[#, {3, 1}] & /@ varValues, 
            NumberForm[experValue, {4, 2}]} // Flatten]], 
        10000]], {nbrOfResults}] // SortBy[#, Last] &] // 
   Prepend[#, {variables, "exp", "exper"} // Flatten] & // 
  Grid[#, Frame -> All] &]

This program compensates the values of the variables of a function and gives the result of the function in the shape of a table ... A column should be added to a second function with the same variables as in the image

enter image description here

$\endgroup$

1 Answer 1

2
$\begingroup$

Add them along with the other NumberForms and adjust the sort criterion

Module[{expValue, experValue, varValues}, 
 With[{nbrOfResults = 10}, 
    Table[Catch[
       Do[If[0 <= (experValue = 
            Round[exper /. 
              Thread[variables -> (varValues = 
                  Round[RandomReal[{0, 1}, Length[variables]], .1])], 
             0.01]) < 1, 
         Throw[{NumberForm[#, {3, 1}] & /@ varValues, 
            NumberForm[experValue, {4, 2}], 
  NumberForm[exper /. Thread[variables -> varValues], {4, 2}]} // Flatten]],
        10000]], {nbrOfResults}] // SortBy[#, #[[-2]] &] &] // 
   Prepend[#, {variables, "exp", "exper"} // Flatten] & // 
  Grid[#, Frame -> All] &]
$\endgroup$
4
  • $\begingroup$ what mean SortBy[#, #[[-2]] &] &] When the equations are replaced by more variables it is so wrong Part::partd: Part specification Null[[-2]] is longer than depth of object. $\endgroup$ Jun 11, 2017 at 17:16
  • $\begingroup$ But the code does not produce the correct results $\endgroup$ Jun 11, 2017 at 17:29
  • $\begingroup$ @Emadkareem The code below "I used this answer" is almost the same as what I wrote, but you replaced experValueby expValue in the If condition, so experValue remains a symbol. I don't see how you encountered Null[[-2]] though $\endgroup$
    – Coolwater
    Jun 11, 2017 at 18:04
  • $\begingroup$ The problem has been resolved in your answer. You have repeated the function experValue and have not noticed it $\endgroup$ Jun 11, 2017 at 18: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.