# solve system including piecewise function

I've read posts about how to do this in a direct way, but the method doesn't work for my simple system? -- see code:

(*preliminary: no piecewise*)
Clear[p]
f[P_] := 54.758 * P ^ (-0.4) * 65 ^ 0.2
gnokink[P_] := 0.126191 * P ^ 1.1
Solve[f[p] == gnokink[p], p][[1]]

(* Out: {p->100.} *)

Now, piecewise g:

g[C_, P_] := Piecewise[{{gnokink[P], gnokink[P] < C}, {C, gnokink[P] > C}}]

Plot[{f[p], g[25, p]}, {p, 50, 150}, PlotLabel -> "System where C > kink (does not constrain)"]
(*plot deleted*)

Plot[{f[p], g[18, p]}, {p, 50, 150}, PlotLabel -> "System where C < kink (constrains)"]
(*plot deleted*)

Now let's try to solve "directly". I know that Solve[g[30, p] == f[p], p] fails:

Simplify[g[30, p] == f[p]]

(* Out: (\[Piecewise] 0.126191 p^1.1    1. p^1.1<237.735 30 1. p^1.1>237.735) == 126.192/p^0.4 *)

Evaluate[Simplify[g[30, p] == f[p]]]

(* Out: (\[Piecewise]   0.126191 p^1.1  1. p^1.1<237.735
30  1. p^1.1>237.735)==126.192/p^0.4 *)

But Solve hangs:

Solve[Evaluate[Simplify[g[30, p] == f[p]]], p]

Thanks!

• Thank you for including your code. Please also add some explanation regarding what you are trying to accomplish with it. – MarcoB Jun 3 '16 at 18:06
• Solve[g[30, p] == f[p], Reals] // Quiet or NSolve[g[30, p] == f[p], p] // Quiet? – Algohi Jun 3 '16 at 18:14
• Adding the "Reals" term makes the difference, thanks, and it seems to solve easily. I'll read up on why. (The code is just trying to find the intersection of the "constrained supply curve" and the "demand curve".) – radford_et_al Jun 3 '16 at 18:34

Specify the real domain in the Solve

Clear[p,f,g]

f[P_] = 54.758*P^(-0.4)*65^0.2 ;

gnokink[P_] = 0.126191*P^1.1 ;

g[C_, P_] = Piecewise[{{gnokink[P], gnokink[P] < C}, {C, gnokink[P] > C}}];

plot[C_] := Module[{
soln = Solve[g[C, p] == f[p], p, Reals][[1]] // Quiet},
Plot[{f[p], g[C, p]}, {p, 50, 150},
PlotLabel -> soln,
PlotLegends -> "Expressions",
Epilog -> {Red, AbsolutePointSize[6], Point[{p, f[p]} /. soln]}]]

plot /@ {18, 25, 30} // Column

Note that g is equivalent to

g[C_, P_] := Min[gnokink[P], C];

Now let's try to solve "directly". I know that Solve[g[30, p] == f[p], p] fails:

You only have to include ComplexExpand. Now all variables are assumed to be real.

f[P_] := 54.758*P^(-0.4)*65^0.2
gnokink[P_] := 0.126191*P^1.1
Solve[f[p] == gnokink[p], p][[1]]

{p -> 100.}

g[c_, P_] := Piecewise[{{gnokink[P], gnokink[P] < c}, {c, gnokink[P] > c}}]
Solve[ComplexExpand[g[30, p] == f[p]], p] // Quiet

{{p -> 100.}}