0

I have been trying to add a plot using Tikzpicture, but it does not locate well in the graph. Here is my code:

\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}

And the result is

enter image description here

All the other parts are okay, but the graph y=1-x/2-sqrt(x(1-3x/4)) isn't where it should be. Obviously it must pass (1/3,1/3) and (1,0) (so make a closed loop with the other two straight lines) but it's way above from its correct location. Furthermore, it is not a straight line but a curve but the result looks like a straight line...

I would really appreciate if you help me out with this trouble!

  • 1
    Welcome to TeX.SE! Please make your code snippet compilable ... – Kurt Sep 17 '17 at 23:02
  • I've just added the packages I used and there was no problem in complying the above code. If it is still not compliable, please let me know – MathPhy Sep 17 '17 at 23:44
2

Your code is almost correct. The only point is the multiplication * between {1-0.5*(\x)+sqrt((\x) and (1-0.75*(\x)))}); that you forgot. You have also put + instead of - in the formulation.

Here is your code and the corresponding plot:

\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}

enter image description here

1

edit: i wrongly down wrote function formula ... instead + before square root it has to be -. corrected now:

i guess that you like to obtain something like this (but i'm not sure)

enter image description here

i rewrote your function and simplify rest of your code:

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.