Finding a maximum value of a function on an interval?

2,107 views
Skip to first unread message

Johann "Myrkraverk" Oskarsson

unread,
Nov 26, 2009, 8:49:04 PM11/26/09
to sage-s...@googlegroups.com
Hello all,

Given a function f(x), what is the best way to find its maximum value
on an interval [a,b]? Do I have to differentiate and do it manually?


Johann

Marshall Hampton

unread,
Nov 26, 2009, 9:25:53 PM11/26/09
to sage-support
Perhaps this is the kind of thing you want?

sage: var('x')
sage: f = -x^4 + 9*x^3 - 23*x^2 + 31*x - 15
sage: f.find_maximum_on_interval(0,6)

(69.216599407272753, 4.6263272799362944)

If you do

sage: f.find_maximum_on_interval?

you can get the documentation for that. For symbolic answers you
could do

sage: solve(diff(f,x)==0,x)

but of course that is more limited in what it can answer.

-Marshall Hampton

On Nov 26, 7:49 pm, "Johann \"Myrkraverk\" Oskarsson"

Laurent

unread,
Nov 27, 2009, 3:52:42 AM11/27/09
to sage-s...@googlegroups.com
Marshall Hamilton ha scritto:
> Perhaps this is the kind of thing you want?
>
> sage: var('x')
> sage: f = -x^4 + 9*x^3 - 23*x^2 + 31*x - 15
> sage: f.find_maximum_on_interval(0,6)
>
>
Is it equivalent to what we get with
plot(f,(0,6)).get_minmax_data()
?


> you can get the documentation for that. For symbolic answers you
> could do
>
> sage: solve(diff(f,x)==0,x)
>
> but of course that is more limited in what it can answer.
>
In particular, it does not solve correctly trigonometric equations : it
finds only one solution and looses if a higher local maximum exists at
another place.


Laurent

Marshall Hampton

unread,
Nov 27, 2009, 10:19:19 AM11/27/09
to sage-support


On Nov 27, 2:52 am, Laurent <moky.m...@gmail.com> wrote:
> Marshall Hamilton ha scritto:> Perhaps this is the kind of thing you want?
>
> > sage: var('x')
> > sage: f = -x^4 + 9*x^3 - 23*x^2 + 31*x - 15
> > sage: f.find_maximum_on_interval(0,6)
>
> Is it equivalent to what we get with
> plot(f,(0,6)).get_minmax_data()
> ?

> Laurent

No, they are very different. I think get_minmax_data is just finding
the max and min of the plotted points of the function, so the values
will depend on the plot_points parameter for example. The
find_maximum_on_interval function uses scipy's fminbound function.

-Marshall

Laurent

unread,
Nov 27, 2009, 10:44:36 AM11/27/09
to sage-s...@googlegroups.com

> No, they are very different. I think get_minmax_data is just finding
> the max and min of the plotted points of the function, so the values
> will depend on the plot_points parameter for example. The
> find_maximum_on_interval function uses scipy's fminbound function.
>
OK, so I deduce that find_maximum_on_interval is more precise.
thanks for the information !

Laurent
Reply all
Reply to author
Forward
0 new messages