Ask Your Question
1

how to find a local maximum?

asked 2012-04-15 08:37:12 -0500

Nil gravatar image

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 flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
3

answered 2012-04-15 09:12:29 -0500

achrzesz gravatar image

find_maximum_on_interval?

edit flag offensive delete link 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

Nil gravatar imageNil ( 2012-04-15 09:57:04 -0500 )edit
1

answered 2012-04-15 10:19:02 -0500

achrzesz gravatar image
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)
edit flag offensive delete link more

Comments

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

Nil gravatar imageNil ( 2012-04-15 11:16:53 -0500 )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

achrzesz gravatar imageachrzesz ( 2012-04-15 19:46:33 -0500 )edit

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

Nil gravatar imageNil ( 2012-04-16 10:53:40 -0500 )edit
1

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

achrzesz gravatar imageachrzesz ( 2012-04-16 11:45:07 -0500 )edit

In Sage 7.3, the command is now find_local_maximum.

srobbert gravatar imagesrobbert ( 2016-10-05 13:45:25 -0500 )edit
0

answered 2020-01-29 09:04:14 -0500

Dan-K gravatar image

updated 2020-01-30 16:25:31 -0500

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.

edit flag offensive delete link more

Comments

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

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2020-01-30 04:48:11 -0500 )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 []

Dan-K gravatar imageDan-K ( 2020-01-30 16:24:15 -0500 )edit

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2012-04-15 08:37:12 -0500

Seen: 5,013 times

Last updated: Jan 30