TransWikia.com

Inefficient Manipulate code with plots and integrals

Mathematica Asked by DJR on August 5, 2021

I did this manipulation piece based on Boltzmann distribution but I noticed that is rather slow, any ideas how to improve this code?

kb = 1.381*10^-23;
Nav = 6.022*10^23;
m = 0.032/Nav;

Manipulate[
  Show[
     Plot[(E^(-((m v^2)/(2 kb T))) Sqrt[2/π] v^2)/((kb T)/m)^(3/2), {v, 0, 4000}, 
       PlotRange -> {{0, 1300}, {0, 0.0055}}, 
       AxesLabel -> {"m/s", "Frequency"}, 
       Epilog -> 
         Inset[Framed[
           Style[
             Integrate[(
               E^(-((m v^2)/(2 kb T))) Sqrt[2/π] v^2)/((kb T)/m)^(3/2), 
               {v, a, Infinity}] /
             Integrate[(
               E^(-((m v^2)/(2 kb T))) Sqrt[2/π] v^2)/((kb T)/m)^(3/2), 
               {v, 0, Infinity}]*100, 
             10]]]], 
     Plot[(E^(-((m v^2)/(2 kb T))) Sqrt[2/π] v^2)/((kb T)/m)^(3/2), {v, a, 1300}, 
       PlotRange -> {{0, 1300}, {0, 0.0055}}, 
       Filling -> Bottom]], 
  {T, 50, 500}, 
  {a, 0, 1300}]

2 Answers

Actually, g[a,T] have analytic expression.

Integrate[f[v, T], {v, a, Infinity}, Assumptions -> T > 0]

$$1., -frac{1. sqrt{a^2} text{erf}left(0.0438624 sqrt{frac{a^2}{T}}right)}{a}+frac{0.0494935 a e^{-frac{0.00192391 a^2}{T}}}{sqrt{T}}$$

Integrate[f[v, T], {v, 0, Infinity}, Assumptions -> T > 0]

1

kb = 1.381*10^-23;
Nav = 6.022*10^23;
m = 0.032/Nav;
f[v_, T_] = (E^(-((m v^2)/(2 kb T))) Sqrt[2/π] v^2)/((kb T)/m)^(3/
      2);
g[a_, T_] = Integrate[f[v, T], {v, a, Infinity}, Assumptions -> T > 0 && a>0];
Manipulate[
 Show[Plot[f[v, T], {v, 0, 4000}, 
   PlotRange -> {{0, 1300}, {0, 0.0055}}, 
   AxesLabel -> {"m/s", "Frequency"}, 
   Epilog -> Inset[Framed[Style[g[a, T]*100, 10]]]], 
  Plot[f[v, T], {v, a, 1300}, PlotRange -> {{0, 1300}, {0, 0.0055}}, 
   Filling -> Bottom]], {T, 50, 500}, {a, 0, 1300}]

Another way is use NIntegrate instead of Integrate .

kb = 1.381*10^-23;
Nav = 6.022*10^23;
m = 0.032/Nav;
f[v_, T_] = (E^(-((m v^2)/(2 kb T))) Sqrt[2/π] v^2)/((kb T)/
      m)^(3/2);
g[a_?NumericQ, T_?NumericQ] := 
  NIntegrate[f[v, T], {v, a, Infinity}]/
   Integrate[f[v, T], {v, 0, Infinity}];
Manipulate[
 Show[Plot[f[v, T], {v, 0, 4000}, 
   PlotRange -> {{0, 1300}, {0, 0.0055}}, 
   AxesLabel -> {"m/s", "Frequency"}, 
   Epilog -> Inset[Framed[Style[g[a, T]*100, 10]]]], 
  Plot[f[v, T], {v, a, 1300}, PlotRange -> {{0, 1300}, {0, 0.0055}}, 
   Filling -> Bottom]], {T, 50, 500}, {a, 0, 1300}]

Answered by cvgmt on August 5, 2021

Evaluating the integrals used to produce the value shown in the inset will produce, as is pointed out in cvgnt's answer, a major improvement in your code's performance, but there are other improvements you can make. The following code shows how I would refactor your Manipulate, not only to improve performance, but also to improve the user experience.

kb = 1.381*10^-23;
Nav = 6.022*10^23;
m = 0.032/Nav;
vMax = 1300;

g[a_][T_] =
   Assuming[T > 0, 
     100 
       Integrate[(E^(-((m v^2)/(2 kb T))) Sqrt[2/π] v^2)/((kb T)/m)^(3/2), {v, a, ∞}] /
       Integrate[(E^(-((m v^2)/(2 kb T))) Sqrt[2/π] v^2)/((kb T)/m)^(3/2), {v, 0, ∞}]];

Manipulate[
  Show[
    Plot[(E^(-((m v^2)/(2 kb T))) Sqrt[2/π] v^2)/((kb T)/m)^(3/2), {v, 0, a}],
    Plot[(E^(-((m v^2)/(2 kb T))) Sqrt[2/π] v^2)/((kb T)/m)^(3/2), {v, a, vMax},
      PlotRange -> {Automatic, {0, 0.0055}},
      Filling -> Bottom],
    PlotRange -> {{0, vMax}, {0, 0.0055}},
    AxesLabel -> {"m/s", "Frequency"},
    Epilog -> Inset[Framed[Style[g[a][T ], 10]]],
    ImageSize -> 450],
  {T, 50, 500, 5, Appearance -> "Labeled", ImageSize -> Large},
  {a, 1, vMax - 1, Appearance -> "Labeled", ImageSize -> Large}]

demo

The main improvement in the above code, other than the one-time evaluation of the integrals in g, is in moving several plot options from the Plot functions to the higher level of Show. I have improved the Manipulator controls so they are easier to use and show their values.

Answered by m_goldberg on August 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP