1
$\begingroup$

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.

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

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]}]

enter image description here

$\endgroup$
3
$\begingroup$

See also Average Function and Average Value of a Function.

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}]

enter image description here

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}]

enter image description here

$\endgroup$
3
$\begingroup$

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}]]]

Mathematica graphics

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

15.7072

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

-0.333333

$\endgroup$

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