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

Symbolic use of numerical function FindRoot via ?NumericQ

109 views
Skip to first unread message

Michael Beqq

unread,
Jun 4, 2004, 4:55:27 AM6/4/04
to
I've use symbolical evalution of numerical functions all the time but still
have not mastered it. Now I got stuck on the following problem:

I have a function g[x1,x2,y1,y2]. I need to solve for x1,x2 and y1,y2 that
maximize g[x1,x2,y1,y2] in 2 steps,- in step 1 I need to find x1*=x1[y1,y2]
and x2*=x2[y1,y2]. Then I substitute solutions, *'s, into g[.] to get
g[y1,y2] and then solve for solve for y1* and y2*. This is a classical
2-stage problem in Economics.

Is there a way to do that in Mathematica 5 using FindRoot command. I tried
using SetDelayed and ?NumericQ options however get error messages that the
function g[.]'s is not a list of numbers with dimension {2} at {2 values}.

Here is more precise code:

g[x1_x2_,y1_,y2_]=g[x1,x2,y1,y2]"g is some function of 4 variables";
{x1[y1_,y2_?Numeric],x2[y1_,y2_?Numeric]}:=Evaluate[{x1,x2}/.FindRoot[Evalua
te[{D[g[x1,x2,y1,y2],x1]]==0,D[g[x1,x2,y1,y2],x2]]==0},{x1,x10},{x2,x20}]]
(* another website showed different code, i.e. x[(y1_,y2_)?Number] but I
assume it is just semantics*)
FindRoot[Evaluate[{D[g[y1,y2],y1]]==0,D[g[y1,y2],y2]]==0},{y1,y10},{y2,y20}]
]

where {x10,x20,y10,y20}=some numbers.

Thank you in advance.


Peter Pein

unread,
Jun 7, 2004, 5:49:47 AM6/7/04
to
"Michael Beqq" <mbek...@iastate.edu> schrieb im Newsbeitrag
news:c9pddv$nuf$1...@smc.vnet.net...
Hi Michael,
calling a function of 4 variables with 2 variables will not work. I've got
difficulties to understand, why you do not solve all the 4 derivatives in
one step? And since x1* and x2* depend on y1 and y2 you have to use the
chain rule, when building the derivatives w.r.t. y1 and y2, or did I get
sth. wrong?

--
Peter Pein, Berlin
to write to me, start the subject with [

Mukhtar Bekkali

unread,
Jun 10, 2004, 3:24:00 AM6/10/04
to
"Peter Pein" <pet...@arcor.de> wrote in message news:<ca1dnr$hp5$1...@smc.vnet.net>...


Here is an example that shows why I can't use one step to solve my
problem:

Support I have g1=(1-(x1-x2)/Q)x1 and g2=((x1-x2)/Q)x2, where Q=y1-y2;
all four variables belong to closed unit R+ interval. Now if you
take derivatives of g1 and g2 with respect to Q you would see that
dg1/dQ>0 and dg2/dQ<0. However, if you maximize g1 and g2 w.r.t. x1
and x2 respectively then you would find that x1*=(2/3)Q and
x2*=(1/3)Q. Substituting this into original objective functions
yields g1=(4/9)Q and g2=(1/9)Q, or both functions are not INCREASING
functions in Q, or dg2/dQ changes its sign from negative to positive.
This is due to corner solution.

I know that out of 4 variables one is likely to be corner, namely
y1=1, and it is y2,x1,x2 that I need to find in two stages.

Peter Pein

unread,
Jun 18, 2004, 2:33:19 AM6/18/04
to
"Mukhtar Bekkali" <mbek...@iastate.edu> schrieb im Newsbeitrag
news:ca92ag$ck$1...@smc.vnet.net...

> "Peter Pein" <pet...@arcor.de> wrote in message
news:<ca1dnr$hp5$1...@smc.vnet.net>...
> > Hi Michael,
> > calling a function of 4 variables with 2 variables will not work. I've
got
> > difficulties to understand, why you do not solve all the 4 derivatives
in
> > one step? And since x1* and x2* depend on y1 and y2 you have to use the
> > chain rule, when building the derivatives w.r.t. y1 and y2, or did I get
> > sth. wrong?
>
>
> Here is an example that shows why I can't use one step to solve my
> problem:
>
> Support I have g1=(1-(x1-x2)/Q)x1 and g2=((x1-x2)/Q)x2, where Q=y1-y2;
> all four variables belong to closed unit R+ interval. Now if you
> take derivatives of g1 and g2 with respect to Q you would see that
> dg1/dQ>0 and dg2/dQ<0. However, if you maximize g1 and g2 w.r.t. x1
> and x2 respectively then you would find that x1*=(2/3)Q and
> x2*=(1/3)Q. Substituting this into original objective functions
> yields g1=(4/9)Q and g2=(1/9)Q, or both functions are not INCREASING
> functions in Q, or dg2/dQ changes its sign from negative to positive.
> This is due to corner solution.
>
> I know that out of 4 variables one is likely to be corner, namely
> y1=1, and it is y2,x1,x2 that I need to find in two stages.
>

Dear Mukhtar,
sorry for the delayed response (I caught a cold) and for not understanding.

1. are the four vars out of the closed interval [0,1], or the half-open
(0,1] which is implied by R+?
2. why do we have suddenly two functions? (not mentioned in the original
posting (OP))
3. the constraints to the variables have not been mentioned in the OP.
4. I misunderstood your intention. I thought of finding the global maximum
of g.
5. independently of using the compact interval or not, I'm afraid
dg1/dQ=x1(x1-x2)/Q^2 can take any real value, because y1-y2 is element of
(-1,1) (or [-1,1]), which contains zero and x1-x2 can take values between -1
and 1.
6. I don't understand the half sentence concerning increasing functions (is
this a given condition? an assumption?) and why it would be so bad if dg2/dQ
would change its sign? Anyway.

I tried (for the question in the OP) the following code:

g[x1_, x2_, y1_, y2_] := (y1 - (1 + x1 - x2)^2/(1 + (y1 - y2))^2)*x1*y2 +
(y2 - (x1 - x2)/(y1 - y2))^2*x2*y1 (* just as example *)
y10 = 0.3; y20 = 0.7; x10 = 0.123; x20 = 0.567;
onestep[y10_, y20_] := Module[{x1star, x2star},
{x10, x20} = {x1star, x2star} =
{x1, x2} /. FindRoot[
{Derivative[1, 0, 0, 0][g][x1, x2, y10, y20] == 0,
Derivative[0, 1, 0, 0][g][x1, x2, y10, y20] == 0}, {x1, x10},
{x2, x20}];
Print[(ScientificForm[#1, {10, 4}] & ) /@
{x10, x20, y10, y20}];
{y1, y2} /.
FindRoot[{Derivative[0, 0, 1, 0][g][x1star, x2star, y1, y2] == 0,
Derivative[0, 0, 0, 1][g][x1star, x2star, y1, y2] == 0}, {y1, y10},
{y2, y20}]]
FixedPoint[onestep @@ #1 & , {y10, y20}, 30]

which should do what you want.

0 new messages