4

I am plotting the Maxwell-Boltzman distribution of molecular speeds. I want to be able to select a region to integrate using a manipulate function. Then I want to have that same region filled in on the plot. How can this be done? I can't seem to get filling to work quite correctly.

f[u_] := 
  4 Pi ( (m)/(2 Pi kb T))^(3/2) u^2 Exp[ -(m u^2)/(2 kb T)];
subs = { kb -> 1.3806 * 10^(-23), T -> 300, m -> .0032/(6*10^(23))};
Plot[f[u] /. subs , {u, 0, 5000}, 
  PlotRange -> All, 
  Filling -> {1000 -> Axis}]
Integrate[f[u] /. subs, {u, 1000, 2000}]

Manipulate[ 
 Integrate[f[u] /. subs, 
 {u, a, b}], 
 {a, 0, 5000}, 
 {b, 0, 5000} ]
kglr
  • 394,356
  • 18
  • 477
  • 896
olliepower
  • 2,254
  • 2
  • 21
  • 34

2 Answers2

4

I am guessing you want to do something like

Manipulate[
 Column[{Row[{With[{a = a, b = b}, HoldForm[Integrate[f[u] /. subs, {u, a, b}]]], 
             Integrate[f[u] /. subs, {u, a, b}]}, "  =  "], 
             Plot[{ConditionalExpression[f[u] /. subs, a <= u <= b], f[u] /. subs}, 
             {u, 0, 5000}, PlotRange -> All, 
             Filling -> {1 -> Axis}, ImageSize -> 400]}],
 {a, 0, 5000}, {b, 0, 5000}]

Note: You can also use

Piecewise[{{f[u], a <= u <= b}}, Indeterminate] /. subs

or

Boole[a <= u <= b] f[u]/.subs

instead of ConditionalExpression[f[u] /. subs, a <= u <= b].

enter image description here

See also: this answer to a related Q/A

kglr
  • 394,356
  • 18
  • 477
  • 896
4

you can also try UnitStep

Manipulate[
 Plot[{f[u] /. subs, 
   f[u] UnitStep[u - a] UnitStep[b - u] /. subs}, {u, 0, 5000}, 
  PlotRange -> All, Filling -> {2 -> Axis}, 
  Epilog -> {PointSize[Large], 
    Point[{{a, f[a] /. subs}, {b, f[b] /. subs}}]}] , {a, 0, 
  4000}, {b, 100, 4000}]

enter image description here

Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78