Area enclosed by two functions

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!

=================

=================

3 Answers
3

=================

Following @Chenminqi’s method ;

Manipulate[
xmin = NSolve[f[x] == threshold && 0 <= x <= 2, x][[1, 1, 2]] //Quiet; Column[{Style[ToString@NumberForm[ 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])] k = 1.69 (NIntegrate[Max[0, # - k] , {x, 0, 2}]/ NIntegrate[ # , {x, 0, 2}] *100 ) & @ (Sin[x] x^2) 28.3033 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) Which I find a bit surprising since this doesn't work: Solve[Sin[x] x^2 == Pi^2/(6 Sqrt[3]), x] f[func1_,func2_,xmin_,xmax_]:=Module[{sol}, sol=Solve[func1[x]==func2&&xminfunc2,PrependTo[sol,xmin]];
If[func1[xmax]>func2,AppendTo[sol,xmax]];
Column[{Total[#2-#1&@@@Partition[sol,2]]/(xmax-xmin),
Plot[{func1[x],If[Or@@(Less[#1,x,#2]&@@@Partition[sol,2]),func2,func1[x]]},
{x,xmin,xmax},Filling->{1->{2}},ImageSize->300,PlotRange->All]}]]
Manipulate[f[Sin[#]&,k,0,4Pi],{k,-0.9999,0.9999}]
Manipulate[f[Cos[#]E^(-#/5)&,k,0,4Pi],{k,-0.9999,0.9999}]