Ask Your Question
1

how to find a local maximum?

asked 2012-04-15 15:37:12 +0100

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 16:12:29 +0100

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 16:57:04 +0100 )edit
1

answered 2012-04-15 17:19:02 +0100

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 18:16:53 +0100 )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-16 02:46:33 +0100 )edit

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

Nil gravatar imageNil ( 2012-04-16 17:53:40 +0100 )edit
1

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

achrzesz gravatar imageachrzesz ( 2012-04-16 18:45:07 +0100 )edit

In Sage 7.3, the command is now find_local_maximum.

srobbert gravatar imagesrobbert ( 2016-10-05 20:45:25 +0100 )edit
0

answered 2020-01-29 16:04:14 +0100

Dan-K gravatar image

updated 2020-01-30 23:25:31 +0100

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 11:48:11 +0100 )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 23:24:15 +0100 )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 15:37:12 +0100

Seen: 9,272 times

Last updated: Jan 30 '20