4
$\begingroup$

How Can I plot the following figures for the following equations?

$x\in\mathbb{R}, f(x) = \frac{\left|\sin(x)\right|}{x^2+x+1}$

$t\in\mathbb{R}, A(t)=\displaystyle\int_t^{t+2\pi}\!\dfrac{|\sin x|}{x^2+x+1}\mathrm{d}x$

Details of The graphs

enter image description here

$\endgroup$
4
  • $\begingroup$ I vote that Mark McClure answer this one (see OPs link). $\endgroup$
    – bill s
    Dec 12 '14 at 20:32
  • $\begingroup$ At least vote him up. $\endgroup$ Dec 12 '14 at 20:40
  • $\begingroup$ As Mark's comment on his answer states, the code is given in the answer source. Here it is at the bottom: math.stackexchange.com/revisions/… $\endgroup$
    – user484
    Dec 12 '14 at 20:44
  • $\begingroup$ @Rahul, Thanks. I find it and produce it to a gif image. $\endgroup$
    – Nimbigli
    Dec 12 '14 at 21:33
5
$\begingroup$
f[x_] := Abs@Sin@x/(x x + x  + 1)
a[t_] := NIntegrate[f[x], {x, t, t + 2 Pi}]
tabA = Table[{t, a[t]}, {t, -3 Pi, 3 Pi, 6 Pi/100}];
opc = Sequence[ImageSize -> 400,  Ticks -> {Array[- 4 Pi + # Pi &, 6], Automatic}];
Animate[Column[{
   Show[Plot[f[x], {x, -3Pi, 3Pi}, AspectRatio->1/4, Evaluate@opc, PlotRange->{{-3 Pi, 3 Pi}, All}],
        Plot[f[x], {x, tabA[[p, 1]], tabA[[p, 1]] + 2 Pi}, Filling -> Axis]],
   Text["area = " <> ToString@N@tabA[[p, 2]]],
   ListLinePlot[tabA, Evaluate@opc, Epilog -> {PointSize@Large, Point[tabA[[p]]]}]}, Center], 
{p, 1, Length@tabA, 1}]

enter image description here

$\endgroup$
2
  • $\begingroup$ nice. but no control buttons :) $\endgroup$
    – Nasser
    Dec 13 '14 at 0:46
  • $\begingroup$ @Nasser I posted this one only because you were too lazy to note that the Op's model doesn't have buttons :D $\endgroup$ Dec 13 '14 at 2:49
2
$\begingroup$

It took me few minutes to figure what it is doing. But here is a Manipulate. I did not know the code is there and did not look at original one yet. I am sure it is done better than my attempt here:

enter image description here

Manipulate[
 tick;
 If[state == "RUN",
  tick = Not[tick];
  a = a + 0.1;
  If[a > 4 Pi,
   state = "RESET";
   a = -4 Pi;
   cArea = {}
   ]
  ];

 Module[{area, g},
  area = NIntegrate[f, {x, a, a + 2 Pi}];
  g = Grid[{{
      Show[
       Plot[f, {x, -4 Pi, 4 Pi},
        PlotRange -> {{-4 Pi, 4 Pi}, {0, .9}}, 
           PlotLabel -> Row[{"Area = ", area}]
        ],

       Plot[f, {x, a, a + 2 Pi}, Filling -> Bottom],

       PlotRange -> {{-4 Pi, 4 Pi}, {0, .9}}, ImageSize -> 400
       ]
      },
     {
      AppendTo[cArea, {a, area}];
      ListPlot[cArea, Joined -> True, 
         PlotRange -> {{-4 Pi, 4 Pi}, {0, 2}},
         Epilog -> Point[{a, area}], 
         Ticks -> {{-4 Pi, -2 Pi, 0, 2 Pi, 4 Pi, 2}, Automatic}]
      }
     }, Frame -> All];
  g
  ],

 Grid[{{Button[Text@Style["run", 12], {state = "RUN"; tick = Not[tick]}, 
      ImageSize -> {60, 40}], 
    Button[Text@Style["stop", 12], {state = "STOP"; tick = Not[tick]}, 
      ImageSize -> {60, 40}], 
    Button[Text@Style["reset", 12], {state = "RESET"; a = -4 Pi;
      cArea = {}; tick = Not[tick]}, ImageSize -> {60, 40}]}}, 
    Spacings -> {.5, 0}, Frame -> True, FrameStyle -> Gray
  ],

 {{a, -3.99 Pi}, None},
 {{cArea, {}}, None},
 {{tick, False}, None},
 {{state, "STOP"}, None},
 TrackedSymbols :> {tick},
 SynchronousUpdating -> True,
 Initialization :>
  (
   f = Abs[Sin[x]]/(x^2 + x + 1)
   )
 ]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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