# Automatically assign solutions to equation as constants

Consider the following code:

ClearAll[K, L, λ, q, r, w]

TC[K_, L_, q] := w*L + r*K + 10*q
Phi[K_, L_, λ_] := TC[K, L, q] - λ*(10*K^(4/5)*(L - 40)^(1/5) - q)
Phi[K, L, λ]

FullSimplify[{
D[Phi[K, L, λ], K],
D[Phi[K, L, λ], L],
D[Phi[K, L, λ], λ]
}]

FullSimplify[
Solve[{
D[Phi[K, L, λ], K] == 0,
D[Phi[K, L, λ], L] == 0,
D[Phi[K, L, λ], λ] == 0
}, {K, L, λ}]
]

r = 64;
w = 32;
FullSimplify[TC[K, L, q]]


Is it possible to automatically assign the real-valued solutions for K and L to get the output

2 (640 + (5 + 4 2^(1/5)) q)


with executing the last command?

Update

After hints from Artes, I've managed to always get the function TC for real-valued K and L. However, how do I print only the real-values [which I forgot to mention the first time I updated the question] of K and L when I solve the system of equations? When executing

F[k_, l_] := 10*k^(4/5)*(l - 40)^(1/5)
TC[k_, l_, q_] := w*l + r*k + 10*q
Phi[k_, l_, λ_] := TC[k, l, q] - λ*(F[k, l] - q)
expressions = {D[Phi[k, l, λ], k], D[Phi[k, l, λ], l], D[Phi[k, l, λ, λ]};
FullSimplify[Solve[expressions == {0, 0, 0}, {k, l, λ}]]


I get five solutions;

{{k -> (q w^(1/5))/(5 2^(3/5) r^(1/5)),
l -> 40 + (q r^(4/5))/(20 2^(3/5) w^(4/5)), λ] -> (
r^(4/5) w^(1/5))/(
4 2^(3/5))}, {k -> -(((-1)^(1/5) q w^(1/5))/(5 2^(3/5) r^(1/5))),
l -> 40 - ((-1)^(1/5) q r^(4/5))/(20 2^(3/5) w^(4/5)), λ ->
r^(4/5) w^(1/5) Root[-1 + 8192 #1^5 &, 2]}, {k -> ((-1)^(2/5) q w^(
1/5))/(5 2^(3/5) r^(1/5)),
l -> 1/40 (1600 + ((-2)^(2/5) q r^(4/5))/w^(
4/5)), λ -> ((-1)^(2/5) r^(4/5) w^(1/5))/(
4 2^(3/5))}, {k -> -(((-1)^(3/5) q w^(1/5))/(5 2^(3/5) r^(1/5))),
l -> 40 - ((-(1/2))^(3/5) q r^(4/5))/(
20 w^(4/5)), λ -> -(((-1)^(3/5) r^(4/5) w^(1/5))/(
4 2^(3/5)))}, {k -> ((-1)^(4/5) q w^(1/5))/(5 2^(3/5) r^(1/5)),
l -> 40 + ((-1)^(4/5) q r^(4/5))/(
20 2^(3/5) w^(4/5)), λ -> ((-1)^(4/5) r^(4/5) w^(1/5))/(
4 2^(3/5))}}


P.S. q, r, and w are all positive numbers.

-
Solve[FullSimplify[TC[K, L, q]] == 2 (640 + (5 + 4 2^(1/5)) q), {K, L}] –  belisarius Dec 27 '13 at 16:50
@belisarius I mean: If I execute FullSimplify[ Solve[{ D[Phi[K, L, λ], K] == 0, D[Phi[K, L, λ], L] == 0, D[Phi[K, L, λ], λ] == 0 }, {K, L, λ}] ], I get the solution K -> (q w^(1/5))/(5 2^(3/5) r^(1/5)), L -> 40 + (q r^(4/5))/(20 2^(3/5) w^(4/5)). Can I automatically assign these values to K and L and therefore get 2 (640 + (5 + 4 2^(1/5)) q) if I execute r = 64; w = 32; FullSimplify[TC[K, L, q] ]? –  Svend Tveskæg Dec 27 '13 at 17:28
Is this what you're after? mathematica.stackexchange.com/a/18706/193 –  belisarius Dec 27 '13 at 17:31
@belisarius Indeed! :) The only thing: Can I make it automatically choose the real-valued solution instead of, say, the first one? –  Svend Tveskæg Dec 27 '13 at 17:37
@belisarius I've found this but I can't get any of the methods to work. Can you help? :) –  Svend Tveskæg Dec 27 '13 at 18:42

Simply save the FullSimplify result (the one before the r = ... assignment), say as "res". Then use

FullSimplify[ TC[K,L,q]]/. res // FullSimplify//First


gives exactly the result you posted. Remove the first (and add a rule to filter only reals) to substitute all real solutions.

Per your update, this should return the $k$ and $l$ (along with any other solution components) where $k$ and $l$ are restricted to reals:

Pick[(res), Refine[ # ∈ Reals, Assumptions -> {q, w, r} ∈ Reals
&& 0 < q && 0 < w && 0 < r] & /@ ({k, l} /. res)]

-
On the top of the page you find a "help" link. There you can choose different help topics. One of them is how to format a post properly. –  halirutan Dec 28 '13 at 0:28
@halirutan - cheers, and thanks for editing! –  rasher Dec 28 '13 at 0:32
I made a huge omission in my first update to the question. See the updated question. –  Svend Tveskæg Dec 28 '13 at 3:07
@Svend - ah- I thought the question a bit simple. I've added to my original answer. Again, assuming you put the solve set into 'res', this should return the set where k and l are reals. –  rasher Dec 28 '13 at 6:26
Great. Thank you. –  Svend Tveskæg Dec 28 '13 at 6:33