# How to visualize a Lagrange problem with Mathematica?

I had asked a question here, which I was advised to open a new question for my further problems.

I have a function as $$f(a_1, a_2) = a_1^2 \sigma_1^2 + a_2^2 \sigma_2^2$$, which is subjected to the constraint: $$a_1 + a_2 = 1$$. With the help of Mathematica, as stated in my previous question, or analytically, one can obtain that $$a_i = \frac{\sigma_i^{-2}}{\sum_{j = 1}^2 \sigma_j^{-2}},$$ minimizes $$f$$, which the minimum of $$f$$ is: $$1/\sum_{j = 1}^2 \sigma_j^{-2}$$.

Is there any possibility to visualize this result, that is, to show this particular $$a_i$$ minimizes $$f$$? I would like to vary the exponents of $$a_i$$ in the obtained formula in the above, let's say, from $$-1$$ to $$-3$$, and see how the values of $$f$$ changes and how it reaches its minimum when the exponent of $$a_i$$ is $$-2$$.

• hmm...initially you seem to want to vary the arguments $a_i$, since that's the kind of thing at which $f$ will be minimized by $\frac{\sigma^{-2}_i}{\sum_{j=1}^2\sigma^{-2}_j}$, but then it seems you want to vary the exponents in the definition of $f$ and see where the minimum is there. can you be more specific as to what you want the visualization to show different values of? Sep 26 at 23:09
• @thorimur Thanks for your comment. I think, my question is not formulated clearly. I need to vary the exponents in the obtained formula for $a_i$, which has $-2$ as exponents and investigate the behavior of $f$. For other values, for example, $-1$ or $-3$, the value of $f$ should become larger. I would like to see this behavior.
– ARez
Sep 26 at 23:15
• I think I see. Do you have explicit values of $\sigma_i$, given, for example, as \[Sigma][1] = val1; \[Sigma][2] = val2 (which will look nicer in mathematica)? Then, having evaluated the definitions f[a1_, a2_] := a1^2 \[Sigma][1]^2 + a2^2 \[Sigma][2]^2 and a[i_, exp_] := \[Sigma][i]^exp / (\[Sigma][1]^exp + \[Sigma][2]^exp), you could try Plot[f[a[1, exp], a[2, exp]], {exp, -1, -3}]. Does that work for you Sep 26 at 23:25
• (Sorry, had a typo there...multiple actually lol.) You could also rewrite a bunch of this if you wanted to generalize it, e.g. starting with a[i_, exp_] := \[Sigma][i]/Sum[\[Sigma][i]^exp, {i, 2}]. Sep 26 at 23:28
• @thorimur Thanks again for your comment. At the moment, I don't have specific values for $\sigma$'s, but they could be any positive number (for example, between $0$ and $6$). Please let me study your comment carefully. It would be great if you could also post it as answer!
– ARez
Sep 26 at 23:30

It's typically useful in Mathematica to translate mathematical notation of the form $$x_i$$ into x[i]. If you do that with \[Sigma][i] (which looks much better in a Mathematica notebook!), we could define explicitly

ClearAll[f, a, \[Sigma]];

f[a1_, a2_] := a1^2 \[Sigma][1]^2 + a2^2 \[Sigma][2]^2

a[i_, exp_] := \[Sigma][i]^exp / (\[Sigma][1]^exp + \[Sigma][2]^exp)


And then we could use Plot to vary exp, with chosen values of \[Sigma]

\[Sigma][1] = 0.2;
\[Sigma][2] = 0.8;
Plot[f[a[1, exp], a[2, exp]], {exp, -3, -1}]


We could also vary these with Manipulate, which is a nice tool! (Sometimes when doing this, it's useful to choose a fixed PlotRange, e.g. Plot[f[x],{x,0,1}, PlotRange -> {0,1}], otherwise the plot range changes with each drag of the slider. This can be distracting, but here, the variation in the function can be so small that it's difficult to see the minimum!)

Manipulate[
\[Sigma][1] = \[Sigma]1; \[Sigma][2] = \[Sigma]2;
Plot[f[a[1, exp], a[2, exp]], {exp, -3, -1}],
{\[Sigma]1, 0.1, 2}, {\[Sigma]2, 0.1, 2}]


Hope this helps! (Also be aware of Plot3D, DensityPlot, and all the rest!)

• Many thanks for this extremely helpful answer. I have noticed one strange behavior: For example, if we let the exponents change from $-5$ to $5$, for $\sigma_1 = 0.6$ and $\sigma_2 = 2.6$, it seems $f$ has also a maximum!
– ARez
Sep 26 at 23:59
• Interesting! You might be interested in seeing the general condition for when $f$ as a function of the exponent has derivative equal to 0: ClearAll[\[Sigma]]; Reduce[D[f[a[1, exp], a[2, exp]], exp] == 0, exp] // Simplify It's a bit thorny, but maybe something could be useful! (Keep in mind Mathematica uses a particular branch cut for Log on the complex plane; see the docs!) You'll also want to delete the Manipulate output cell for this, otherwise the change to \[Sigma] will cause it to dynamically update (and thus set \[Sigma] to a particular value again) Sep 27 at 0:05
• Thanks again, I will investigate it (in my previous question, it turned out that there is only one extremum which is minimum).
– ARez
Sep 27 at 0:09
• But what does this have to do with the core aspect of the constraint $a_1 + a_2 = 1$? Sep 27 at 17:31
• @DavidG.Stork You're right, it doesn't. But if you read the comments on the original post, you'll see that OP isn't actually interested in the Lagrange problem per se (and in minimizing $f$ by varying $a_i$), but seeing what happens when you feed $f$ the $a_i$ given by that formula of $\sigma$, and then varying the exponent on $\sigma$. I wasn't quite sure why, but that's what they wanted to visualize. I agree it could be a good idea to solve for $\sigma_2$ given $sigma_1$ given the exponent and the constraint, though, instead of letting both vary independently. Sep 27 at 21:51

Is this what you're seeking.....?

• Many thanks! Could you also please provide the code?
– ARez
Sep 27 at 5:36
• Nice answer! @ARez you are misinterpreting the goals of this forum. It is not a free homework service. You have to provide the code where some preliminary work is demonstrated. Sep 27 at 5:43
• @yarchik Hi, yarchik. This is not a homework question, and I don't think any teacher ask students to visualize the Lagrange problem,
– ARez
Sep 27 at 7:51
• @yarchik You are right. Initially, I had problem to formulate my question clearly (I had solved the optimization problem with Mathematica), so I was struggling there. Next time, I'll post the codes.
– ARez
Sep 27 at 8:15
• @yarchik In fact, I was trying to solve my friend's question, by Mathematica, posted here: mathoverflow.net/questions/404776/… Anyway, thanks for letting me know the culture of this website.
– ARez
Sep 27 at 8:19