Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to find the Max of a Piecewise function

194 views
Skip to first unread message

Aaron Fude

unread,
Jul 8, 2008, 7:49:21 AM7/8/08
to
Hi,

If f is deined according to

f[x_] := Piecewise[{{1, x < 1}, {2, x > 1}}]

then

Max[f[Range[0, 1, .001]]]

does not yield the desired result. What's the proper way to approach
this?

Thanks!

Aaron

David Park

unread,
Jul 9, 2008, 4:55:12 AM7/9/08
to
Why do you write a doubly nested expression, not obtain the result you
expect, and then fail to look at what the intermediate results are?

Either give f the Listable Attribute, or Map f onto the list generated by
Range. As defined by you f does not work with a List as an argument

--
David Park
djm...@comcast.net
http://home.comcast.net/~djmpark/


"Aaron Fude" <aaro...@gmail.com> wrote in message
news:g4vk81$9pv$1...@smc.vnet.net...

W_Craig Carter

unread,
Jul 9, 2008, 4:55:45 AM7/9/08
to
Hello Aaron,
The problem is here: f[Range[0,1,.001]

Compare:
f[Range[0,2,.1]]
Map[f[#] &, Range[0, 2, .1]]
Map[f, Range[0, 2, .1]]
f /@ Range[0, 2, .1]

WCC

On Tue, Jul 8, 2008 at 7:48 AM, Aaron Fude <aaro...@gmail.com> wrote:

> f[x_] := Piecewise[{{1, x < 1}, {2, x > 1}}]
>
> then
>
> Max[f[Range[0, 1, .001]]]
>
> does not yield the desired result. What's the proper way to approach
> this?
>


--
W. Craig Carter

Szabolcs Horvát

unread,
Jul 9, 2008, 4:55:57 AM7/9/08
to

f is not Listable. One way to achieve the behaviour you expect is to
set this attribute on it:

SetAttributes[f, Listable]

But it may be simpler and better to just use Max[f /@ Range[0,1,.001]]

DrMajorBob

unread,
Jul 9, 2008, 4:56:41 AM7/9/08
to
"Max[f[Range[0, 1, .001]]]

does not yield the desired result."

You haven't told us what the desired result IS, but I'm sure you're right.

Here's one possibility:

f[x_] = Piecewise[{{1, x < 1}, {2, x > 1}}];
Max[f /@ Range[0, 1, .001]]

1

Note that we have

f[1]

0

since 1 is neither < 1 nor > 1.

Bobby

On Tue, 08 Jul 2008 06:48:46 -0500, Aaron Fude <aaro...@gmail.com> wrote:

> Hi,
>
> If f is deined according to
>
> f[x_] := Piecewise[{{1, x < 1}, {2, x > 1}}]
>
> then
>
> Max[f[Range[0, 1, .001]]]
>
> does not yield the desired result. What's the proper way to approach
> this?
>

> Thanks!
>
> Aaron
>
>

--
DrMaj...@longhorns.com

Bob Hanlon

unread,
Jul 9, 2008, 4:57:16 AM7/9/08
to
SetAttributes[f, Listable];

f[x_] := Piecewise[{{1, x < 1}, {2, x > 1}}]

Max[f[Range[0, 1, .001]]]

1


Bob Hanlon

---- Aaron Fude <aaro...@gmail.com> wrote:

=============

Bill Rowe

unread,
Jul 9, 2008, 4:57:38 AM7/9/08
to
On 7/8/08 at 7:48 AM, aaro...@gmail.com (Aaron Fude) wrote:

>If f is deined according to

>f[x_] := Piecewise[{{1, x < 1}, {2, x > 1}}]

>then

>Max[f[Range[0, 1, .001]]]

>does not yield the desired result. What's the proper way to approach
>this?

To find maxima of functions use Maximize or NMaximize

For example,

Maximize[{f[x], 0 < x < 1}, x]

gives:

{1,{x->1/2}}

The first value is the function maximum and the second is the
value of x where that maximum occurs. In your particular
example, the point at which the maximum occurs is not well
defined since you have defined the function to have a constant
value for 0 < x < 1

Jens-Peer Kuska

unread,
Jul 9, 2008, 4:57:50 AM7/9/08
to
Hi,

f[x_?NumericQ] := Piecewise[{{1, x < 1}, {2, x > 1}}]
f[x_List] := f /@ x

or the attribute Listable for f[] may help.

Regards
Jens

Jean-Marc Gulliet

unread,
Jul 9, 2008, 4:58:57 AM7/9/08
to
Aaron Fude wrote:

SetAttributes[f, Listable]

Max[f[Range[0, 1, .001]]]

1

Regards,
-- Jean-Marc

Francisco Gutierrez

unread,
Jul 10, 2008, 6:32:40 AM7/10/08
to
Friends:
What are the best references you know for programming genetic algorithms in Mathematica?
Thanks
Francisco

--- On Wed, 7/9/08, Jean-Marc Gulliet <jeanmarc...@gmail.com> wrote:

Jean-Marc Gulliet

unread,
Jul 10, 2008, 6:33:36 AM7/10/08
to
On Wed, Jul 9, 2008 at 3:10 PM, Francisco Gutierrez
<fgutie...@yahoo.com> wrote:

> What are the best references you know for programming genetic algorithms in
> Mathematica?

Excellent resources I am aware of, at the introductory level, are:

[1] Christian Jacob, *Illustrating Evolutionary Computation with
Mathematica*, _The Morgan Kaufmann Series in Artificial Intelligence_,
hardcover 578 pages, Morgan Kaufmann; 1st edition (February 1, 2001)
ISBN-13: 978-1558606371

[2] The companion website for Christian Jacob's book, *Illustrating
Evolutionary Computation with Mathematica*, website that contains
numerous Mathematica notebooks.
http://pages.cpsc.ucalgary.ca/~jacob/IEC/IEC%20Main%20Page/IEC%20Main%20Page.htm

[3] Claudio Mattiussi, *Bio-inspired Adaptive Machines: Genetic
algorithms for function optimization*, Laboratory Material,

"In this laboratory you will experiment with various illustrative and
real-world applications of Genetic Algorithms (GA). The first three
examples are implemented as Mathematica notebooks.You will find a
detailed description of these exercises in the pdf files linked below.
Some basics on the Mathematica interface are given in the pdf file
describing the first exercise." (From Claudio Mattiussi's website.)

http://lissrv1.epfl.ch/~claudio/education/BAM/GAs.php

Regards,
--
Jean-Marc

0 new messages