0
$\begingroup$

I have a piecewise function that has as one piece the result of a numerical approximation, like in the example below:

Y = 1;
a = 1;
b = 0.9;
alphalowx = a*Log[a + b];
alphahighx = Y + a*Log[a + b] - (a + b - 1);
f1[x_?NumericQ] := 
  Piecewise[{{0, x <= alphalowx}, {x - alphalowx, 
     alphalowx < x <= alphahighx}}, 
   FindRoot[f1value + a*Log[1 + Y - f1value] - x, {f1value, 0.01}][[1,
      2]]];
g1[x_?NumericQ] := 
  Piecewise[{{E^(x/a) - 1, x <= alphalowx}, {a + b - 1, 
     alphalowx < x <= alphahighx}}, Y - f1[x]];

And I have a function that combine these two piecewise functions:

V[x_?NumericQ] := f1[x] + b*Log[1 + g1[x]];

Finally, I want to use a numerical approximation, FindRoot, to calculate the value of a variable using an equation that required the derivative of V[x] above:

lambda1 = 1 + b*D[V[x], x]
g11 = a + b*lambda1
xvalue = FindRoot[
   x + b*Log[1 + g11] + b*V[Y - x - g11, x] - 0.3, {x, 0.1}][[1, 2]]

Of course, this can be done by hand. But I would like to know if there is a way to make it "automatic" on Mathematica. Basically, I would to tell Mathematica: if there is no FindRoot, just take the derivative, otherwise, if you find a FindRoot, calculate the derivative using the implicit function theorem, or something like that. Hopefully this example is clear. Thank you!

$\endgroup$
  • $\begingroup$ You may use Check to determine wether FindRoot was successful. $\endgroup$ – Henrik Schumacher Feb 9 '18 at 9:05
  • $\begingroup$ I could, @HenrikSchumacher. But the problem is the way is coded above the xvalue cannot be found because Mathematica doesn't take the derivative of a function that has to have a numerical solution. Therefore, is less about checking and more about how to handle the derivative of the FindRoot piece. $\endgroup$ – Laura K Feb 9 '18 at 15:19
  • $\begingroup$ In fact, just to try to clarify even more, if I remove the ?NumericQ the derivatives look correct, but then the initial FindRoots don't work anymore. $\endgroup$ – Laura K Feb 9 '18 at 15:28
  • $\begingroup$ Would you please remove every not essential to the question from the post? For example, I have the feeling that your question is not related at all to Piecewise functions... $\endgroup$ – Henrik Schumacher Feb 9 '18 at 18:59
  • $\begingroup$ I would, but it is related. If the function was not piecewise and f[] was the result of a FindRoot I would simply use the Dt function to find the implicit derivative of f[] with respect to x. However, since f[] is piecewise, I need to tell Mathematica: if f[] is not numerical (ie, if it is a closed-form), then simply calculate the derivative; otherwise calculate the implicit derivative. Hopefully that is clearer. $\endgroup$ – Laura K Feb 9 '18 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.