5

I want to draw the upper Riemann sum of a function like below with pstricks, however is does not take the supremum correctly (seems just to take the maximum of the left and right value of the step). Any idea what's wrong here and how to fix it?

\documentclass{standalone}
\usepackage{pstricks-add}

\psset{algebraic,plotpoints=100}
\begin{document}
\def\f(x){-x^2 + 10}

\begin{pspicture}(-6,-3)(6,12) 
   \psStep[StepType=supremum,linewidth=1.5pt,linecolor=red](-3,3){3}{\f(x)}
   \psplot[linewidth=1.5pt]{-3}{3}{\f(x)}
   \psaxes[Dy=2]{->}(0,0)(-4.7,-2)(4.7,11)[$x$,0][$y$,90]
\end{pspicture}

\end{document}

enter image description here

student
  • 29,003
  • Use that version (3.77): texnik.dante.de/tex/generic/pstricks-add/pstricks-add.tex It also fixes the bug for the infimum, which had the same problem. An upload to CTAN/TeXLive is on the way. –  Dec 09 '14 at 07:27

3 Answers3

5

Indeed, \psStep uses only the left and right values and uses the larger one for the step.

Here is a modified version of \psStep, which divides each interval in 20 subintervals to compute the supremum:

\documentclass[margin=12pt,pstricks]{standalone}
\usepackage{pstricks-add}
\makeatletter
\def\psStepSupremum{\def\pst@par{}\pst@object{psStepSupremum}}
\def\psStepSupremum@i(#1,#2)#3#4{%
  \begin@ClosedObj%
  \addto@pscode{
    /Func \ifPst@algebraic (#4) tx@addDict begin AlgParser end cvx \else {#4} \fi def 
    /x #1  def
    /dx #2 #1 sub #3 div def
    /scx { \pst@number\psxunit mul } def 
    /scy { \pst@number\psyunit mul } def
      x scx 0 moveto 
      #3 {
        \ifPst@algebraic Func \else #4 \fi /y0 ED % left value f(x)
        /x1 x dx add def
        20 { 
          /x x dx 20 div add def
          \ifPst@algebraic Func \else #4 \fi 
          dup y0 gt { /y0 ED } if
        } repeat
        /x x1 def
        \ifPst@algebraic Func \else #4 \fi /y1 ED % left value f(x)
        y0 y1 gt { y0 }{ y1 } ifelse          % use supremum
    scy dup x dx sub scx exch \ifPst@noVerticalLines  moveto \else lineto \fi 
        x scx exch lineto 
        x scx 0 \ifPst@noVerticalLines  moveto \else lineto \fi
    closepath x scx 0 moveto 
      } repeat
  }%
  \psk@fillstyle
  \pst@stroke
  \end@ClosedObj%
}
\makeatother

\begin{document}
\psset{algebraic,plotpoints=100}
\def\f(x){-x^2 + 10}

\begin{pspicture}(-6,-3)(6,12) 
   \psStepSupremum[linewidth=1.5pt,linecolor=red](-3,3){3}{\f(x)}
   \psplot[linewidth=1.5pt]{-3}{3}{\f(x)}
   \psaxes[Dy=2]{->}(0,0)(-4.7,-2)(4.7,11)[$x$,0][$y$,90]
\end{pspicture}
\end{document}

enter image description here

Christoph
  • 16,593
  • 1
  • 23
  • 47
4

With the updated version:

\documentclass[pstricks,border=10pt]{standalone}
\usepackage{pstricks-add}

\psset{algebraic,plotpoints=100}
\begin{document}
\def\f(x){-x^2 + 10}

\begin{pspicture}(-5,-3)(5,12) 
   \psStep[StepType=sup,linewidth=1.5pt,linecolor=red](-3,3){3}{\f(x)}
   \psplot[linewidth=1.5pt]{-3}{3}{\f(x)}
   \psplot[linewidth=1.5pt,linestyle=dotted]{-3}{3}{x^2+1}
   \psStep[StepType=inf,linewidth=1.5pt,linecolor=blue](-3,3){3}{x^2+1}
   \psaxes[Dy=2]{->}(0,0)(-4.7,-2)(4.7,11)[$x$,0][$y$,90]
\end{pspicture}

\end{document}

enter image description here

2

Here's a similar function for Metapost that uses the built-in path bounding box features to find the maximum or minimum of each part of a function curve.

prologues := 3;
outputtemplate := "%j%c.eps";

vardef steps_over(expr p, k) = steps(p,k,true) enddef;
vardef steps_under(expr p, k) = steps(p,k,false) enddef;

vardef steps(expr p, k, over) =
  save xmin, xmax, xstep, ss;
  xmin = xpart point 0 of p;
  xmax = xpart point infinity of p;
  xstep = (xmax-xmin)/k;
  path ss;
  for x = xmin step xstep until xmax-eps:
     hide(ss := p cutbefore (down--up) scaled infinity shifted (x,0)
                  cutafter  (down--up) scaled infinity shifted (x+xstep,0);)
     if x>xmin: & fi
     (x,0) -- if over: ulcorner ss -- urcorner ss 
              else:    llcorner ss -- lrcorner ss fi -- (x+xstep,0)  
  endfor
enddef;

beginfig(1);

u = 8mm;

% axes
path xx, yy; 
xx = (-4.5u,0) -- (4.5u,0);
yy = (0,-u)    -- (0,11u);
drawarrow xx withcolor .5 white; label.rt (btex $x$ etex, point 1 of xx);
drawarrow yy withcolor .5 white; label.top(btex $y$ etex, point 1 of yy);

% define graph path
vardef f(expr x) = 10-x**2 enddef;
-xmin = xmax = 3; s = 0.1;
path ff; ff = ((xmin,f(xmin)) for x=xmin+s step s until xmax+eps: -- (x,f(x)) endfor) scaled u; 

% draw steps and graph
draw steps_over(ff,7) withcolor .67 red; 
draw ff; 

endfig;
end.

enter image description here

Thruston
  • 42,268