First time here? Check out the FAQ!

Ask Your Question
1

how to find a local maximum?

asked 12 years ago

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!

Preview: (hide)

3 Answers

Sort by » oldest newest most voted
3

answered 12 years ago

achrzesz gravatar image

find_maximum_on_interval?

Preview: (hide)
link

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 ( 12 years ago )
1

answered 12 years ago

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)
Preview: (hide)
link

Comments

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

Nil gravatar imageNil ( 12 years ago )
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 ( 12 years ago )

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

Nil gravatar imageNil ( 12 years ago )
1

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

achrzesz gravatar imageachrzesz ( 12 years ago )

In Sage 7.3, the command is now find_local_maximum.

srobbert gravatar imagesrobbert ( 8 years ago )
0

answered 5 years ago

Dan-K gravatar image

updated 5 years ago

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.

Preview: (hide)
link

Comments

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

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 5 years ago )

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 ( 5 years ago )

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: 12 years ago

Seen: 9,416 times

Last updated: Jan 30 '20