我需要帮助,我有一个 i-2 问题
我致力于此:http://texwelt.de/wissen/fragen/4912/skizze-zur-illustration-linearer-regression
我正在尝试做这样的事情
\documentclass[12pt,oneside,fleqn]{book}
\usepackage{tikz}
\usetikzlibrary{arrows,intersections}
\tikzpicture[
thick,
>=stealth',
dot/.style = {
draw,
fill=white,
circle,
inner sep=1pt,
minimum size=7pt
}
]
\coordinate (O) at (0,0);
\draw[->] (-0.3,0) -- (8,0) coordinate[label={below:$t$}] (xmax);
\draw[->] (0,-0.3) -- (0,7) coordinate[label={right:$y$}] (ymax);
\path[name path=x] (0,0) -- (7,7);
\path[name path=y] plot[smooth] coordinates {(-0.3,2) (2,1.5) (4,2.8) (6,5)};
\scope[name intersections={of=x and y,name=i}]
\draw (0,0) -- (5,5) node[pos=1,below right] {f(t)=t};
\draw (i-1) node[dot](i-1) {} -- node[left] {} (i-1 |- O) node[dot,label={below:$(r-1)!$}] {};
\path (i-2) node[dot,label={above:$Q$}] (i-2) {} -- (i-2 |- i-1) node[dot] (i-12) {};
% \draw (i-2) -- (i-2 |- O) node[dot,label={below:$x_0 + \varepsilon$}] {};
%\draw[blue,<->] (i-2) -- node[right] {$f(x_0 + \varepsilon) - f(x_0)$} (i-12);
%\draw[blue,<->] (i-1) -- node[below] {$\varepsilon$} (i-12);
%\path (i-1 |- O) -- node[below] {$\varepsilon$} (i-2 |- O);
%\draw[gray] (i-2) -- (i-2 -| xmax);
%\draw[gray,<->] ([xshift=-0.5cm]i-2 -| xmax) -- node[fill=white] {$f(x_0 + \varepsilon)$} ([xshift=-0.5cm]xmax);
\end{scope}
\end{tikzpicture}
答案1
我已经回答了LaTeX 社区论坛,但也要在此处复制代码。它依赖于简单函数 f(t)=t,这使得计算交点实际上没有必要。
- 画出轴线
\foreach
循环绘制所有点和连接quotes
使用库语法添加标签节点
\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{arrows,quotes}
\begin{document}
\begin{tikzpicture}[
thick,
>=stealth',
empty dot/.style = { circle, draw, fill = white!0,
inner sep = 0pt, minimum size = 4pt },
filled dot/.style = { empty dot, fill = black}
]
\def\r{3}
\draw[->] (-0.5,0) -- (6,0) coordinate[label = {below:$t$}] (xmax);
\draw[->] (0,-0.5) -- (0,6) coordinate[label = {left:$y$}] (ymax);
\draw [dashed] (0,0) -- (5,5);
\foreach \i in {\r+1,\r,\r-1} {
\draw [densely dashed] (\i,\i) -- (\i+1,\i);
\draw [densely dashed] (\i,\i) -- (\i,0);
\draw [densely dashed] (\i+1,\i) -- (\i+1,0);
\node [filled dot] at (\i,\i) {};
\node [empty dot] at (\i+1,\i) {};
}
\node ["above right:$f(t)=t$"] at (5,5) {};
\node ["above right:$\phi(t)$"] at (\r+2,\r+1) {};
\node ["below:$(r-1)!$"] at (\r-1,0) {};
\node ["below:$\phantom{()}r!$"] at (\r,0) {};
\node ["below:$(r+1)!$"] at (\r+1,0) {};
\end{tikzpicture}
\end{document}
LC 问题链接:使用 TikZ 绘制图表。
答案2
因此坐标(i-2)
将是名为x
和的路径的第二个交点y
。您为路径编写的代码生成两个仅相交一次的图,因此未定义(i-2)
,如下所示(x
红色和y
蓝色)。
用创建第二个坐标的某个坐标替换最后一个坐标(在本例中使用(4.5,7)),然后取消注释后面的一些代码,以演示(i-2)
坐标的工作原理。
最后对其余代码进行了一些调整,我认为这是理想的结果
这是用以下代码生成的
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,intersections}
\begin{document}
\tikzpicture[
thick,
>=stealth',
dot/.style = {
draw,
fill=white,
circle,
inner sep=1pt,
minimum size=7pt
}
]
\coordinate (O) at (0,0);
\draw[->] (-0.3,0) -- (8,0) coordinate[label={below:$t$}] (xmax);
\draw[->] (0,-0.3) -- (0,7) coordinate[label={right:$y$}] (ymax);
\path[name path=x] (0,0) -- (7,7);
\path[name path=y] plot [smooth] coordinates {(-0.3,2) (2,1.5) (4,2.8) (4.5,7)};
\scope[name intersections={of=x and y,name=i}]
\draw (0,0) -- (5,5) node[pos=1,below right] {f(t)=t};
\draw (i-1) node[dot](i-1) {} -- node[left] {} (i-1 |- O) node[dot,label={below:$(r-1)!$}] {};
\draw (i-2) node[dot,label={above:$Q$}] {} -- (i-2) -- (i-2 |- i-1) node[dot,label={}] (i-12) {};
\draw (i-2) -- (i-2 |- O) node[dot,label={below:$x_0 + \varepsilon$}] {};
\draw[blue,<->] (i-2) -- (i-12) node[right] {$f(x_0 + \varepsilon) - f(x_0)$};
\draw[blue,<->] (i-1) -- (i-12) node[below] {$\varepsilon$};
\path (i-1 |- O) -- node[below] {$\varepsilon$} (i-2 |- O);
\draw[gray] (i-2) -- (i-2 -| xmax);
\draw[gray,<->] ([xshift=-0.5cm]i-2 -| xmax) -- node[fill=white] {$f(x_0 + \varepsilon)$} ([xshift=-0.5cm]xmax);
\endscope
\endtikzpicture
\end{document}