3
$\begingroup$

I have the following function. I want to graph its discrete plot and find the points where it takes on the highest value:

20*Log[20] - N2*Log[N2] - N3*Log[N3]-(20 - N2 - N3)*Log[20 - N2 - N3];

N2=Range[20];
N3=Range[20];
$\endgroup$
6
$\begingroup$
f[N2_, N3_] := 
  20*Log[20] - N2*Log[N2] - N3*Log[N3] - (20 - N2 - N3)*Log[20 - N2 - N3];

For the continuous function

max = NMaximize[
  {f[N2, N3], 1 <= N2 <= 18, 1 <= N3 <= 19 - N2},
  {N2, N3}, Reals]

(*  {21.9722, {N2 -> 6.66667, N3 -> 6.66667}}  *)

f[20/3, 20/3] // Simplify

(*  20 Log[3]  *)

% // N

(*  21.9722  *)

For the discrete function

maxInt = Maximize[
  {f[N2, N3], 1 <= N2 <= 18, 1 <= N3 <= 19 - N2},
  {N2, N3}, Integers]

(*  {-6 Log[6] - 14 Log[7] + 20 Log[20], {N2 -> 6, N3 -> 7}}  *)

maxInt[[1]] // N

(*  21.9213  *)

From the expression's symmetry

f[6, 7] == f[7, 6] == f[7, 7]

(*  True  *)

Show[
 Plot3D[f[N2, N3],
  {N2, 1, 18}, {N3, 1, 19 - N2},
  PlotStyle -> Opacity[.5]],
 Graphics3D[{Thick,
   Table[{
     Gray,
     Line[{
       {N2, N3, f[N2, N3]},
       {N2, N3, 0}}],
     If[N2 == 6 && N3 == 7 ||
       N2 == 7 && N3 == 6 ||
       N2 == 7 && N3 == 7, Red, Green],
     AbsolutePointSize[6],
     Point[{N2, N3, f[N2, N3]}]},
    {N2, 1, 18}, {N3, 1, 19 - N2}]}],
 AxesLabel -> (Style[#, 14, Bold] & /@
    {"N2", "N3", "f"})]

enter image description here

| improve this answer | |
$\endgroup$
5
$\begingroup$

Just a brute force approach:

f[x_, y_, z_] := With[{r = {20, -x, -y, -z}}, r.Log[Abs@r]]
ip = IntegerPartitions[20, {3}];
ord = Ordering[N[f @@@ ip]];
ans = Last@ip[[ord]]
can = Catenate[Permutations /@ ip];
pnt[x_, y_, z_] := {x, y, f[x, y, z]}
With[{c = pnt @@@ can}, Graphics3D[{Yellow, PointSize[0.015], Point[c],
   MapThread[Line[{#1, #2}] &, {(c /. {x_, y_, z_} :> {x, y, 0}), c}],
   Red, PointSize[0.02], Point[pnt @@@ Permutations[ans]]}, 
  Axes -> True, BoxRatios -> {1, 1, 1/2}, Background -> Black]]

enter image description here

Note: edited to remove misleading DiscretePlot3D showing points at places non-real values.

| improve this answer | |
$\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.