1
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$ – corey979 Nov 7 '16 at 19:03
3
$\begingroup$

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"}]

enter image description here

$\endgroup$
5
  • $\begingroup$ Yes, it works. Thank you. $\endgroup$ – Qi Zhong Nov 7 '16 at 19:32
  • $\begingroup$ @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. $\endgroup$ – corey979 Nov 11 '16 at 13:50
  • $\begingroup$ 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? $\endgroup$ – Qi Zhong Nov 14 '16 at 21:28
  • $\begingroup$ 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. $\endgroup$ – corey979 Nov 14 '16 at 21:39
  • $\begingroup$ 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. $\endgroup$ – Qi Zhong Nov 15 '16 at 16:52
2
$\begingroup$

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}]
$\endgroup$
1
  • $\begingroup$ Yes, the table can work in this way. But "Transpose[{F, g} /. Thread[{x1, x2, x3,->Transpose@Tuples..." doesn't work. $\endgroup$ – Qi Zhong Nov 7 '16 at 18:50

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.