请教各位大佬,如何用tikz实现迭代的蛛网图?

2021-01-29 11:10发布

![](https://wenda.latexstudio.net/data/attach/210129/Pfh6o3XU.png) 请教各位大佬,tikz有迭代功能吗?比如数列的蛛网图,一个是y=x...

![](https://wenda.latexstudio.net/data/attach/210129/Pfh6o3XU.png) 请教各位大佬,tikz有迭代功能吗?比如数列的蛛网图,一个是y=x+sinx,一个是y=x,关键问题是如何把上一个点的纵坐标转化为下一个点的横坐标?
2条回答
U9430
2021-01-29 22:00
```c \def\myfunction#1{#1+sin(#1 r)}%保存一个函数 \def\myactionA#1#2{%#1 是初始值,#2 是一个阈值 %当执行 \myactionA{}{} 后,宏 \myinitialvalue, \savevalues 可用 %注意谨慎选择参数 , \def\myinitialvalue{#1}%保存初始值 \def\savevalues{}%用于保存纵坐标序列 \def\myempty{} \def\mystoptag{#2}%保存一个阈值 \def\mytempvalueA{#1} \myactionB } \def\myactionB{ \pgfmathparse{\myfunction{\mytempvalueA}} \let\mytempvalueB\pgfmathresult \myactionC } \def\myactionC{ \pgfmathparse{abs(\mytempvalueA-\mytempvalueB)} \pgfmathparse{\pgfmathresult>\mystoptag} \ifnum\pgfmathresult=1\relax \ifx\savevalues\myempty \edef\savevalues{\mytempvalueB} \else \edef\savevalues{\savevalues,\mytempvalueB} \fi \let\mytempvalueA\mytempvalueB \let\mynextaction\myactionB \else \let\mynextaction\relax \fi \mynextaction } \begin{tikzpicture} \draw [-Stealth,thick] (0,0)--(5,0); \draw [-Stealth,thick] (0,0)--(0,5); \myactionA{0.2}{0.05} \draw [red] (\myinitialvalue,0)--++(0,1); \draw plot [domain=0:5,smooth] (\x,{\x+sin(\x r)}); \draw (0,0)--(5,5); \foreach [expand list=true] \i [remember=\i as \j (initially \myinitialvalue)] in {\savevalues} { \draw [->] (\j,\j)--(\j,\i); \draw [->] (\j,\i)--(\i,\i); } \end{tikzpicture} ``` 效果是 ![](https://wenda.latexstudio.net/data/attach/210129/n8LbfPVG.png)

一周热门 更多>