6
$\begingroup$
plotAreaApprox[f_, a_, b_, n_] := 
 Module[{h = (b - a)/n, rects}, 
  rects = Table[
    Rectangle[{i, 0.}, {i + h, f[i + h/2]}], {i, a, b - h, h}];
  Plot[f[x], {x, a, b}, 
   Epilog -> {EdgeForm[Black], FaceForm[None], rects}, 
   ImageSize -> 500, Axes -> False, 
   Frame -> {{True, False}, {False, True}}, 
   FrameLabel -> {{"I"[\[Phi]], None}, {None, None}}, 
   BaseStyle -> {FontSize -> 18}, FrameTicks -> None]]

plotAreaApprox[(2 #/(1 + #^2)) &, 0., 3, 10]

The above code will plot an image in mathematica, I want:

1) to show width of any one rectangle by saying <-$2\pi/T$-> (with arrows, but $2\pi/T$ can be beneath the arrow or in the middle)

2) On the top of the $8$th box $\mathbf{I}(\phi_8)$

3)x-axis starting from 0 and ending at $\pi$ with nothing else in the middle

Can anyone help me with that?

$\endgroup$
  • $\begingroup$ If you're really interested in producing LaTeX in MMA then I highly recommend the MaTeX package produced by (I believe) @Szabolcs, you can find it here: szhorvat.net/pelican/latex-typesetting-in-mathematica.html $\endgroup$ – Quantum_Oli Aug 20 '16 at 15:48
  • $\begingroup$ Thank you, from graphics -->maths mode, and I was able to solve the issue $\endgroup$ – Waqas Aug 20 '16 at 15:49
  • $\begingroup$ How about answer your question? $\endgroup$ – user9660 Aug 20 '16 at 15:59
  • $\begingroup$ Oh great I will do that $\endgroup$ – Waqas Aug 20 '16 at 16:00
7
$\begingroup$

Another approach is to do everything programmatically.

Add the arrows, line and text to Epilog. I also made a small adjustment to the padding so the text would print below the figure.

Note that although the right hand side of the x-axis is labeled π, it is in reality the number 3. You might want to adjust the input b value to match the axis label.

Plot[f[x], {x, a, b},
  Epilog ->
   {
    EdgeForm[Black],
    FaceForm[None],
    rects,
    Arrow[{{a + 3 h, 2*f[a + 3 h]/5}, {a + 3.4 h, 2*f[a + 3 h]/5}}],
    Arrow[{{a + 4 h, 2*f[a + 3 h]/5}, {a + 3.6 h, 2*f[a + 3 h]/5}}],
    Text[Style["\!\(\*FractionBox[\(2  π\), \(T\)]\)", 
      14], {a + 3.5 h, 2 f[a + 3 h]/7}],
    Text[Style["0", 14], {0, -0.05}],
    Text[Style["π", 14], {3, -0.05}],
    Text[Style["\!\(\*SubscriptBox[\(ϕ\), \(8\)]\)", 
      14], {a + 7.5 h, -0.05}],
    Text[Style["I[\!\(\*SubscriptBox[\(ϕ\), \(8\)]\)]", 
      14], {a + 7.5 h, f[a + 7.5 h] + 0.1}],
    Red,
    Line[{{a + 7.5 h, 0}, {a + 7.5 h, f[a + 7.5 h]}}]
    },
  ImageSize -> 500,
  Axes -> False,
  Frame -> {{True, False}, {False, True}},
  FrameLabel -> {{"I[ϕ]", None}, {None, None}},
  BaseStyle -> {FontSize -> 18},
  FrameTicks -> None,
  PlotRangePadding -> {{Scaled[0.03], Scaled[0.03]}, {Scaled[0.07], 
     Scaled[0.03]}}
  ]
 ]

plotAreaApprox[(2 #)/(1 + #^2) &, 0, 3, 10]

Mathematica graphics

$\endgroup$
  • $\begingroup$ This certainly looks more neat, perhaps a bit more time consuming and require deeper understanding of mathematica. $\endgroup$ – Waqas Aug 20 '16 at 21:36
  • $\begingroup$ @WakasJamil Normally you should wait a day or two before accepting an answer. Someone may come along with a better answer. It is OK to vote if you like it, but best to wait before accepting it. $\endgroup$ – Jack LaVigne Aug 20 '16 at 21:45
7
$\begingroup$

What I was looking for

Click on the picture and from the menu bar select Graphics-->drawing tools. A box will open by clicking on $\sum$, we can write mathematical text, one can explore more options.

$\endgroup$
3
$\begingroup$

You may use DiscretePlot to do most of the drawing for you with some help from FindDivisions for the Ticks. Then with Show most of the effort can be placed in Epilog with some help from Inset.

plotAreaApprox[f_, a_, b_, n_] :=
 Show[
  Plot[f[x], {x, a, b},
   Ticks -> {FindDivisions[{a, b, π}, 6], None}],
  DiscretePlot[f[x], {x, a, b, (b - a)/n}, 
   ExtentSize -> Center, PlotMarkers -> Point, 
   PlotStyle -> {Gray, EdgeForm[{Thin, Gray}]}, FillingStyle -> White],
  ImageSize -> Large,
  PlotRangePadding -> {{Scaled[.05], Scaled[.05]}, {Scaled[.15], Scaled[.1]}},
  Epilog -> {
    Inset[Row[{Style["I", Bold], "(\!\(\*SubscriptBox[\(ϕ\), \(7\)]\))"}, 
      FrameMargins -> Large, Frame -> True, FrameStyle -> None, 
      BaseStyle -> {FontSize -> Scaled[.025]}], 
     With[{p = a + 7 (b - a)/n}, {p, f[p]}], {Center, Bottom}],
    Inset[Style["\!\(\*SubscriptBox[\(ϕ\), \(7\)]\)", 
      FontSize -> Scaled[.025]], {a + 7 (b - a)/n, -.065}, {Center, Top}],
    Arrowheads[{-.02, .02}],
    Arrow[{{a + 1/2 (b - a)/n, -0.05}, {a + 3/2 (b - a)/n, -0.05}}],
    Inset[Style[(b - a)/n, Bold], {a + (b - a)/n, -.065}, {Center, Top}]}
  ]

Then

plotAreaApprox[(2 #/(1 + #^2)) &, 0, π, 10]

enter image description here

And with Manipulate altering the fineness of the approximation

Manipulate[
 plotAreaApprox[(2 #/(1 + #^2)) &, 0, 2 π, n],
 {{n, 7}, 7, 20, 1}]

enter image description here

Hope this helps.

$\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.