Skip to main content
12-Amethyst
September 13, 2022
Solved

Optima - a custom function for finding minima and maxima

  • September 13, 2022
  • 3 replies
  • 3009 views

Greetings,

 

Across all industries, when we do math we often want to find the minimum or maximum point of a function. There are many ways, but the most reliable is to do it iteratively. I have built a function that does this. Attached is the function itself. Put it in a closed area at the start of your template so that you can always call it.

 

The inputs of optima(f,x0,xf) are any single variable function "f", and the starting value "x0" and ending value "xf" of the domain of that function. It outputs the x value of the function at its maxima, the value at that x value, the x value of the minima, and the value of the function at its minima. It finds absolute maxima and minima of the function only. You can make a multivariable function be single variable by defining a new function of a single variable, where the other inputs are all constants. For example, f(x)=g(x,c1,c2,c3)

 

This is the interesting part: To make it run quickly but still get a precise answer, I split the process into two parts. The program splits the domain into 1000 increments and looks for the maxima and minima, and then it zooms in on the x value and repeats this process with a new, tighter domain centered at the x value of the found minima/maxima.

 

I have also built in a help feature where if you enter "?" into any input, it displays an explanation of how the function works. I make this feature standard on my custom functions now. 

 

Please, tell me what you think? Is it useful? Redundant with something else?

 

Any input is welcome. Thank you.

 

PS. For users not using Prime 8, here is what it looks like:

KevinFinity_0-1663102447078.png

 

Best answer by LucMeekes

Nice, may be handy for functions for which the derivative doesn't exist.

Did you try it out / do you have an example of usage? I found this:

LucMeekes_0-1663146590138.png

I guess you didn't need the help yourself...

 

Success!
Luc

3 replies

LucMeekes23-Emerald IVAnswer
23-Emerald IV
September 14, 2022

Nice, may be handy for functions for which the derivative doesn't exist.

Did you try it out / do you have an example of usage? I found this:

LucMeekes_0-1663146590138.png

I guess you didn't need the help yourself...

 

Success!
Luc

12-Amethyst
September 14, 2022

Hi Luc,

 

I took your suggestion to add some examples. See the new Optima below for these. Indeed, it comes up with an error when evaluating sin(x)/x. I built this function for engineering problems without asymptotes or dividing by 0 situations - these rarely come up in my practice. After looking at my code again, I found an error where it reports the xmin as fmin, so I've rewritten it. I also replaced the while loops with for loops because I was using while as for anyways, and it just works better with for loops because it will never hang.

 

The new attachment is the latest version.

 

23-Emerald IV
September 14, 2022

Suggestion: change the test for help to:

LucMeekes_0-1663151380637.png

The or( ) function requires two arguments, matrices each that must be of same size. }

After that is solved, I get:

LucMeekes_1-1663151478621.png

and

LucMeekes_4-1663151771523.png

but:

LucMeekes_5-1663151796954.png

and

LucMeekes_3-1663151560411.png

Doesn't seem to be able to find a solution...

 

Success!
Luc

12-Amethyst
September 14, 2022

Oops! Good catch, Luc. 

 

I forgot to include the or() function in the sheet. If any of the logical statements in the vector or matrix "x" are 1, then or(x) returns 1. If any are not 1 or 0, then it returns an error. If x is "?", then it returns the help message.

 

 

KevinFinity_0-1663171659674.png

 

23-Emerald IV
September 14, 2022

You attached optima.mcdx, not or.mcdx...and it doesn't include or().

A few remarks on or():

- Note that you are overriding the Prime defined function or().

- What if ORIGIN=1 (or any other value differing from 0...?

- you can use the 'not' operator to test if x is not an array:

LucMeekes_0-1663173890137.png

and in this case you want to issue an error, so I would

LucMeekes_1-1663174019878.png

if you do it your way, the result of the or() function becomes a string.

- You test for elements of x to be either 1, or 0 and else the result of your or() function is again a string...

Note that by default Prime understands 0 as FALSE, any other value as TRUE. You can (ab)use that.

 

Success!
Luc

 

 

25-Diamond I
September 14, 2022

I can't open your Prime file so I depend on the picture you posted.

I tend to complain about the determination of the limits x.0min/max and x.fmin/max to refine the results after the first run throug with 1001 equidistant x-values, and I do it in a couple of ways:

  • Your method fails or gives wrong interval limits when the x values for min and max are negative. I think that is what Luc had also found.
  • The accuracy depends on the absolute x-value. Imho the limits should not be x +- 10% of the x-value, but they could be about x +- (x.f-x0)/n.
  • If xMin or xMax is exactly at an interval end (x.f or x.0), the interval is extended beyond the given one and you may get a result which lies 10% outside the initially given limits
25-Diamond I
September 14, 2022

Here is an alternative. Its done in Mathcad 15

Werner_E_1-1663242365623.png

Werner_E_2-1663242408528.png

 

You may add an argument which determines how often the "refinement" you introduced should be applied.

Werner_E_4-1663190571575.png

 

Often you won't see much improvement by these refinements, sometimes it helps increasing accuracy

Werner_E_5-1663190604095.png

MC15 worksheet attached

 

EDIT: Added improvements to deal with complex function values (they are ignored) and fixed a bug which occurred when the function evaluation threw an error.