Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X
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:
Solved! Go to Solution.
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:
I guess you didn't need the help yourself...
Success!
Luc
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:
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.
Suggestion: change the test for help to:
{ The or( ) function requires two arguments, matrices each that must be of same size. }
After that is solved, I get:
and
but:
and
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.
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:
and in this case you want to issue an error, so I would
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.
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
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:
Here is an alternative. Its done in Mathcad 15
You may add an argument which determines how often the "refinement" you introduced should be applied.
Often you won't see much improvement by these refinements, sometimes it helps increasing accuracy
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.