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

Find minima and maxima of a function

xyz123
11-Garnet

Find minima and maxima of a function

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
StuartBruff
23-Emerald II
(To:xyz123)

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

View solution in original post

4 REPLIES 4
StuartBruff
23-Emerald II
(To:xyz123)

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

Yes!

 

My time was not a vector, it was a range variable. With your trick it´s all fine 🙂

 

Thanks!

StuartBruff
23-Emerald II
(To:xyz123)

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

 

Werner_E
24-Ruby IV
(To:xyz123)

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

 

Announcements
Check out the latest
Mathcad Tip
"PTC Mathcad 15 / Prime 1-6 Update."