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!