我想用 pstricks 绘制如下所示函数的上黎曼和,但是它不能正确取上确界(似乎只是取步骤左值和右值的最大值)。您知道这里出了什么问题以及如何修复它吗?
\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}
答案1
确实,\psStep
仅使用左值和右值,并使用较大的值作为步长。
这是 的修改版本\psStep
,它将每个区间划分为 20 个子区间来计算上确界:
\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}
答案2
使用更新后的版本:
\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}
答案3
以下是类似的函数元帖子使用内置路径边界框特征来查找函数曲线各部分的最大值或最小值。
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.