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

Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X

How to find Global Maximum, Global Minimum, Local Maximum and Local Minimum of a function?

Cornel
19-Tanzanite

How to find Global Maximum, Global Minimum, Local Maximum and Local Minimum of a function?

Hello,

Considering the following function:

CornelBejan_1-1683700759179.png

CornelBejan_2-1683700770965.png

How to find: 

  • Local Maximum: x1 and f(x1)
  • Local Minimum: x2 and f(x2)
  • Global Maximum: x3 and f(x3)
  • Global Minimum: x4 and f(x4)

on the interval of x:

CornelBejan_7-1683702113827.png

CornelBejan_6-1683702095867.png

 

 

Mathcad Prime 8 file attached.

 

Thank you.

 

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:Cornel)

You could do as Luc suggested, but the symbolic engine (or its interface) is not capable enough to give you all needed solutions. "assume" and "fully" are ignored and just one solution is returned.

Werner_E_2-1683718759980.png

It should work that way, but only does for simpler functions where Primes symbolic has not to switch to its float mode.

EDIT: It works for simpler functions, but we should omit "fully"!

Werner_E_3-1683719354965.png

 

 

So in your case you may resort to numeric methods like the "root" function:

Werner_E_0-1683718076314.png

You may also try a utility function like the one below, but it sure won't work for all type of functions.

Werner_E_1-1683718598441.png

 

To distinguish between maxima and minima, the second derivative can be used:

Werner_E_0-1683718397462.png

Attached file is in P9 format

 

View solution in original post

21 REPLIES 21
LucMeekes
23-Emerald III
(To:Cornel)

Take the derivative of f to x, f'(x). Solve f'(x)=0. That should give you (at least) 4 roots. Calculate f(x) for each of these roots and determine from their values which one is global or local, max or min.

 

Success!

Luc

Cornel
19-Tanzanite
(To:LucMeekes)

CornelBejan_2-1683716557498.png
CornelBejan_0-1683716427323.png

So, only one root the derivative of f(x) wants to give to us (at least in Mathcad Prime (8)).

CornelBejan_3-1683716710903.png

CornelBejan_4-1683716762571.png

 

 

 

ttokoro
20-Turquoise
(To:Cornel)

Using digital sampling data.

image.png

Werner_E
25-Diamond I
(To:Cornel)

You could do as Luc suggested, but the symbolic engine (or its interface) is not capable enough to give you all needed solutions. "assume" and "fully" are ignored and just one solution is returned.

Werner_E_2-1683718759980.png

It should work that way, but only does for simpler functions where Primes symbolic has not to switch to its float mode.

EDIT: It works for simpler functions, but we should omit "fully"!

Werner_E_3-1683719354965.png

 

 

So in your case you may resort to numeric methods like the "root" function:

Werner_E_0-1683718076314.png

You may also try a utility function like the one below, but it sure won't work for all type of functions.

Werner_E_1-1683718598441.png

 

To distinguish between maxima and minima, the second derivative can be used:

Werner_E_0-1683718397462.png

Attached file is in P9 format

 

Cornel
19-Tanzanite
(To:Werner_E)

I thought about root, but in the root it must be specified an initial guess of each individual root. And this it is something like that per guess. Now, of course, with the graph in front of our eyes the things are much easier to guess each initial guess root. I do not like pretty much this variant with guessing the solutions...
I know that in Mathcad15 there is a function by which the solution is found by giving an interval. Is it possible something like that also in Mathcad Prime?

Werner_E
25-Diamond I
(To:Cornel)


@Cornel wrote:

I thought about root, but in the root it must be specified an initial guess of each individual root. And this it is something like that per guess. Now, of course, with the graph in front of our eyes the things are much easier to guess each initial guess root. I do not like pretty much this variant with guessing the solutions...


That exactly was the reason why I quickly wrote the utility function "Extrema()". Look it up in my answer above. You specify the function and the interval and it returns the extrema. Its a function which sure would require some additional work to deal with the situation when one of the x-values used by chance IS exactly an extreme position or to deal with discontinuities, etc. But the function provided could be a starting point for your own attempts.

 


I know that in Mathcad15 there is a function by which the solution is found by giving an interval.

I don't know what you are talking about!? Are you talking of a built-in function? Which one?

Maybe you confuse it with "localmin()" and "localmax()". These functions are still available in Prime, too, but they don't work for functions but rather only for sampled data vectors, as ttokoro already has shown, and they also have their own numeric limitations.

I know that in the past questions about finding all zeros of a function in a given interval came up here in the forum quite often and me an others may have provided some kind of utility functions, don't remember. They sure all had their limits and drawbacks, though. You may search the forum, maybe you can find something you like.

Cornel
19-Tanzanite
(To:Werner_E)

In Mathcad 15: root(f(x)/function, x/variable, a,b), a and b are the range of the interval (like a=-6 and b=7 in the above example). Isn't it like that?

And can you also attach your variant in Mathcad Prime 8 file?

Werner_E
25-Diamond I
(To:Cornel)


@Cornel wrote:

