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

Solve and the order of variables to solve for (v7.0)

0 views
Skip to first unread message

kristoph

unread,
Nov 19, 2009, 5:36:51 AM11/19/09
to
Hi,

consider the following problem:

costsHome[yh_, yht_] := w (yh^2 + 10/100 yh yht + 110/100 yht^2);
costsFor[yf_, yft_] := wt (110/100 yf^2 + 10/100 yf yft + yft^2);

profitHome[\[Epsilon]_, pht_, ph_] := \[Epsilon] pht yht + ph yh -
costsHome[yh, yht];
profitFor[\[Epsilon]_, pf_, pft_] :=
1/\[Epsilon] pf yf + pft yft - costsFor[yf, yft];

res = Solve[{D[profitHome[\[Epsilon], pht, ph], yh] == 0,
D[profitHome[\[Epsilon], pht, ph], yht] == 0,
D[profitFor[\[Epsilon], pf, pft], yf] == 0,
D[profitFor[\[Epsilon], pf, pft], yft] == 0}, {yh, yht, yf, yft}]

The list of results I get is sorted not in the way I would like to
have it, i.e. yh, yht, yf and yft.

How do I always the the list to be the way I want it?

The order even changes if I let

costsHome[yh_, yht_] := w (yh^2 + yht^2);
costsFor[yf_, yft_] := wt (yf^2 + yft^2);

Does anybody know why? Thanks in advance.

Andrzej Kozlowski

unread,
Nov 19, 2009, 7:25:31 AM11/19/09
to
The simplest way is

res = Solve[{D[profitHome[\[Epsilon], pht, ph], yh] == 0,
D[profitHome[\[Epsilon], pht, ph], yht] == 0,
D[profitFor[\[Epsilon], pf, pft], yf] == 0,

D[profitFor[\[Epsilon], pf, pft], yft] == 0}, {yh, yht, yf, yft},
Sort -> False]

but it may have a cost. Solve chooses an ordering of the variables that
it believes will be the most efficient and the answer is returned in
this particular order. Probably Solve is going to be more often right
than wrong so setting the Sort option to False may give you poorer
performance (but it may also not).

The alternative is to let Solve choose its own ordering and then sort
the answers to put them in the order you want them to be. It's easy
enough to program this but I don't think it is worth bothering about
unless you find that the simple solution above seriously damages
performance.

Andrzej Kozlowski

Bob Hanlon

unread,
Nov 20, 2009, 6:49:28 AM11/20/09
to

$Version

7.0 for Mac OS X x86 (64-bit) (February 19, 2009)

costsHome[yh_, yht_] :=
w (yh^2 + 10/100 yh yht + 110/100 yht^2);
costsFor[yf_, yft_] :=
wt (110/100 yf^2 + 10/100 yf yft + yft^2);

profitHome[\[Epsilon]_, pht_, ph_] :=
\[Epsilon] pht yht + ph yh -
costsHome[yh, yht];
profitFor[\[Epsilon]_, pf_, pft_] :=

1/\[Epsilon] pf yf + pft yft - costsFor[yf, yft];

res = Solve[{
D[profitHome[\[Epsilon], pht, ph], yh] == 0,
D[profitHome[\[Epsilon], pht, ph], yht] == 0,
D[profitFor[\[Epsilon], pf, pft], yf] == 0,
D[profitFor[\[Epsilon], pf, pft], yft] == 0},

{yh, yht, yf, yft}][[1]];

First /@ res

{yh,yht,yf,yft}

To force a specified order

desiredOrder = {yf, yh, yft, yht};

res2 = SortBy[res,
Position[desiredOrder, #[[1]]][[1, 1]] &];

desiredOrder == First /@ res2

True

Or slightly more compactly

res3 = SortBy[res,
Position[desiredOrder, #[[1]]] &];

desiredOrder == First /@ res3

True


Bob Hanlon

---- kristoph <kristop...@web.de> wrote:

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

0 new messages