Yes, comes out the same when I reverse the order of Q_opt

Yes, comes out the same when I reverse the order of Q_opt

Thanks.
To be clear, the plots you get look different from the plots in the original file that I uploaded, i.e you get correct plots? I assume you saw my plots in my original uploaded file that showed Q_opt increasing (concave) in s?

Thanks.
To be clear, the plots you get look different from the plots in the original file that I uploaded, i.e you get correct plots? I assume you saw my plots in my original uploaded file that showed Q_opt increasing (concave) in s?

Thanks -- incredibly helpful. Do you convert piecewise functions to a Heaviside representation generally, or are there things you look for before deciding to do the conversion (and back to piecewise)?
My take-away from this was that Maple handles the problem much more capably when you explicitly define the breaks, i.e. the piecewise nature of the function and, specifically, the cases where it's undefined. Would you say that's fair?
Thanks again.

Thanks -- incredibly helpful. Do you convert piecewise functions to a Heaviside representation generally, or are there things you look for before deciding to do the conversion (and back to piecewise)?
My take-away from this was that Maple handles the problem much more capably when you explicitly define the breaks, i.e. the piecewise nature of the function and, specifically, the cases where it's undefined. Would you say that's fair?
Thanks again.

Thanks -- exactly what I want to do. However, when I use the above procedure with my problem, I get the following error (where I have only shown part of the array):
Error, (in maxpt) invalid input: ArrayDims expects its 1st argument, A, to be of type Array, but received [[.2500000000, .3125000000, .3620558262, .4031851184, .4375000000, .4659321892, .4891103366, .5074959503, .5214466094, undefined, .5368923611,...
I wonder if the error is coming from the fact that there are undefined elements in my array A? If so, what's a good fix?

Thanks -- exactly what I want to do. However, when I use the above procedure with my problem, I get the following error (where I have only shown part of the array):
Error, (in maxpt) invalid input: ArrayDims expects its 1st argument, A, to be of type Array, but received [[.2500000000, .3125000000, .3620558262, .4031851184, .4375000000, .4659321892, .4891103366, .5074959503, .5214466094, undefined, .5368923611,...
I wonder if the error is coming from the fact that there are undefined elements in my array A? If so, what's a good fix?

Yes, I get the plot of a line when I use your sample FOC.
My FOC is a piecewise function, defined as follows:
FOC := s*Q+1/4*s^2-PIECEWISE([1, 1 < Q+1/2*pa], [Q+1/2*pa, otherwise])^2*PIECEWISE([1, Q < 1-1/2*pa], [undefined, Q = 1-1/2*pa], [0, 1-1/2*pa < Q])+Q^2+PIECEWISE([1, 1 < Q+1/2*pa], [Q+1/2*pa, otherwise])^2+2*Q*PIECEWISE([1, 1 < Q+1/2*pa], [Q+1/2*pa, otherwise])*PIECEWISE([1, Q < 1-1/2*pa], [undefined, Q = 1-1/2*pa], [0, 1-1/2*pa < Q])-2*Q*PIECEWISE([1, 1 < Q+1/2*pa], [Q+1/2*pa, otherwise])-Q^2*PIECEWISE([1, Q < 1-1/2*pa], [undefined, Q = 1-1/2*pa], [0, 1-1/2*pa < Q])-s*PIECEWISE([1, 1 < Q+1/2*pa], [Q+1/2*pa, otherwise])+pa*PIECEWISE([1, 1 < Q+1/2*pa], [Q+1/2*pa, otherwise])*PIECEWISE([1, Q < 1-1/2*pa], [undefined, Q = 1-1/2*pa], [0, 1-1/2*pa < Q])+1/4*pa^2*PIECEWISE([1, Q < 1-1/2*pa], [undefined, Q = 1-1/2*pa], [0, 1-1/2*pa < Q])-s*(1-PIECEWISE([1, 1 < Q+1/2*pa], [Q+1/2*pa, otherwise]))+pa*(1-PIECEWISE([1, 1 < Q+1/2*pa], [Q+1/2*pa, otherwise]))+pa*(-1/2*pa-Q)*PIECEWISE([1, Q < 1-1/2*pa], [undefined, Q = 1-1/2*pa], [0, 1-1/2*pa < Q])-p
In my code, my FOC is defined as follows, where U ("utility") is a piecewise function:
FOC:=diff(U-p*Q,Q);
Clearly fsolve is able to evaluate my FOC for given parameters (and evaluate it to values that I find reasonable), per my previous post.
Thanks!

Yes, I get the plot of a line when I use your sample FOC.
My FOC is a piecewise function, defined as follows:
FOC := s*Q+1/4*s^2-PIECEWISE([1, 1 < Q+1/2*pa], [Q+1/2*pa, otherwise])^2*PIECEWISE([1, Q < 1-1/2*pa], [undefined, Q = 1-1/2*pa], [0, 1-1/2*pa < Q])+Q^2+PIECEWISE([1, 1 < Q+1/2*pa], [Q+1/2*pa, otherwise])^2+2*Q*PIECEWISE([1, 1 < Q+1/2*pa], [Q+1/2*pa, otherwise])*PIECEWISE([1, Q < 1-1/2*pa], [undefined, Q = 1-1/2*pa], [0, 1-1/2*pa < Q])-2*Q*PIECEWISE([1, 1 < Q+1/2*pa], [Q+1/2*pa, otherwise])-Q^2*PIECEWISE([1, Q < 1-1/2*pa], [undefined, Q = 1-1/2*pa], [0, 1-1/2*pa < Q])-s*PIECEWISE([1, 1 < Q+1/2*pa], [Q+1/2*pa, otherwise])+pa*PIECEWISE([1, 1 < Q+1/2*pa], [Q+1/2*pa, otherwise])*PIECEWISE([1, Q < 1-1/2*pa], [undefined, Q = 1-1/2*pa], [0, 1-1/2*pa < Q])+1/4*pa^2*PIECEWISE([1, Q < 1-1/2*pa], [undefined, Q = 1-1/2*pa], [0, 1-1/2*pa < Q])-s*(1-PIECEWISE([1, 1 < Q+1/2*pa], [Q+1/2*pa, otherwise]))+pa*(1-PIECEWISE([1, 1 < Q+1/2*pa], [Q+1/2*pa, otherwise]))+pa*(-1/2*pa-Q)*PIECEWISE([1, Q < 1-1/2*pa], [undefined, Q = 1-1/2*pa], [0, 1-1/2*pa < Q])-p
In my code, my FOC is defined as follows, where U ("utility") is a piecewise function:
FOC:=diff(U-p*Q,Q);
Clearly fsolve is able to evaluate my FOC for given parameters (and evaluate it to values that I find reasonable), per my previous post.
Thanks!

Thanks for the suggestions, Doug. I made the changes to my Q_opt procedure -- to check that al l three initial arguments are numeric -- but I am still not able to plot.
It's not that Maple returns an error message: it just struggles and apparently takes forever! (As in minutes, while it says "Evaluating...")
I ran a few manual checks on the values fsolve was returning for the range I of the s_val parameter that I wanted to plot, to make sure that there wasn't some numerical instability.
For instance, I am simply trying to plot:
plot( Q_opt(s,0,0.6), s=0.5..0.55, axes=BOXED);
where my Q_opt procedure looks like yours:
> Q_opt:=proc(s_val,p_val,pa_val)
> if not is({args}, set(numeric)) then
> return 'procname'('args')
> end if;
> fsolve(eval (FOC,[s=s_val,p=p_val,pa=pa_val]),Q);
> end proc:
> Q_opt(a,1,1);
Q_opt(a, 1, 1)
No luck! Yet the values that fsolve returns for the range I'm plotting seem completely reasonable:
> fsolve(eval (FOC,[p=0,s=0.5,pa=0.6]),Q);
0.7500000000
> fsolve(eval (FOC,[p=0,s=0.51,pa=0.6]),Q);
0.7450000000
> fsolve(eval (FOC,[p=0,s=0.52,pa=0.6]),Q);
0.7400000000
> fsolve(eval (FOC,[p=0,s=0.53,pa=0.6]),Q);
0.7349999998
> fsolve(eval (FOC,[p=0,s=0.54,pa=0.6]),Q);
0.7299999998
> fsolve(eval (FOC,[p=0,s=0.55,pa=0.6]),Q);
0.7249999999

Thanks for the suggestions, Doug. I made the changes to my Q_opt procedure -- to check that al l three initial arguments are numeric -- but I am still not able to plot.
It's not that Maple returns an error message: it just struggles and apparently takes forever! (As in minutes, while it says "Evaluating...")
I ran a few manual checks on the values fsolve was returning for the range I of the s_val parameter that I wanted to plot, to make sure that there wasn't some numerical instability.
For instance, I am simply trying to plot:
plot( Q_opt(s,0,0.6), s=0.5..0.55, axes=BOXED);
where my Q_opt procedure looks like yours:
> Q_opt:=proc(s_val,p_val,pa_val)
> if not is({args}, set(numeric)) then
> return 'procname'('args')
> end if;
> fsolve(eval (FOC,[s=s_val,p=p_val,pa=pa_val]),Q);
> end proc:
> Q_opt(a,1,1);
Q_opt(a, 1, 1)
No luck! Yet the values that fsolve returns for the range I'm plotting seem completely reasonable:
> fsolve(eval (FOC,[p=0,s=0.5,pa=0.6]),Q);
0.7500000000
> fsolve(eval (FOC,[p=0,s=0.51,pa=0.6]),Q);
0.7450000000
> fsolve(eval (FOC,[p=0,s=0.52,pa=0.6]),Q);
0.7400000000
> fsolve(eval (FOC,[p=0,s=0.53,pa=0.6]),Q);
0.7349999998
> fsolve(eval (FOC,[p=0,s=0.54,pa=0.6]),Q);
0.7299999998
> fsolve(eval (FOC,[p=0,s=0.55,pa=0.6]),Q);
0.7249999999

m and p are parameters. For my problem, it is true that pa>m to get a solution. There are certain requirements for the parameter values and their relationships, as one would expect

m and p are parameters. For my problem, it is true that pa>m to get a solution. There are certain requirements for the parameter values and their relationships, as one would expect