In Mathcad 15: root(f(x)/function, x/variable, a,b), a and b are the range of the interval (like a=-6 and b=7 in the above example). Isn't it like that?


???

Look again to what I have posted! Thats exactly the way I had used the "root" function. It works the same in Prime as in real Mathcad. "root" will always give you just one return value, it never returned all roots in a given interval, but in can be used in a program which tries to collect all roots, like I did in the "Extrema()" function.

 


And can you also attach your variant in Mathcad Prime 8 file?

No, sorry. I can't back convert Prime 9 files. Thank you, PTC.

Thats the reason for all the pictures. Shouldn't be that difficult to retype.

Cornel
19-Tanzanite
(To:Werner_E)


@Werner_E wrote:

@Cornel wrote:

In Mathcad 15: root(f(x)/function, x/variable, a,b), a and b are the range of the interval (like a=-6 and b=7 in the above example). Isn't it like that?


???

Look again to what I have posted! Thats exactly the way I had used the "root" function. It works the same in Prime as in real Mathcad. "root" will always give you just one return value, it never returned all roots in a given interval, but in can be used in a program which tries to collect all roots, like I did in the "Extrema()" function.

 



Ok. Now I understand what you are trying to say to me. Thank you. 

 

 

Werner_E
25-Diamond I
(To:Cornel)

I remember that in an older thread I provided a function similar to the aforementioned "Extrema" which found all zeros in a given interval and used the "root" function not by providing a range, but rather by providing a guess value. I divided the interval in many parts (similar to what i did in "Extrema") and used all values as guesses to let root find a solution and collected all in a vector. Afterwards it was necessary to clean up and remove all the duplicate values which were found and also the values aoutside of the given interval. Maybe this was ls elegant but I guess it was a more robust approach. Could not find the thread now, though.

Cornel
19-Tanzanite
(To:Cornel)

And these are the manual calculations (only for (at least my) understanding):

CornelBejan_0-1683724177209.png

 

CornelBejan_1-1683724205847.png

CornelBejan_2-1683724215942.png

 

What does trim and match (and also NaN) functions do? What are their purposes? When do we must use trim, match and NaN functions?

Werner_E
25-Diamond I
(To:Cornel)


@Cornel wrote:

And these are the manual calculations (only for understanding):

Yes, that method was used by me in the first approach shown. I just put the interval limits in vectors and calculated all values in one go using vectorization.

 


What does trim and match (and also NaN) functions do? What are their purposes?

When do we must use trim, match and NaN functions?


Whats wrong with looking them up in the help?

The get the maxima I used  f2(X)<0 to create a vector with 1 at the position where the second derivative at X is negative and 0 otherwise. Then I used "match" to find the indices of zeros and used "trim"  to delete the X-values at these positions. What then remains are the maxima.

Similar approach for the minima.

Cornel
19-Tanzanite
(To:Werner_E)


@Werner_E wrote:

What does trim and match (and also NaN) functions do? What are their purposes?

When do we must use trim, match and NaN functions?


Whats wrong with looking them up in the help?

The get the maxima I used  f2(X)<0 to create a vector with 1 at the position where the second derivative at X is negative and 0 otherwise. Then I used "match" to find the indices of zeros and used "trim"  to delete the X-values at these positions. What then remains are the maxima.

Similar approach for the minima.


Ok. Thank you.

ttokoro
20-Turquoise
(To:Cornel)

image.pngimage.png

Cornel
19-Tanzanite
(To:ttokoro)

Can you attache also the Mathcad Prime 8 file?

ttokoro
20-Turquoise
(To:Cornel)

Now I am using Prime 9. Copy and Paste can do from Prime 9 to 8, only one equation each time.
Plot and programming are also one item for Copy and Paste to lower version.

ttokoro
20-Turquoise
(To:ttokoro)

image.png

Cornel
19-Tanzanite
(To:ttokoro)

@Werner_E @LucMeekes 

 

How to find the maximum point: (x1, f(x1)) in the phase-frequency bode plot for G(s) function from below...?

Cornel_0-1685524445099.png

 

Cornel_1-1685524548925.png

 

Cornel_2-1685524577716.png

 

Cornel_3-1685524625647.png

 

Cornel_5-1685524807216.png

Cornel_0-1685525664387.png

Cornel_0-1685526319400.png

 

Mathcad Prime 8 file attached.

 

 

Werner_E
25-Diamond I
(To:Cornel)

How about just to use the derivative of the very function you would like to see the max of?

Werner_E_2-1685535938948.png

 

You may also use the "maximize" function to do the job, but I guess that its necessary to define an auxiliary function

Werner_E_1-1685535610599.png

I used ff as free variable so to not interfere with the vector f you defined.

 

Cornel
19-Tanzanite
(To:Werner_E)

Thank you very much, Werner_E and LucMeekes.

LucMeekes
23-Emerald III
(To:Cornel)

LucMeekes_0-1685536317481.png

LucMeekes_1-1685536325214.png

LucMeekes_2-1685536342084.png

LucMeekes_3-1685536364131.png

LucMeekes_4-1685536382977.png

Success!
Luc

 

 

 

Announcements

Top Tags