2
$\begingroup$

Here is my code

tt[t_] := (t/(1 - t));
b[k_, n_, pf_] := \!\(\*UnderoverscriptBox[\(\[Sum]\), \(i = 0\), \(k\)]\(Binomial[n, i] \*SuperscriptBox[\(pf\), \(i\)] \*SuperscriptBox[\((1 - pf)\), \((n - i)\)]\)\)
l[t_, n_] := 1/(1 + tt[t]^n) - b[Floor[n/2], n, t];
Table[FindMaximum[l[t, i], {t, 0.4}], {i, 3, 501, 2}]

The result is as follows:

{{0.148148, {t -> 0.333333}}, {0.204148, {t -> 0.384677}},.....

I want to Listplot the data: {(0.333333,0.148148),(0.384677,0.204148),.....}

but I dont know how to manipulate the output of Table so that I can plot it. I am not able to do it manually therefore wanted to ask it.

$\endgroup$

5 Answers 5

4
$\begingroup$
results= Table[FindMaximum[l[t, i], {t, 0.4}], {i, 3, 501, 2}]

ListPlot[Transpose[{t /. results[[All, 2]], results[[All, 1]]}]]

enter image description here

Explanation

results[[All, 1]] takes all the y values

t /. results[[All, 2]] replaces the rules creating a list of all the t values

Transpose to have it ready for ListPlot

$\endgroup$
2
$\begingroup$

And still another method is

ListPlot[Cases[results, {z1_, {Rule[_, z2_]}} :> {z2, z1}, Infinity]]

Mathematica graphics

$\endgroup$
1
$\begingroup$

That's like a breeze:

result = {{0.148148, {t -> 0.333333}}, {0.204148, {t -> 0.384677}}}

Flatten /@ result /. x_Rule :> x[[2]] // ListLinePlot 

PS: Because there are only 2 points better using ListLinePlot for illustration

$\endgroup$
1
  • $\begingroup$ When I try this it appears that the x and y axis are flipped $\endgroup$ Commented Aug 3, 2015 at 23:37
1
$\begingroup$

Let tab be the results table:

ListPlot[{t /. #2, #1} & @@@ tab]
$\endgroup$
3
  • $\begingroup$ Very terse as is your answer. I like it but don't quite understand it. Could you elaborate a bit as to what is going on? $\endgroup$ Commented Aug 3, 2015 at 23:43
  • $\begingroup$ @JackLaVigne this merely takes the result tab and converts into the coordinates...t/.#2 extractst from the rule and #1the maximum $\endgroup$
    – ubpdqn
    Commented Aug 4, 2015 at 10:34
  • $\begingroup$ I get it now. {t /. #2, #1}& is a pure function that works on each element of the tab list. Each element is a list and the symbol @@@ replaces the head of each element with this pure function. So (taking the first element as an example) argument #1 is 0.148148 and argument #2 is {t -> 0.3333}. $\endgroup$ Commented Aug 4, 2015 at 19:10
0
$\begingroup$
dt = Quiet@ Table[{Last@@#2, #1}&@@ FindMaximum[l[t, i], {t, 0.4}], {i, 3, 501, 2}];

ListPlot[dt]

enter image description here

You could also use:

dt = Quiet@ Table[{Last @@ #, #2} & @@ 
            Reverse@FindMaximum[l[t, i], {t, 0.4}], {i, 3, 501, 2}]; 
$\endgroup$

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.