5

Please consider the following animation.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\def\f{x^2-x-6}
\psset{unit=3,algebraic}

\def\Atom#1{%
\begin{pspicture}(-1.5,-6.5)(2,-2.5)
    \psplot[linecolor=blue]{-1}{1}{\f}
    \psline[linecolor=green,linewidth=5\pslinewidth](-1,-3.5)(#1,-3.5)
    \psline[linecolor=green,linewidth=5\pslinewidth](0,0|*-1 {\f})(0,0|*#1 {\f})
    \psline[linecolor=orange,linestyle=dashed](#1,-3.5)(*#1 {\f})(0,0|*#1 {\f})
    \pscircle*[linecolor=red](*#1 {\f}){2pt}
    \psaxes[linecolor=lightgray,Oy=-3.50,Dy=.25,Dx=.5]{->}(0,-3.5)(-1.49,-6.49)(1.5,-3)[$x$,0][$y$,90]
\end{pspicture}}

\begin{document}
\multido{\r=-1.0+.125}{17}{\Atom{\r}}
\end{document}

enter image description here

The vertical green line is supposed to be a range of f(x) in -1<=x<=1. When x>.5 I want to prevent the vertical green line (range) from shrinking. My rough idea is as follows, but it will not compile as \ifnum cannot be a fraction.

\ifnum#1>.5
    \psline[linecolor=green,linewidth=5\pslinewidth](0,0|*-1 {\f})(0,0|*.5 {\f})
\else
    \psline[linecolor=green,linewidth=5\pslinewidth](0,0|*-1 {\f})(0,0|*#1 {\f})
\fi

Actually fp package can help but I want to avoid using it. In order to accommodate more general function, I want to buffer the minimum value of f(x) for each iteration in -1<=x<=1 and use this buffer against which the current value of \f is compared.

What do you think?

  • I can also change the #1 to be an integer and manipulate it in the point expression (*{#1 ... 8 div} {\f}) for example. – kiss my armpit Nov 19 '13 at 14:12
4

You can use \ifdim to check for double numbers:

\ifdim#1pt>.5pt
    \psline[linecolor=green,linewidth=5\pslinewidth](0,0|*-1 {\f})(0,0|*.5 {\f})
\else
    \psline[linecolor=green,linewidth=5\pslinewidth](0,0|*-1 {\f})(0,0|*#1 {\f})
\fi

If you want to have it very flexible, you need to accumulate all previous function values. First I tried to save with \pnode the current minimum value, but that doesn't work because you images span over several pages. So you can accumulate the Postscript code of all function values and the determine the minimum when you need it for \psline:

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-node, pst-plot}
\def\f{x^2-x-6}
\psset{unit=3,algebraic}

\makeatletter
\def\minynode@list{}%
\def\addyvalue(#1){%
  \pst@getcoor{#1}\pst@tempA%
  \ifx\minynode@list\@empty
    \xdef\minynode@list{mark }%
  \fi
  \xdef\minynode@list{\minynode@list \pst@tempA \tx@UserCoor exch pop }
}%
\def\getminyvalue{%
  \minynode@list counttomark 1 sub { min } repeat exch pop 0 exch 
}
\makeatother

\def\Atom#1{%
\begin{pspicture}(-1.5,-6.5)(2,-2.5)
    \psplot[linecolor=blue]{-1}{1}{\f}
    \psline[linecolor=green,linewidth=5\pslinewidth](-1,-3.5)(#1,-3.5)
    \addyvalue(0,0|*#1 {\f})
    \psline[linecolor=green,linewidth=5\pslinewidth](0,0|*-1 {\f})(!\getminyvalue)
    \psline[linecolor=orange,linestyle=dashed](#1,-3.5)(*#1 {\f})(0,0|*#1 {\f})
    \pscircle*[linecolor=red](*#1 {\f}){2pt}
    \psaxes[linecolor=lightgray,Oy=-3.50,Dy=.25,Dx=.5]{->}(0,-3.5)(-1.49,-6.49)(1.5,-3)[$x$,0][$y$,90]
\end{pspicture}}

\begin{document}
\multido{\r=-1.0+.125}{17}{\Atom{\r}}
\end{document}

That works, but I'm open for improvements :)

enter image description here

2
\psline[linecolor=green,linewidth=5\pslinewidth]%
       (0,0|*-1 {\f})(*{#1 .5 gt {.5}{#1} ifelse } {\f})
  • I don't understand why pstricks loads pgffor by default but does not load multido. I think multido should be loaded by default as well. – kiss my armpit Nov 19 '13 at 23:41

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.