# Piecewise function and deriving - solving

I'm working in a (Economics) problem where I want to add two concave (profit) functions in order to maximize them together through one variable (quantity). My problem is that one of the two functions shouldn't take negative values. I've built it as to take either the result of the function or 0, with Piecewise.

Afterwards, when I derive the added profit function, I get a doubled expression, that takes in consideration the two pieces of the piecewise equation. However, when I Solve that result equal to 0, I get nothing. I could do it manually, but the exercice goes further and the inability to restrain the function to be positive makes the results wrong (a negative price is keeping the firm from getting the quantity that would otherwise maximize both profits together).

p1 = Piecewise[{{0.8 - q,0.8 - q1>0}}];
p2 = 1 - q;
profit1 = (p1) * \[Gamma] * q;
profit2 = (p2 - cost) * q;
totalprofits = profit1 + profit2
dq = D[totalprofits, q]
Solve[dq == 0, q]


Thanks!

p1 = Piecewise[{{4/5 - q, 4/5 - q > 0}}]; (* used exact numbers *)


You could also use p1 = Max[4/5 - q, 0];.

p2 = 1 - q;
profit1 = p1 γ q;
profit2 = (p2 - cost) q;
totalprofits = profit1 + profit2;
dq = D[totalprofits, q];


Provide additional info on cost and γ in the first argument of Solve:

Solve[{dq == 0, cost >= 0, γ > 0}, q]


{{q -> ConditionalExpression[(5 - 5 cost + 4 γ)/(10 + 10 γ), γ > 0 && 0 < cost < 1/5 (5 + 4 γ)]}}

You can also use Maximize:

p1 = 4/5 - q;
p2 = 1 - q;
profit1 = (p1)*γ*q;
profit2 = (p2 - cost)*q;
totalprofits = profit1 + profit2;

{min, sol} = Simplify[
Maximize[{totalprofits, p1 >= 0}, q],
{cost > 0, γ > 0}
];

min
sol


$$\frac{(4 \gamma -5 \text{cost}+5)^2}{100 (\gamma +1)}$$

$$\left\{q\to \frac{4 \gamma -5 \text{cost}+5}{10 \gamma +10}\right\}$$

Notice that this presumes cost > 0 and γ > 0. You might have to change the second argument of Simplify to adjust it to your needs.