如何使用 PStricks 在 f(x) 的給定點 (x,y) 繪製任意函數 y=f(x) 的切線?

如何使用 PStricks 在 f(x) 的給定點 (x,y) 繪製任意函數 y=f(x) 的切線?

我對 PStricks 的使用相對較新,並且不知道如何在曲線的給定點繪製 y=f(x) 的切線。我知道,透過簡單的數學計算,可以透過在點 [(x,y), dx, dy] 處建構關聯微分三角形來完成。但是,我認為 PSricks 應該有一個「簡單的方法」來自動完成它。拜託,你能幫我嗎?

答案1

Pstricks 由多個包組成。基本的是pstricks.

所有可用軟體包的完整清單以及簡短的描述位於tug.org/PStricks

要繪製函數,pst-plot建議使用該套件。它提供了命令\psplot

\psplotTangent透過套件提供的命令可以輕鬆地繪製給定函數的切線pstricks-add

這是文檔的範例:

\documentclass[pstricks]{standalone}
\usepackage{pstricks,pst-plot,pstricks-add}

\begin{document}
\def\F{x RadtoDeg dup dup cos exch 2 mul cos add exch 3 mul cos add}
\def\Fp{x RadtoDeg dup dup sin exch 2 mul sin 2 mul add exch 3 mul sin 3 mul add neg}
\psset{plotpoints=1001}
\begin{pspicture}(-7.5,-2.5)(7.5,4)%X\psgrid
\psaxes{->}(0,0)(-7.5,-2)(7.5,3.5)
\psplot[linewidth=3\pslinewidth]{-7}{7}{\F}
\psset{linecolor=red, arrows=<->, arrowscale=2}
\multido{\n=-7+1}{8}{\psplotTangent{\n}{1}{\F}}
\psset{linecolor=magenta, arrows=<->, arrowscale=2}%
\multido{\n=0+1}{8}{\psplotTangent[linecolor=blue, Derive=\Fp]{\n}{1}{\F}}
\end{pspicture}

\end{document}

在此輸入影像描述

答案2

我下面的答案將中綴版本添加到 Marco Daniel 的答案中,並提供一些易於自訂的設定作為模板。

\documentclass[pstricks,border=0bp,12pt,dvipsnames]{standalone}
\usepackage{pstricks-add}

\usepackage[nomessages]{fp}

\FPset\TrigLabelBase{4}
\FPeval\XMin{0-pi}
\FPeval\XMax{2*pi}
\FPset\YMin{-3}
\FPset\YMax{3}

\FPeval\DeltaX{pi/TrigLabelBase}
\FPeval\DeltaY{1}

\FPeval\Left{XMin-DeltaX/2}
\FPeval\Right{XMax+DeltaX/2}
\FPeval\Bottom{YMin-DeltaY/4}
\FPeval\Top{YMax+DeltaY/4}

\newlength\Width\Width=12cm
\newlength\Height\Height=6cm

\newlength\urx\urx=15pt
\newlength\ury\ury=15pt
\newlength\llx\llx=-5pt
\newlength\lly\lly=-5pt



\psset
{
    algebraic,
    urx=\urx,
    ury=\ury,
    llx=\llx,
    lly=\lly,
    plotpoints=1000,
    trigLabels,
    trigLabelBase=\TrigLabelBase,
    xAxisLabel=$x$,
    yAxisLabel=$y$,
    tickcolor=gray,
    ticksize=0 -4pt,
    labelFontSize=\scriptstyle,
}



% the same as \sum_{i=1}^{3} \frac{\cos(i x)}{i},
% the third arg represent increment step,
\def\f{Sum(i,1,1,3,cos(i*x)/i)}% is the same as \def\f{cos(x)+cos(2*x)/2+cos(3*x)/3}


% the first derivative of \f
\def\fp{Derive(1,\f)}



\begin{document}

\begin{psgraph}[dx=\DeltaX,dy=\DeltaY,linecolor=gray]{->}(0,0)(\Left,\Bottom)(\Right,\Top){\dimexpr\Width-\urx+\llx}{!}%{\dimexpr\Height-\ury+\lly}
    \psplot[linecolor=NavyBlue]{\XMin}{\XMax}{\f}
    \pstVerb{/xxx {Pi 4 div} def}%
    \psset{arrows=<->}
    \psplotTangent[linecolor=ForestGreen]{xxx}{3}{\f}% tangent line
    \psplotTangent[linecolor=Maroon,Derive={-1/\fp}]{xxx}{3}{\f}% normal line
\end{psgraph}

\end{document}

在此輸入影像描述

相關內容