title: Mathematica: Defining a function as the derivative or integral of a piecewise defined function
body: <p>I am trying to evaluate a function that I have defined piecewise. I need to integrate it with respect to one variable and then take the derivative with respect to another (variables are independent). However, something in the way I am defining the functions is causing mathematica to throw errors or infinitely evaluate. I believe that the derivative function does not like the format of the output of the integrate function, and vice versa when I tired reversing the order of the steps. The integrand is not analytical by hand, so I need to pipe the output of one into the other. Can anyone tell me what is going wrong?</p>
  
  <pre><code>\[Theta] = 30 Degree; 
  d = 50.8*10^-3 ;
  reo = (150/2)*10^-3; 
  rei = ((reo/Tan[\[Theta]]) - 
      d) Tan[\[Theta]] 
  
  B = 24.4*10^-3; 
  \[CapitalGamma] = 10*10^-3; 
  l = .2*10^-3; 
  \[CurlyPhi] = 20 Degree;
  
  Pe = 101325; 
  Ps = 1.1* Pe ;
  
  \[Gamma] = (\[CurlyPhi]*Sin[\[Theta]])/(B*\[CapitalGamma]^3);
  Pd[h_] := 
   Sqrt[((Ps^2 + Pe^2*\[Gamma]*h^3*(l + h) + 
        Log[rei/reo])/(1 + \[Gamma]*h^3*(l + h) + Log[rei/reo]))]
  
  rd = Sqrt[reo*rei]
  P [r_, h_] := 
    Piecewise[{{Sqrt[
        Pd[h]^2 + .5*(Pe^2 - Pd[h]^2)*Log[rei/reo]*Log[rd/r]], 
       r &gt; rd}, {Sqrt[
        Pd[h]^2 + .5*(Pe^2 - Pd[h]^2)*Log[rei/reo]*Log[r/rd]], r &lt; rd}}];
  W[h_] := Integrate[2*Pi*r*P[r, h]/9.8, {r, rei, reo}]
  S[h_] := D[W[h], h]
  
  Plot[{P[r, 10*10^-6], P[r, 8*10^-6], P[r, 6*10^-6], P[r, 4*10^-6], 
    P[r, 2*10^-6], P[r, 1*10^-6]}, {r, rei, reo}]
  Plot[W[h], {h, 1*10^-6, 10*10^-6}]
  Plot[S[h], {h, 1*10^-6, 10*10^-6}]
  </code></pre>