Tikzpicture:情节并不在应有的位置

Tikzpicture:情节并不在应有的位置

我一直在尝试使用 Tikzpicture 添加绘图,但它在图形中的位置不太好。这是我的代码:

\usepackage{tikz}
\usetikzlibrary{calc}
\begin{figure}[h]
\centering
\begin{tikzpicture}[scale=2]
\draw[step=1, gray, dashed, very thin] (-0.2,-0.2) grid (2.2,2.2); 
\draw[->] (-0.2,0) -- (2.2,0) node[right] {$X_1$}; 
\foreach \x/\xtext in {1/, 2} 
\draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north,fill=white]     {$\xtext$};
\draw[->] (0,-0.2) -- (0,2.2) node[above] {$X_2$};
\foreach \y/\ytext in {1, 2} 
\draw (1pt,\y cm) -- (-4pt,\y cm) node[anchor=north,fill=white] {$\ytext$};
\draw[blue, dashed, thick] (1/3,1/3) -- (0,0) -- (1,0);
\draw[blue, dashed, thick, domain=1/3:1] plot (\x, {1-0.5*(\x)+sqrt((\x)(1-0.75*(\x)))});
\draw[blue, dashed, thick] (5/3,5/3) -- (2,2) -- (1,2);
\draw[dashed,very thin] (0,0) -- (2,2);
\draw[red, fill=red, opacity=0.2] (3/7,1/7) -- ($(0,1)+sqrt(14)*(0,3/14)$) -- ($(0,1)-sqrt(14)*(0,3/14)$) -- cycle;
\draw[red, fill=red, opacity=0.2] (3/7,1/7) -- ($(1,0)+sqrt(14)*(1/7,0)$) -- ($(1,0)-sqrt(14)*(1/7,0)$) -- cycle;
\draw[red, fill=red, opacity=0.2] (11/7,13/7) -- ($(2,1)-sqrt(14)*(0,3/14)$) -- ($(2,1)+sqrt(14)*(0,3/14)$) -- cycle;
\draw[red, fill=red, opacity=0.2] (11/7,13/7) -- ($(1,2)-sqrt(14)*(1/7,0)$) -- ($(1,2)+sqrt(14)*(1/7,0)$) -- cycle;
\end{tikzpicture}
\end{figure}

结果是

在此处输入图片描述

所有其他部分都很好,但图形 y=1-x/2-sqrt(x(1-3x/4)) 不在应在的位置。显然它必须经过 (1/3,1/3) 和 (1,0)(因此与其他两条直线形成一个闭环),但它远远超出了正确位置。此外,它不是直线而是曲线,但结果看起来像一条直线……

如果您能帮助我解决这个问题我将非常感激!

答案1

您的代码几乎是正确的。唯一的问题是您忘记了和*之间的乘法。您还在公式中用了代替。{1-0.5*(\x)+sqrt((\x)(1-0.75*(\x)))});+-

这是您的代码和相应的情节:

\documentclass[a4paper, 11pt]{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{figure}[h]
\centering
\begin{tikzpicture}[scale=2]
\draw[step=1, gray, dashed, very thin] (-0.2,-0.2) grid (2.2,2.2); 
\draw[->] (-0.2,0) -- (2.2,0) node[right] {$X_1$}; 
\foreach \x/\xtext in {1/, 2} 
\draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north,fill=white]     {$\xtext$};
\draw[->] (0,-0.2) -- (0,2.2) node[above] {$X_2$};
\foreach \y/\ytext in {1, 2} 
\draw (1pt,\y cm) -- (-4pt,\y cm) node[anchor=north,fill=white] {$\ytext$};
\draw[blue, dashed, thick] (1/3,1/3) -- (0,0) -- (1,0);
\draw[blue, dashed, thick, domain=1/3:1] plot (\x, {1-(0.5)*(\x)-sqrt((\x)*(1-(0.75)*(\x)))});
\draw[blue, dashed, thick] (5/3,5/3) -- (2,2) -- (1,2);
\draw[dashed,very thin] (0,0) -- (2,2);
\draw[red, fill=red, opacity=0.2] (3/7,1/7) -- ($(0,1)+sqrt(14)*(0,3/14)$) -- ($(0,1)-sqrt(14)*(0,3/14)$) -- cycle;
\draw[red, fill=red, opacity=0.2] (3/7,1/7) -- ($(1,0)+sqrt(14)*(1/7,0)$) -- ($(1,0)-sqrt(14)*(1/7,0)$) -- cycle;
\draw[red, fill=red, opacity=0.2] (11/7,13/7) -- ($(2,1)-sqrt(14)*(0,3/14)$) -- ($(2,1)+sqrt(14)*(0,3/14)$) -- cycle;
\draw[red, fill=red, opacity=0.2] (11/7,13/7) -- ($(1,2)-sqrt(14)*(1/7,0)$) -- ($(1,2)+sqrt(14)*(1/7,0)$) -- cycle;
\end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

答案2

编辑:我错误地把函数公式写+在了平方根之前-。现在更正:

我猜你喜欢获得这样的东西(但我不确定)

在此处输入图片描述

我重写了你的函数并简化了其余代码:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{calc}

\begin{document}
    \begin{tikzpicture}[scale=2]
\draw[step=1, gray, dashed, very thin] (0,0) grid (2.1,2.1);
\draw[->] (-1pt,0) -- (2.1,0) node[right] {$X_1$};
\draw[->] (0,-1pt) -- (0,2.1) node[above] {$X_2$};
\foreach \i in {0, 1, 2}
{
\draw (\i,1pt) -- ++ (0,-2pt) node[below,font=\footnotesize] {$\i$};
\draw (1pt,\i) -- ++ (-2pt,0) node[left ,font=\footnotesize] {$\i$};
}
\draw[blue, dashed, thick] (1/3,1/3) -- (0,0) -- (1,0);
\draw[blue, dashed, thick, domain=1/3:1] plot (\x, {1-0.5*\x-sqrt(\x-0.75*\x*\x)});
\draw[blue, dashed, thick] (5/3,5/3) -- (2,2) -- (1,2);
\draw[dashed,very thin] (0,0) -- (2,2);

\draw[red, fill=red, opacity=0.2]
    ($(0,1)-sqrt(14)*(0,3/14)$) -- ($(0,1)+sqrt(14)*(0,3/14)$) --
    ($(1,0)-sqrt(14)*(1/7, 0)$) -- ($(1,0)+sqrt(14)*(1/7, 0)$) -- cycle;
\draw[red, fill=red, opacity=0.2]
    ($(2,1)-sqrt(14)*(0,3/14)$) -- ($(2,1)+sqrt(14)*(0,3/14)$) --
    ($(1,2)-sqrt(14)*(1/7, 0)$) -- ($(1,2)+sqrt(14)*(1/7, 0)$) -- cycle;
    \end{tikzpicture}
\end{document}

相关内容