6

I have 'written' the following code to get the attached diagram but am unable to draw horizontal lines above the even notes, i.e. x-2j and x-2n as well as x-0 and x-2. This was extremely straight forward for the composite trapezoidal rule, but I'm lost now for this way.

Can someone tell me what I have to do to get the correct image?

enter image description here

Here is the code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
\begin{figure}[h!]
\caption{Composite Midpoint Rule}
\begin{tikzpicture}[scale=1.0,transform shape]
\def\basic{%
    \draw[very thick, ->] (-0.3,0) -- (12,0) node[right] {$x$} coordinate (x axis);
    \draw[very thick, ->] (0,-0.3) -- (0,3) node[above] {$y$};
    \draw [name path=curve] (0.5,2.2) .. controls (1.5,4.4) and (9.6,0.2) .. (11.5,2.5);

    \path [name path=line 1] (1.0,0) -- (1.0,3);
    \path [name path=line 2] (1.5,0) -- (1.5,3);
    \path [name path=line 3] (2.0,0) -- (2.0,3);
    \path [name path=line 4] (2.5,0) -- (2.5,3);
    \path [name path=line 5] (3.0,0) -- (3.0,3);

    \path [name path=line 6] (5.6,0) -- (5.6,3);
    \path [name path=line 7] (6.5,0) -- (6.5,3);
    \path [name path=line 8] (7.4,0) -- (7.4,3);

    \path [name path=line 9] (9.3,0) -- (9.3,3);
    \path [name path=line 10] (10.1,0) -- (10.1,3);
    \path [name path=line 11] (11,0) -- (11,3); 


    \path [name intersections={of=curve and line 1, by={a}}];
    \path [name intersections={of=curve and line 2, by={b}}];
    \path [name intersections={of=curve and line 3, by={c}}];
    \path [name intersections={of=curve and line 4, by={d}}];
    \path [name intersections={of=curve and line 5, by={e}}];
    \path [name intersections={of=curve and line 6, by={f}}];
    \path [name intersections={of=curve and line 7, by={g}}];
    \path [name intersections={of=curve and line 8, by={h}}];
    \path [name intersections={of=curve and line 9, by={i}}];
    \path [name intersections={of=curve and line 10, by={j}}];
    \path [name intersections={of=curve and line 11, by={k}}];

    \draw[dashed] (1,0) node[below] {$x_{-1}$} -- (a);
    \draw[dashed] (1.5,0) node[below] {$x_0$} -- (b);
    \draw[dashed] (2,0) node[below] {$x_1$} -- (c);
    \draw[dashed] (2.5,0) node[below] {$x_2$} -- (d);
    \draw[dashed] (3,0) node[below] {$x_3$} -- (e);

    \draw[dashed] (5.6,0) node[below] {$x_{2j - 1}$} -- (f);
    \draw[dashed] (6.5,0) node[below] {$x_{2j}$} -- (g);
    \draw[dashed] (7.4,0) node[below] {$x_{2j+1}$} -- (h);

    \draw[dashed] (9.3,0) node[below] {$x_{2n-1}$} -- (i);
    \draw[dashed] (10.1,0) node[below] {$x_{2n}$} -- (j);
    \draw[dashed] (11,0) node[below] {$x_{2n+1}$} -- (k);

}
\basic
%\node[above=10pt] at (k) {$\boxed{f(x)}$};
\end{tikzpicture} \end{figure}


\end{document}
2
  • Welcome to TeX.SE! I made your code into a complete MWE :) welcome to the group!
    – cmhughes
    Oct 27, 2012 at 3:51
  • Thanks, My bad. I'll make sure to follow that convention in future postings :D Oct 27, 2012 at 4:02

2 Answers 2

6

enter image description here

Suppose you want to connect the nodes (f) (2j-1), (g) (2j), and (h) (2j+1), where g is the middle node (as shown in the orange line above):

\draw[dashed, red]  (5.6,0) node[below] {$x_{2j - 1}$} -- (f);
\draw[dashed]       (6.5,0) node[below] {$x_{2j}$}     -- (g);
\draw[dashed, blue] (7.4,0) node[below] {$x_{2j+1}$}   -- (h);

