cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X

Optima - a custom function for finding minima and maxima

KevinFinity
12-Amethyst

Optima - a custom function for finding minima and maxima

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

 

ACCEPTED SOLUTION

Accepted Solutions
LucMeekes
23-Emerald III
(To:KevinFinity)

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

View solution in original post

9 REPLIES 9
LucMeekes
23-Emerald III
(To:KevinFinity)

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

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.

 

LucMeekes
23-Emerald III
(To:KevinFinity)

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

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

 

LucMeekes
23-Emerald III
(To:KevinFinity)

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

 

 

OK, I didn't realize that MathCAD has a built in "or" function of the same name. Reading the help on this, it appears to be made for working with images. For my purposes, I use or() to check a vector of logical statements, so the built-in MathCAD function won't work for what I do. The sum of logical statements being greater than 1 will work and is more rigorous, since there may be users that will use optima alongside the native or() for images.

 

What if ORIGIN is not 0? I don't know what this implies. 

LucMeekes
23-Emerald III
(To:KevinFinity)

You could call your function OR() instead of or() to keep them side-by-side...

On ORIGIN:

Type ORIGIN:=1 at the very (very very) top of your sheet, or set it to 1 using the menu (Ribbon: 'Calculation' tab) and see what happens when you call your or() function.

ORIGIN is a built-in variable that sets the first index value of arrays (Vectors and Matrices). You hard-code your indexing to start at 0, but if users have set it to 1 (the most common other value) attempting to index 0 will cause an error. The safe way is to index vector V from ORIGIN .. last(V).

 

Success!
Luc

Werner_E
25-Diamond I
(To:KevinFinity)

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
Werner_E
25-Diamond I
(To:Werner_E)

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.

 

 

Announcements

Top Tags