# Maximize function with undetermined coefficient

My question is related to this one:

Parametric plot on multi-dimensional domain

I will use a simple example to state my pronblem.

I have a variable f and g, such as

f = x1+x2*Sin[t]+x3*Sin[2t];
g = x1+x2+x3;


x1, x2, x3 will have some values, then I need

F = Maximize[{f,0<t<10},t][[1]]


which means F is the maximum of f as function of t.

Then I will make {x1,0,10,1}, {x2,0,10,1}, {x3,0,10,1} to obtain many Fs and gs, and Plot F vs g.

The problems is when I write the code like this (as suggested in the Parametric plot on multi-dimensional domain):

ListPlot[Transpose[{F, g} /. Thread[{x1, x2, x3,->Transpose@Tuples[{Range[0, 10, 1], Range[0, 10, 1], Range[0, 10, 1]}]]


Or make it simple, just generate a table of F

Table[F,{x1,0,10,1}, {x2,0,10,1}, {x3,0,10,1}]


The problem is it seems that Mathematica will run the Maximize first, then give the x1, x2, x3's value to F. What I want is give values to x1, x2, x3 first, then take the maximum to obtain F.

Is there anyone know how to solve this.

• There are errors in you ListPlot: a comma instead of a }in the Thread part, and Transpose and ListPlot don't have their closing ] brackets anywhere. Nov 7, 2016 at 19:03

I'd do

f = x1 + x2*Sin[t] + x3*Sin[2 t];
g = x1 + x2 + x3;

tuples = N @ Tuples[Range[0, 10, 1], {3}];

F[i_] := Maximize[{f /. Thread[{x1, x2, x3} -> tuples[[i]]], 0 < t < 10}, t][[1]]
G[i_] := g /. Thread[{x1, x2, x3} -> tuples[[i]]]

data = Table[{G[i], F[i]}, {i, Length @ tuples}];

plot = ListPlot[data, Frame -> True, AspectRatio -> 1, FrameLabel -> {"G", "F"}]


• Yes, it works. Thank you. Nov 7, 2016 at 19:32
• @QiZhong I've noticed that you haven't accepted any answer to none of your questions. If you find some answers useful/helpful, consider accepting them by clicking the gray checkmark. Nov 11, 2016 at 13:50
• Sorry, I thought the click the upper arrow is enough. Now I have clicked the checkmark. I have a problem. That is it takes about 10 minutes to run my code for 1000+points. The reason I think is the function Maximize. Is there any way to reduce the time for running Maximize, for example reducing the precision of the result? Nov 14, 2016 at 21:28
• Maximize tries to find a global maximum symbolically, and is not that fast. You may try with NMaximize or related functions, like FindMaximum or so. On the other hand, 10 mins is not that long. Nov 14, 2016 at 21:39
• Do you know how to control the Precision of G[x]? For example G[4]=1.8364958495, I want G[4]=1.8365 by setting the Precision. Nov 15, 2016 at 16:52

If I understand correctly, this should work:

f[x1_, x2_, x3_] = x1 + x2*Sin[t] + x3*Sin[2 t];
F[x1_, x2_, x3_] := Maximize[{f[x1, x2, x3], 0 < t < 10}, t][[1]]
Table[F[x1, x2, x3], {x1, 0, 10, 1}, {x2, 0, 10, 1}, {x3, 0, 10, 1}]

• Yes, the table can work in this way. But "Transpose[{F, g} /. Thread[{x1, x2, x3,->Transpose@Tuples..." doesn't work. Nov 7, 2016 at 18:50