# Average function value over an Interval [closed]

What is the best way to find the average value of a function over a specific interval in Mathematica? I can not figure it out. I have two points on the interval and need to find the average value.

• NIntegrate[f[x], {x, a, b}]/(b - a) Apr 23 '16 at 4:00
• How would you do it on paper? Apr 23 '16 at 15:59
• To those voting to close, I don't think the folks over at math.se will appreciate migrating these kind of questions. Apr 25 '16 at 21:03

I was just inspired by @kglr to illustrate:

f[x_] := 1/1000 x^4 - 280/1000 x^2 + 25;
sim[a_, b_, n_] :=
With[{u = RandomVariate[UniformDistribution[{a, b}], n]},
{#, f@#} & /@ u];
Manipulate[
Module[{res = sim[a, b, n], mn = Integrate[f[x], {x, a, b}]/(b - a)},
Show[Plot[f[x], {x, a, b}],
ListPlot[res, PlotStyle -> Red],
GridLines -> {None, {{mn,
Black}, {Mean[res[[All, 2]]], {Red, Dashed}}}},
PlotRange -> {{-12, 12}, {0, 25}}]],
{a, -12, 0}, {b, 0, 12}, {n, PowerRange[10, 10000, 10]}] Say you have messured values and express them with the Function f(x):

f[x_] := 1/1000 x^4 - 280/1000 x^2 + 25


You can Plot that Function:

Plot[f[x], {x, -15, 15}] And you like to find the average value as of -12 ... 12

a = -12; b = 12;

solNI = NIntegrate[f[x], {x, a, b}]/(b - a)


15.7072

Plot[{f[x], solNI}, {x, -15, 15}] You can also use the built-in functions Expectation or NExpectation taking the function argument x to be uniformly distributed over the interval {a,b}.

f[x_] := 1/1000 x^4 - 280/1000 x^2 + 25

Expectation[f[x], Distributed[x, UniformDistribution[{a, b}]]] NExpectation[f[x], Distributed[x, UniformDistribution[{-12, 12}]]]


15.7072

NExpectation[x Sin[x],
Distributed[x, UniformDistribution[{-Pi, 2 Pi}]]]


-0.333333