提问于:
浏览数:
1732
![](https://wenda.latexstudio.net/data/attach/210129/Pfh6o3XU.png)
请教各位大佬,tikz有迭代功能吗?比如数列的蛛网图,一个是y=x+sinx,一个是y=x,关键问题是如何把上一个点的纵坐标转化为下一个点的横坐标?
2 回答
0
找到两个网站:
1、[https://tex.stackexchange.com/questions/153989/draw-a-sequence-on-unimodal-map](https://tex.stackexchange.com/questions/153989/draw-a-sequence-on-unimodal-map)
2、[https://tex.stackexchange.com/questions/45678/creating-cobweb-diagrams-of-some-functions-with-tikz-pstrick-etc](https://tex.stackexchange.com/questions/45678/creating-cobweb-diagrams-of-some-functions-with-tikz-pstrick-etc)
可以参考一下。
-
谢谢,我去看看 – 小鬼 2021-01-29 12:14 回复
0
```c
\def\myfunction#1{#1+sin(#1 r)}%保存一个函数
\def\myactionA#1#2{%#1 是初始值,#2 是一个阈值
%当执行 \myactionA{<num1>}{<num2>} 后,宏 \myinitialvalue, \savevalues 可用
%注意谨慎选择参数 <num1>, <num2>
\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)
你的回答
请登录后回答
你的回答将会帮助更多人,请务必认真回答问题。