We need a horizontal line that extends from (g) to the other two nodes. So, first "draw " a intersection of a horizontal line from (g) to the y-axis:

\path[name path=Horizontal Line at g] (g) -| (0,0);

compute the intersection of this line with the line line 6:

\path [name intersections={of=Horizontal Line at g and line 6, by={f'}}];

and label that intersection (f'). Then we just want a horizontal line from (h) to (g) and extend it to (f'):

\draw[ultra thick, orange] (h) |- (g) -| (f');

Notes:

  • The code could use some clean up, but I'll do that after I know that this is what you are looking for. It was not clear from your description what you wanted to show. I have added color to the two lines to show which ones I was trying to connect.

Code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
\begin{figure}[h!]
\caption{Composite Midpoint Rule}
\begin{tikzpicture}[scale=1.0,transform shape]
\def\basic{%
    \draw[very thick, ->] (-0.3,0) -- (12,0) node[right] {$x$} coordinate (x axis);
    \draw[very thick, ->] (0,-0.3) -- (0,3) node[above] {$y$};
    \draw [name path=curve] (0.5,2.2) .. controls (1.5,4.4) and (9.6,0.2) .. (11.5,2.5);

    \path [name path=line 1] (1.0,0) -- (1.0,3);
    \path [name path=line 2] (1.5,0) -- (1.5,3);
    \path [name path=line 3] (2.0,0) -- (2.0,3);
    \path [name path=line 4] (2.5,0) -- (2.5,3);
    \path [name path=line 5] (3.0,0) -- (3.0,3);

    \path [name path=line 6] (5.6,0) -- (5.6,3);
    \path [name path=line 7] (6.5,0) -- (6.5,3);
    \path [name path=line 8] (7.4,0) -- (7.4,3);

    \path [name path=line 9] (9.3,0) -- (9.3,3);
    \path [name path=line 10] (10.1,0) -- (10.1,3);
    \path [name path=line 11] (11,0) -- (11,3); 


    \path [name intersections={of=curve and line 1, by={a}}];
    \path [name intersections={of=curve and line 2, by={b}}];
    \path [name intersections={of=curve and line 3, by={c}}];
    \path [name intersections={of=curve and line 4, by={d}}];
    \path [name intersections={of=curve and line 5, by={e}}];
    \path [name intersections={of=curve and line 6, by={f}}];
    \path [name intersections={of=curve and line 7, by={g}}];
    \path [name intersections={of=curve and line 8, by={h}}];
    \path [name intersections={of=curve and line 9, by={i}}];
    \path [name intersections={of=curve and line 10, by={j}}];
    \path [name intersections={of=curve and line 11, by={k}}];

    \draw[dashed] (1,0) node[below] {$x_{-1}$} -- (a);
    \draw[dashed] (1.5,0) node[below] {$x_0$} -- (b);
    \draw[dashed] (2,0) node[below] {$x_1$} -- (c);
    \draw[dashed] (2.5,0) node[below] {$x_2$} -- (d);
    \draw[dashed] (3,0) node[below] {$x_3$} -- (e);

    \draw[dashed, red] (5.6,0) node[below] {$x_{2j - 1}$} -- (f);
    \draw[dashed] (6.5,0) node[below] {$x_{2j}$} -- (g);
    \draw[dashed, blue] (7.4,0) node[below] {$x_{2j+1}$} -- (h);

    \draw[dashed] (9.3,0) node[below] {$x_{2n-1}$} -- (i);
    \draw[dashed] (10.1,0) node[below] {$x_{2n}$} -- (j);
    \draw[dashed] (11,0) node[below] {$x_{2n+1}$} -- (k);

    \path[name path=Horizontal Line at g] (g) -| (0,0);
    \path [name intersections={of=Horizontal Line at g and line 6, by={f'}}];
    \draw[ultra thick, orange] (h) |- (g) -| (f');
}
\basic
%\node[above=10pt] at (k) {$\boxed{f(x)}$};
\end{tikzpicture} \end{figure}

\end{document}

Code Simplification 1:

You can use a \foreach loop to simplify your code. The second \foreach here is from Gonzalo Medina's comment -- not sure how long it would have taken me to figure that one out.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
\begin{figure}[h!]
\caption{Composite Midpoint Rule}
\begin{tikzpicture}[scale=1.0,transform shape]
\def\basic{%
    \draw[very thick, ->] (-0.3,0) -- (12,0) node[right] {$x$} coordinate (x axis);
    \draw[very thick, ->] (0,-0.3) -- (0,3) node[above] {$y$};
    \draw [name path=curve] (0.5,2.2) .. controls (1.5,4.4) and (9.6,0.2) .. (11.5,2.5);

    \path [name path=line 1] (1.0,0) -- (1.0,3);
    \path [name path=line 2] (1.5,0) -- (1.5,3);
    \path [name path=line 3] (2.0,0) -- (2.0,3);
    \path [name path=line 4] (2.5,0) -- (2.5,3);
    \path [name path=line 5] (3.0,0) -- (3.0,3);

    \path [name path=line 6] (5.6,0) -- (5.6,3);
    \path [name path=line 7] (6.5,0) -- (6.5,3);
    \path [name path=line 8] (7.4,0) -- (7.4,3);

    \path [name path=line 9] (9.3,0) -- (9.3,3);
    \path [name path=line 10] (10.1,0) -- (10.1,3);
    \path [name path=line 11] (11,0) -- (11,3); 

% Can replace all these with a \foreach
    %    \path [name intersections={of=curve and line 1, by={a}}];
    %    ...
    %    \path [name intersections={of=curve and line 11, by={k}}];

    %\foreach \Line in {1,...,11} {% For this to work needs some expandafter magic
    %    \path [name intersections={of=curve and line \Line, by={\alph{\Line}}}];
    %}
    \foreach \Line/\Name in {1/a, 2/b, 3/c, 4/d, 5/e, 6/f, 7/g, 8/h, 9/i, 10/j, 11/k} {% 
        \path [name intersections={of=curve and line \Line, by={\Name}}];
    }

% Can replace all these with a \foreach
    %    \draw[dashed] (1,0)   node[below] {$x_{-1}$} -- (a);
    %    ...
    %    \draw[dashed] (11,0)   node[below] {$x_{2n+1}$} -- (k);
    \foreach \point/\label in 
        {a/-1, b/0, c/1, d/2, e/3, f/2j-1, g/2j, h/2j+1, i/2n-1, j/2n, k/2n+1} 
            \draw[dashed] (\point|-0,0) node[below] {$x_{\label}$} -- (\point);

% Draw the desired lines
    \path[name path=Horizontal Line at g] (g) -| (0,0);
    \path[name intersections={of=Horizontal Line at g and line 6, by={f'}}];
    \draw[ultra thick, orange] (h) |- (g) -| (f');
}
\basic
%\node[above=10pt] at (k) {$\boxed{f(x)}$};
\end{tikzpicture} \end{figure}

\end{document}

Code Simplification 2:

And using another \foreach you can reduce the code even further, and a few minor (my personal preferences of the arrow tips, and line styles) tweaks:

enter image description here

Notes:

  • This was done as a separate MWE so as to make it easier to follow the evolution from the given code to this.

Code

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
\begin{figure}[h!]
\caption{Composite Midpoint Rule}
\begin{tikzpicture}[scale=1.0,transform shape]
\def\basic{%
    \draw[very thick, -stealth] (-0.3,0) -- (12,0) node[right] {$x$};% x-axis
    \draw[very thick, -stealth] (0,-0.3) -- (0,3)  node[above] {$y$};% y-axis

    \draw [name path=curve, thick, blue] (0.5,2.2) .. controls (1.5,4.4) and (9.6,0.2) .. (11.5,2.5);% curve

    \foreach \x/\Point [count=\xi] in {1.0/a, 1.5/b, 2.0/c, 2.5/d, 3.0/e, 5.6/f, 6.5/g, 7.4/h, 9.3/i, 10.1/j, 11/k } {%
        \path [name path global=line \xi, red] (\x,0) -- (\x,3);
        \path [name intersections={of=curve and line \xi, by={\Point}}];
    }%

    \foreach \Line/\Name in {1/a, 2/b, 3/c, 4/d, 5/e, 6/f, 7/g, 8/h, 9/i, 10/j, 11/k} {% 
        \path [name intersections={of=curve and line \Line, by={\Name}}];
    }

    \foreach \point/\label in 
        {a/-1, b/0, c/1, d/2, e/3, f/2j-1, g/2j, h/2j+1, i/2n-1, j/2n, k/2n+1} 
            \draw[densely dotted, thick] (\point|-0,0) node[below] {$x_{\label}$} -- (\point);

% Draw the desired lines
    \path[name path=Horizontal Line at g] (g) -| (0,0);
    \path[name intersections={of=Horizontal Line at g and line 6, by={f'}}];
    \draw[ultra thick, orange] (h) |- (g) -| (f');
}
\basic
%\node[above=10pt] at (k) {$\boxed{f(x)}$};
\end{tikzpicture} \end{figure}

\end{document}
3
  • Exactly. Let me digest the changes you made :D Oct 27, 2012 at 3:58
  • @ Gonzalo, thanks, I did that and it worked, but I went back to the other version just because I understand it better and I'm just beginning to use Tikz. I'll get there though! Oct 27, 2012 at 4:07
  • @GonzaloMedina: Perhaps you should un-delete your revised answer as mine does not use the perpendicular coordinate system. Oct 27, 2012 at 5:02
4

Another possibility, using the perpendicular coordinate system without calculating more intersections:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections,calc}

\begin{document}

\begin{figure}[h!]
\caption{Composite Midpoint Rule}
\begin{tikzpicture}[scale=1.0,transform shape]
\def\basic{%
    \draw[very thick, ->] (-0.3,0) -- (12,0) node[right] {$x$} coordinate (x axis);
    \draw[very thick, ->] (0,-0.3) -- (0,3) node[above] {$y$};
    \draw [name path=curve] (0.5,2.2) .. controls (1.5,4.4) and (9.6,0.2) .. (11.5,2.5);

    \path [name path=line 1] (1,0,0) -- (1.0,3);
    \path [name path=line 2] (1.5,0) -- (1.5,3);
    \path [name path=line 3] (2.0,0) -- (2.0,3);
    \path [name path=line 4] (2.5,0) -- (2.5,3);
    \path [name path=line 5] (3.0,0) -- (3.0,3);

    \path [name path=line 6] (5.6,0) -- (5.6,3);
    \path [name path=line 7] (6.5,0) -- (6.5,3);
    \path [name path=line 8] (7.4,0) -- (7.4,3);

    \path [name path=line 9] (9.3,0) -- (9.3,3);
    \path [name path=line 10] (10.1,0) -- (10.1,3);
    \path [name path=line 11] (11,0) -- (11,3); 

    % naming the intersections
    \foreach \name [count=\i from 1] in {a,b,...,k}
      \path [name intersections={of=curve and line \i, by={\name}}];

    % vertical lines
    \foreach \point/\label in {a/-1,b/0,c/1,d/2,e/3,f/2j-1,g/2j,h/2j+1,i/2n-1,j/2n,k/2n+1}
      \draw[dashed] (\point|-0,0) node[below] {$x_{\label}$} -- (\point);

    % horizontal lines
      \draw[ultra thick,red!60!black]  (a) |- (a|-b) -- (b) -- (c|-b);
      \draw[ultra thick,red!60!black]  (c|-d) -- (d) -- (e|-d) -| (e);
      \draw[ultra thick,red!60!black]  (f|-g) -- (g) -- (h|-g) -| (h);
      \draw[ultra thick,red!60!black]  (i|-j) -- (j) -- (k|-j);
}
\basic
%\node[above=10pt] at (k) {$\boxed{f(x)}$};
\end{tikzpicture} \end{figure}

\end{document}

enter image description here

2
  • Note exactly but thats a really good start. What i was looking for was, for instance, ton connect the height of x_2j to the vertical line of x_2j-1 and the height of x_2j+1. Does that make any sense? Oct 27, 2012 at 3:55
  • @Robert see my updated answer. Oct 27, 2012 at 17:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .