Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i want to calculate the area percentage of a function in a certain regime above a constant threshold. For example:

f[x] = sin[x] x² 

from 0 to 2 with a threshold g(x) = k

If I would do it by hand, I would identify the intersections points between the two functions and than calculate the integrals of the right ones and substract the value of G(x) from F(x) to just get the volume above the threshold. Depending on the number of intersections there quite a number of integrals to consider.

So I thought, let Mathematica do the the leg work! Well, but I am quite a novice in Mathematica, and soon realised that I have only a vague idea how to do it in Mathematica. Probably there is even a better way to do it than described in the middle paragraph, because at the end it could also just approximate the enclosed areas numerically.

Can anyone show me how to do it?

Edit: Thank your very much for your answers/solutions. With a small correction (probably I explained poorly what I wanted) the solution from rasher worked just fine (and I understood the code used). In will also try to learn from Chenminqi solution which looks defintely more sophisticated and exceeds my current mathematica knowledge. So again, thanks to everyone!

share|improve this question

Following @Chenminqi's method ;

   xmin = NSolve[f[x] == threshold && 0 <= x <= 2, x][[1, 1, 2]] //Quiet;
      NIntegrate[f[x] - threshold, {x, xmin, 2}]/smax*100, {4, 1}, 
        NumberPadding -> {" ", " "}] <> " %", Red, 18],
   Plot[{f[x], threshold}, {x, 0, 2},
         Filling -> {2 -> {{1}, {Yellow, White}}}, ImageSize -> 350]}],
  {{threshold, ymin + 0.01}, ymin + 0.01, ymax - 0.01, Appearance -> "Labeled"}, 
  Initialization :> (f[x_] := Sin[x] x^2; 
     smax = NIntegrate[f[x], {x, 0, 2}];
     ymin = First@NMinimize[{f[x], 0 <= x <= 2}, x];
     ymax = First@NMaximize[{f[x], 0 <= x <= 2}, x])]

enter image description here

share|improve this answer
 k = 1.69
 (NIntegrate[Max[0, # - k] , {x, 0, 2}]/
       NIntegrate[ # , {x, 0, 2}] *100  ) & @ (Sin[x] x^2) 


Incidentally this works analytically:

k = Pi^2/(6 Sqrt[3])
(Integrate[Max[0, # - k] , {x, 0, 2}]/
    Integrate[ # , {x, 0, 2}] *100  ) & @ (Sin[x] x^2) 

enter image description here

Which I find a bit surprising since this doesn't work:

 Solve[Sin[x] x^2 == Pi^2/(6 Sqrt[3]), x]
share|improve this answer
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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