# Question:Handling Piecewise FOCs in an Optimization Problem

## Question:Handling Piecewise FOCs in an Optimization Problem

Maple
I have an optimization problem where my integration bounds depend on the maximizer and take the form max[1,Q+pa/2], where Q is the variable that I am maximizing with respect to.

For instance, my function U (utility), which I am maximizing is defined as follows:

U:=simplify(int(-(-d)^2,d=0..D_lbar)+int(-(-s/2)^2-s*(d-s/2),d=D_lbar..D_bar2(Q))
+int(-(Q-d)^2-s*Q,d=D_bar2(Q)..DD_bar2(Q)))+int(-(-pa/2)^2-s*Q-pa*(d-pa/2-Q),d=DD_bar2(Q)..1)

where

D_lbar:=s/2;
D_bar:=Q+s/2;
D_bar2:=Q->piecewise(Q+s/2>1,1,Q+s/2);
DD_bar2:=Q->piecewise(Q+pa/2>1,1,Q+s/2);

I get a piecewise FOC for U, with respect to Q, and I'm able to solve for the optimal Q, given the parameters s, p, and pa, using fsolve. For example:

fsolve(subs(p=0,subs(s=0.6-0.01,subs(pa=0.6,FOC_1_alt))),Q);
0.70500

Here's my question:

I want to take the piecewise expression for the optimal Q and use it in a second maximization problem, where I am maximizing the following expression, a function of Q, with respect to (s,p) (and pa, m, and K are all given parameters):

profit:=simplify(p*Q_opt + s*subs(Q=Q_opt),y) + m*(K-subs(Q=Q_opt,y)));

where

y:=simplify(int((d-s/2),d=D_lbar..D_bar)+int(Q,d=D_bar..1));

and Q_opt would be my expression for the optimal Q

But

fsolve({subs(K=1,subs(m=0.5,subs(pa=0.8,profit)))},{s,p});

returns an error:

Error, (in fsolve) Can't handle expressions with typed procedures

because I have defined Q_opt as a piecewise function that depends on Q_opt itself (since the integration bound that I need to check is Q+pa/2<=1), i.e.

Q_opt:=Q_opt->piecewise(Q_opt+pa/2<1,Q_Case_1,Q_Case_2);

I have also tried using the following procedure, where I define Q_Case_1 and Q_Case_2 as the expressions for the optimal Q depending on whether Q+pa/2<=1 (Case 1) or Q+pa/2>1 (Case 2):

> profit:=proc(s,p,m,pa,K)
> if Q<1-1/2*pa then
> p*Q_Case_1 + s*subs(Q=Q_Case_1,y) + m*(K-subs(Q=Q_Case_1,y));
> else
> p*Q_Case_2 + s*subs(Q=Q_Case_2,y) + m*(K-subs(Q=Q_Case_2,y));
> end if;
> end proc:
> fsolve({profit(s,p,0.5,0.8,1)},{s,p},{s=0..0.7,p=0..10});

and get an error:

Error, (in profit) cannot determine if this expression is true or false: Q < .6000000000

This doesn't work since Q is not known when Maple goes to evaluate the if statement in proc!

Clearly my problem is the recursive nature of my definition of the optimal Q. What is the correct implementation for this optimization problem? Thanks! ﻿