List Archives > 
Maple User Group List Archive > 
Archive by date > 
This Month By Date > 
This Month By Topic
[MUG] Re: minimizing functions - question
| [MUG] Re: minimizing functions - question |
|
Author: Maple User Group
Posted: Tue, 11 Jun 2002 17:31:40 -0400
|
>> From: Maple User Group "maple_gr"
| >> From: Alexander Serebrenik "Alexander.Serebrenik"
| I try to find the minimal value of an interval-defined function as
| following:
|
| > h := x -> if x < 5 then t-x^2 else x+t end if ;
| h := proc(x)
| option operator, arrow;
| if x < 5 then t - x^2 else x + t end if
| end proc
|
| > minimize(h(x,t), x = 0..7, t=10..14);
| Error, (in h) cannot evaluate boolean: x < 5
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
Date: Wed, 5 Jun 2002 14:13:40 -0700 (PDT)
From: Robert Israel "israel"
To: "maple-list"
Subject: minimizing functions - question
There are several problems here.
The error message is caused by premature evaluation: when you say
h(x,t) you are trying to evaluate the function h with the symbolic
argument x, and this can't be done because Maple can't determine
which branch to take in the "if" statement. One way around this
would be to use the "piecewise" function.
A second problem is that you defined h with one argument and called
it with two. The second argument, t, will simply be ignored. In
this case that doesn't cause problems because the result will be the
same with h(x,t) as it would be with h(x).
You could say
> h:= (x,t) -> piecewise(x<5, t-x^2, x+t);
minimize(h(x,t), x= 0 .. 7, t = 10 .. 14);
In Maple 6 or 7 this produces an error message (the result of a bug):
Error, (in solve/addident) wrong number (or type) of parameters in
function map
Maple 8 (surprisingly enough) comes up with an answer, which I presume
is the one you wanted:
-15
The final problem is that this isn't really correct either: the function
has an infimum but not a minimum on the rectangle 0 <= x <= 7,
10 <= t <= 14, since h(x,10) -> -15 as x -> 5-, but h(5,10) = +15.
Robert Israel "israel"
Department of Mathematics http://www.math.ubc.ca/~israel
University of British Columbia
Vancouver, BC, Canada V6T 1Z2
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
From: Alec Mihailovs "amihailo"
To: "maple-list"
Subject: minimizing functions - question
Date: Wed, 5 Jun 2002 21:43:25 -0400
> h := piecewise(x < 5,t-x^2,x+t):
> minimize(h, x=0..7, t=10..14);
-15
Best wishes,
Alec Mihailovs
http://webpages.shepherd.edu/amihailo
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
From: "Bill Page" "bill.page1"
To: "maple-list"
Subject: minimizing functions - question
Date: Wed, 5 Jun 2002 22:19:54 -0400
Alexander,
The first argument to minimize is supposed to be an
"algebraic" expression. In your example Maple evaluates
h(x,t) prematurely, even before attempting the minimization
and fails right away. The same thing happens if you just
write
> h(x,t);
But Maple 8 understands the function 'piecewise' as a kind
of algebraic expression. If h is defined as follows:
> h:=(x,t) -> piecewsie(x < 5,t-x^2,x+t);
then h(x,t) evaluates as the expression piecewsie(x < 5,t-x^2,x+t)
which is processed properly by minimize.
> minimize(h(x,t), x = 0..7, t=10..14);
But note: Maple 7 fails on this and Maple 5 gets the wrong result
with a similar command
> minimize(h(x,t), {x,t}, {x = 0..7, t=10..14});
(Maple 5 used a different syntax for minimize.)
Cheers,
Bill Page.
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
From: Stanley J Houghton "S.J.Houghton"
Date: Fri, 7 Jun 2002 15:52:21 +0100
To: "Alexander.Serebrenik" "maple-list"
Subject: minimizing functions - question
It is a case of premature evaluation. Since the parameters of
functions such as minimize or plot3d are evaluated, you have to write
your function to return an unevaluated result if x and t are not
constants.
> h :=
> (x,t) -> if not (type(x,'constant') and type(t,'constant')) then
> 'procname'(x,t) # return unevaluated
> elif x < 5 then t-x^2
> else x+t end if ;
> minimize(h(x,t), x = 0..4.9999995, t=10..14);
This was not much help though, since, although I could plot3d the
function h(x,t), the minimise call returned unevaluated after
"thinking" for a while.
I will leave it for others to comment on this aspect.
I would add that, in the new release 8, you can simplify this form by
using piecewise as below
> h := (x,t) -> piecewise( x < 5 , t-x^2 , x+t ) ;
> minimize(h(x,t), x = 0..7, t=10..14,location);
This worked and returned the correct answer (but, I repeat, only in
version 8).
Regards
Stan
On Tue, 4 Jun 2002 11:11:16 +0200
(MEST) Alexander Serebrenik
"Alexander.Serebrenik" wrote:
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
Date: Tue, 11 Jun 2002 13:20:33 +0200
From: PierLuigi Zezza "pierluigi.zezza"
Subject: minimizing functions - question
To: "maple-list"
Hi, you have two different problems:<br><br>
1. In the definition your function you must include the possibility of x
being a formal parameter
>h := proc(x): if
type(x,numeric) then if x < 5 then t-x^2 else x+t end if else 'h(x)'
end if end;
2. To my knowledge minimize work for univariate functions<br><br>
> minimize(h(1,t), t=10..14);
9
gigi zezza
PierLuigi Zezza
"pzezza"
|
[View Complete Thread]
Previous by date: [MUG] Export to html in Maple 8 -- problem, Carl Eberhart
Next by date: [MUG] Re: Assignment, Maple User Group
Previous thread: [MUG] Export to html in Maple 8 -- problem, Carl Eberhart
Next thread: [MUG] Assignment, Edgar G Goodaire
|