4
$\begingroup$

This question already has an answer here:

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} ]
$\endgroup$

marked as duplicate by Mr.Wizard Oct 18 '14 at 18:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ This question is somewhat related. $\endgroup$ – m_goldberg Oct 10 '14 at 3:08
4
$\begingroup$

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

$\endgroup$
4
$\begingroup$

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

$\endgroup$
  • $\begingroup$ kguler I think that this question is a duplicate of the one you link with See also: -- do you disagree, and if so, why? $\endgroup$ – Mr.Wizard Oct 18 '14 at 18:17
  • $\begingroup$ @Mr.Wizard, it looks so.. $\endgroup$ – kglr Oct 18 '14 at 18:49

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