3
$\begingroup$

I define the following piecewise function:

U[c_,n_,g_,p_]:=Piecewise[{{c^(1-g)/(1-g)-n^(1+p)/(1+p),g!=1}, {Log[c]-n^(1+p)/(1+p),g==1}}]

When I take the derivative

D[U[c,n,g,p],c]

It returns a a third unexpected line.

enter image description here

(In the image, gamma corresponds to g)

What is the meaning of this third line and how can I remove it from the printed output?

$\endgroup$

2 Answers 2

3
$\begingroup$
Clear["Global`*"]

Instead of using g != 1 use g < 1 || g > 1, then

U[c_, n_, g_, p_] := 
 Piecewise[{{c^(1 - g)/(1 - g) - n^(1 + p)/(1 + p), g < 1 || g > 1}}, 
  Log[c] - n^(1 + p)/(1 + p)]

D[U[c, n, g, p], c] // Simplify

(* c^-g *)

EDIT: Or use Simplify with your original definition

U[c_, n_, g_, p_] := 
 Piecewise[{{c^(1 - g)/(1 - g) - n^(1 + p)/(1 + p), 
    g != 1}, {Log[c] - n^(1 + p)/(1 + p), g == 1}}]

D[U[c, n, g, p], c] // Simplify

(* c^-g *)
$\endgroup$
3
  • $\begingroup$ Using Simplify does not return the derivative for the second case of the piecewise function, i.e. I expect two outputs but I only get one $\endgroup$
    – NC520
    Oct 3, 2022 at 21:21
  • $\begingroup$ They simplify to one case since c^-1 == 1/c $\endgroup$
    – Bob Hanlon
    Oct 3, 2022 at 21:41
  • $\begingroup$ I understand thank you $\endgroup$
    – NC520
    Oct 3, 2022 at 22:34
2
$\begingroup$

It looks like Mathematica insists on having a default condition (with value zero, unless otherwise specifed).

With

ass=D[U[c,n,g,p],c]

You can construct something like

Piecewise[Drop[ass[[1]]], ass[[1, 2, 1]]]

This uses the second terms as the fall through value.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.