Ask Your Question

# how to find a local maximum?

Hello. I'm fairly new to Sage, so lets see if someone more experienced can help!

Say I have a function f(x) continuous in [a,b] and derivable in (a,b). How can I implement in Sage a function 'maximum(f,a,b)' that returns the maximum of f(x) in [a,b] (as a numerical approximation, not as an expression)?

Thanks you a lot!

edit retag close merge delete

## 3 Answers

Sort by ยป oldest newest most voted

find_maximum_on_interval?

more

## Comments

However, this bit piece of code gives me a runtime error: f(x) = -4*x^6/(x^4 + 1)^(3/2) + 6*x^2/sqrt(x^4 + 1) find_maximum_on_interval(f,0,2) This is the error: RuntimeError: ECL says: THROW: The catch MACSYMA-QUIT is undefined. Thanks you

( 2012-04-15 16:57:04 +0200 )edit
sage: f = lambda x:-4*x^6/(x^4 + 1)^(3/2) + 6*x^2/sqrt(x^4 + 1)
sage: find_maximum_on_interval(f,0,2)
(2.8284271247461898, 1.0000000183339277)

more

## Comments

Can you explain why, or how, does your solution work? Thanks you

( 2012-04-15 18:16:53 +0200 )edit
1

Read the documentation: sage: find_maximum_on_interval?? sage: find_minimum_on_interval?? and you will see that this procedure gives you an access to scipy.optimize.fminbound which uses Brent's method

( 2012-04-16 02:46:33 +0200 )edit

More concretely, I was asking about the keyword lambda, because 'lambda?' yields no results.

( 2012-04-16 17:53:40 +0200 )edit
1

http://www.diveintopython.net/power_of_introspection/lambda_functions.html

( 2012-04-16 18:45:07 +0200 )edit

In Sage 7.3, the command is now find_local_maximum.

( 2016-10-05 20:45:25 +0200 )edit

Not sure when the change occurred, but in SageMath 9.0, find_maximum_on_interval has been replaced with find_local_maximum.

Bear in mind this is a numerical method.

more

## Comments

What would be wrong with f.diff(x).solve(x) (or diff(f(x),x).solve(x)) ?

( 2020-01-30 11:48:11 +0200 )edit

The thing that's wrong with what you propose @Emmanuel Charpentier is that it doesn't find the local maximum. For example take y = 2*x + 3; find_local_maximum(y, 1, 4) will return (10.999999837732908, 3.9999999188664543) while y.diff(x).solve(x) returns []

( 2020-01-30 23:24:15 +0200 )edit

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2012-04-15 15:37:12 +0200

Seen: 8,878 times

Last updated: Jan 30 '20