Function to calculate minima and maxima of a function using Lagrange Multipliers

12292019, 07:26 AM
(This post was last modified: 12292019 06:37 PM by hamorabi.)
Post: #1




Function to calculate minima and maxima of a function using Lagrange Multipliers
Sometimes, we are interested in finding the minima and maximum of a function ff subjected to a constraint by one or more constraints, usually some curve(s) along the surface of f.
The function lagrangemult takes 3 parameters: 1. ff: this is the function f(x,y,z) such that f(x,y,z)=0 2. cnt: this is/are the constraint(s) to which ff is subjected to. The constraints should be entered separately in a list and defined as g(x,y,z)=0 3. vars: These are the variables used. Here are a couple of examples, mostly taken from this page: PHP Code: lagrangemult(5x3y,[x^2+y^2136],[x,y]) With 2 constraints: PHP Code: lagrangemult(4y2z,[2xyz2, x^2+y^21],[x,y,z]) The result is presented in a matrix format. The first j columns correspond to the values of of the variables. For instance, column 1 is for the xcoordinate, column 2 for y, etc. The last column is the function ff evaluated at the point P(x,y,z). Each row corresponds to either a minima or a maxima. For example, if there are 4 rows, then there can be 3 local minima and 1 maxima, or some other mixture. The function works well in most cases except in those where the solver fails to find a solution even though a solution exists. I have attached a screenshot that demonstrates this. You can clearly see that the function arrives at the same equations as in the example. However, for a reason which I don't understand at the moment, the solver fails. If someone has any idea why this is happening or what should be done to prevent it, I'd be happy to adjust the code. This is the problematic case. Also see attached image: PHP Code: lagrangemult(x^2+2y^2+z,[x^2z^22],[x,y,z]) Finally, the code: PHP Code: #cas 

« Next Oldest  Next Newest »

User(s) browsing this thread: 1 Guest(s)