Skip to main content
12-Amethyst
April 8, 2022
Solved

Find minima and maxima of a function

  • April 8, 2022
  • 2 replies
  • 4310 views

Hello all,

 

as a simple example I use a sinus function where I want to find the absolute minimum and absolute maximum.

 

If the function will be more complicated then the local extrema are not interesting currently. I am looking for the absolute values.

 

To be honest I donΒ΄t want to make a derivation d/dt and find the corresponding times via solve block.

 

I would like to use some kind of integrated function.

 

Unfortunately "max(vector)" and "min(vector)" does not work. My function f(txx) seems to be not an array ... Hm.

 

Also I have found in the F1 help menu a function for local minima and local maxima. I understand that it compares the array elements but it also doesnt help.

 

Can you help please? My sheet is attached. Also here the screenshot:

 

xyz123_0-1649418411214.png

 

Best answer by StuartBruff

There are several approaches to solving this problem - here's one.

 

StuartBruff_0-1649425824618.png

 

Note that k is a range variable - which is most assuredly not a vector.  k is chosen to be an integer range variable so that it can be used as a vector index.   t is a vector.  The vectorized operator is used to ensure that Function is applied to each element of t, and the result is assigned to variable v.   

 

max and min then return the values you are looking for - unless a Jedi Mind Trick has meant that the values of t don't exactly coincide with the true values that give the max & min of Function.   The choice of interval below should give near enough the true min & max:

 

2022 04 08 b.png

 

 

Stuart

2 replies

23-Emerald V
April 8, 2022

There are several approaches to solving this problem - here's one.

 

StuartBruff_0-1649425824618.png

 

Note that k is a range variable - which is most assuredly not a vector.  k is chosen to be an integer range variable so that it can be used as a vector index.   t is a vector.  The vectorized operator is used to ensure that Function is applied to each element of t, and the result is assigned to variable v.   

 

max and min then return the values you are looking for - unless a Jedi Mind Trick has meant that the values of t don't exactly coincide with the true values that give the max & min of Function.   The choice of interval below should give near enough the true min & max:

 

2022 04 08 b.png

 

 

Stuart

xyz12312-AmethystAuthor
12-Amethyst
April 8, 2022

Yes!

 

My time was not a vector, it was a range variable. With your trick itΒ΄s all fine πŸ™‚

 

Thanks!

23-Emerald V
April 8, 2022

No worries.

 

There are other ways of dealing with range variables.   Here are a few program versions:

 

2022 04 08 c.png

 

I find the function vec to be quite useful in many circumstances (although I tend to use a slightly more complicated, recursive version that completely flattens even deeply nested arrays).

 

Here's vec being used to directly convert the range variable tx as Function's argument.

 

2022 04 08 d.png

 

Stuart

 

25-Diamond I
April 8, 2022

Using the derivative might seem like the most accurate way to solve the task but you may run in multiple problems. With simpler function you may be able to use the symbolics to find all extremas in a given range and then chose the absolute ones. But with just slightly more complex functions the symbolics might be overwhelmed. And when you switch to numeric evaluation via "root" or  a solve block, you get only one solution (depending on a guess) which is not much helpful.
So using the brute force way by creating a vector of equidistant x-values and calculating the corresponding y-values and then use "min" and "max" seems to be the most appropriate. A disadvantage might be that too few values in the vector will give an inaccurate result (especially for high frequencies and an larger range) while creating a very large vector might eat up too much resources (time, memory, ...).

One possible approach could be to follow the step with "minimize" and "maximize" (these functions usually only find local extremas) with the just found values as guess values.
The following example shows what i am talking about:

Werner_E_0-1649451066582.png

Prime6 sheet attached

 

12-Amethyst
May 8, 2023

Helpful solution @Werner_E . I just used this hybrid approach for finding global max. values and it worked great!