Hey guys!

Recently, a question was asked in stack exchange about a plot that demonstrate **Newton's method in LaTeX**. As an example, I find it interesting to highlight **path intersections in TikZ, **mainly:

- Intersections of perpendicular lines.
- Intersections of named paths.

Moreover, we will learn how to:

- Plot functions in TikZ.
- Create axis and change its units.

## 1. Intersection of perpendicular lines

Assume we have the coordinates of two points A and B, located at (0,0) and (3,3) respectively. We distinguish two cases:

**1) Horizontal line passing through point A and a vertical line passing through point B":**

The coordinates of the point of intersection in TikZ are defined by:

- \verb|(A -| | \verb| B)|: where A and B are the names of the points created using \verb|\coordinate| command or a \verb|node| command (e.g \verb|\coordinate (A) at (0,0);|)
- or \verb|(0,0 -| | \verb| 3,3)|: if we would like to use coordinates directly (coordinates are without parenthesis).

** 2) Vertical line passing through point A and a horizontal line passing through point B":**

The coordinates of the point of intersection in TikZ are defined by:

- \verb|(A | | \verb|- B)| or \verb|(0,0 | | \verb|- 3,3)|.

**Example:**

\documentclass[border=0.2cm]{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture} \draw[fill=brown] (0,0) node[left]{$A$}-- (3,2) node[above]{$B$} -- (3,2 |- 0,0) node[right]{$C$} -- cycle; \end{tikzpicture} \end{document}

The next section presents the interesting part of this tutorial, **how one get the coordinates of intersecting paths**?

## 2. Intersection of paths in TikZ

Intersections of two arbitrary paths in TikZ can be calculated using **intersections library**. This is achieved in two steps:

**Step 1** - Save paths

The first thing that we do is to name paths when we create them using the key **name path={name}** or **name path global={name}. **Here is an example:

\documentclass[border=0.2cm]{standalone} \usepackage{tikz} \usetikzlibrary{intersections} \begin{document} \begin{tikzpicture} % Path 1 \draw[name path={orange line},orange] (0,0) -- (4,2); % Path 2 \draw[name path={violet line},violet] (0,1) -- (4,0); \end{tikzpicture} \end{document}

In this example, we created two paths (straight lines) and we named them according to their colors orange line and violet line.

Using **name path global** allows us to save paths beyond any scopes (not a local path).

**Step 2** - Calculate intersections

After naming paths, we use the key **name intersections={options}** to get intersections. One of these options is to provide the two named paths as follows: **name intersections={of={name1 and name2}}. **By default, coordinates of intersections are named intersection-1, intersection-2, etc. Here is an illustrative example:

\documentclass[border=0.2cm]{standalone} \usepackage{tikz} \usetikzlibrary{intersections} \begin{document} \begin{tikzpicture} % Path 1 \draw[name path={orange line},orange] (0,0) -- (4,2); % Path 2 \draw[name path={violet line},violet] (0,1) -- (4,0); % Intersections \fill[name intersections={of=orange line and violet line}] (intersection-1) circle(2pt); \end{tikzpicture} \end{document}

In this example, we have one intersection between two lines. We used the \fill command to draw a small circle (filled with black) at (intersection-1) which is by default the intersection name.

In the sequel, we will present more options through examples and the reader is invited to check PGF/TikZ documentation for lore details.

### Intersection of a line and a circle

The following LaTeX code draws a circle at the point with coordinates (1,0) and with radius equal to 1.5cm. The second path corresponds to a line drawn from (-1,0) to (4,1).

\documentclass[border=0.2cm]{standalone} \usepackage{tikz} \usetikzlibrary{intersections} \begin{document} \begin{tikzpicture} % Help lines \draw[lightgray] (-2,-2) grid (5,2); % Draw path 1: circle \draw[name path={circle},orange] (1,0) circle(1.5); % Draw path2: line \draw[name path={line},violet] (-1,0) -- (4,1); % Draw small circles at intersections \fill[name intersections={of=circle and line},red] (intersection-1) circle(2pt) (intersection-2) circle(2pt); % Add labels \node[above right] at (intersection-1) {A}; \node[above left] at (intersection-2) {B}; \end{tikzpicture} \end{document}

In this case, the line intersects the circle in two points, named A and B. Coordinates of the first (second) intersection are saved in (intersection-1) ((intersection-2)), respectively.

Labels of these intersections are added using \verb|\node| command.

### Intersection of two circles ("name" option)

By default, intersections are named intersection-1, intersection-2, etc. In this part, we present the key option that allows us to change this prefix it to something different.

Let us say we would like to use the prefix *point* instead of *intersection*. This can be achieved by providing the key **name=point** to the options of **name intersections. **

The following LaTeX code highlights the intersections of two circles:

\documentclass[border=0.2cm]{standalone} \usepackage{tikz} \usetikzlibrary{intersections} \begin{document} \begin{tikzpicture} % Help Lines \draw[lightgray] (-2,-2) grid (5,2); % Circle 1 \draw[name path={circle1}] (1,0) circle(1.5); % Circle 2 \draw[name path={circle2},dashed,gray] (2.5,0) circle(1.5); % Intersections \fill[name intersections={of=circle1 and circle2, name=point},red] (point-1) circle(2pt) (point-2) circle(2pt); % Labels \node[above] at (point-1) {A}; \node[below] at (point-2) {B}; \end{tikzpicture} \end{document}

### Intersection of a circle and a rectangle ( "by" option)

In the previous section, we have changed the prefix of intersections name from *intersection* to *point *as an example. Now, we will change completely the name of intersections (not just the prefix) using the option **by={name1,name2,etc}. **Let us consider the example of intersections between a rectangle and a circle in TikZ.

\documentclass[border=0.2cm]{standalone} \usepackage{tikz} \usetikzlibrary{intersections} \begin{document} \begin{tikzpicture} % Help lines \draw[lightgray] (-2,-2) grid (5,2); % Path 1: rectangle \draw[name path={rectangle}] (-1,-1) rectangle(4,1); % Path 2: circle \draw[name path={circle}] (1.5,0) circle(1.5); % Intersecrtions \fill[name intersections={of=rectangle and circle, by={a,b,c,d}},red] (a) circle(2pt) (b) circle(2pt) (c) circle(2pt) (d) circle(2pt); % Labels \node[above right] at (a) {A}; \node[above left] at (b) {B}; \node[below left] at (c) {C}; \node[below right] at (d) {D}; \end{tikzpicture} \end{document}

The rectangle intersects the circle in four places. Using the **by={a,b,c,d}** option, we saved coordinates of these intersections in (a), (b), (c) and (d).

Despite the fact that we saved these *intersections *in a *predefined names* (a, b, c and d), the default names can also be used (intersection-1, intersection-2, intersection-3 and intersection-4).

### Number of intersections ("total" option)

The number of intersections can be obtained using the option **total **which requires a variable to save the values in it. Let us consider the previous example (intersection of a rectangle and circle):

\documentclass[border=0.2cm]{standalone} \usepackage{tikz} \usetikzlibrary{intersections} \begin{document} \begin{tikzpicture} % Help lines \draw[lightgray] (-2,-2) grid (5,2); % Path 1: rectangle \draw[name path={rectangle}] (-1,-1) rectangle(4,1); % Path 2: circle \draw[name path={circle}] (1.5,0) circle(1.5); % Intersections \fill[name intersections={of=rectangle and circle, name={point},total=\n},red] \foreach \i in {1,...,\n} { (point-\i) circle(2pt) }; \end{tikzpicture} \end{document}

We stored the number of intersections in \verb|\n| and we used a for loop to draw circles at each intersection.

That's all what we need to know to start drawing the **Newton's method in TikZ**. For more details, I invite you again to check the PGF/TikZ documentation.

## 3. Newton's Method in TikZ

**Newton's Method**, known also as** Newton-Raphson method**, is a numerical method for solving an equation f(x)=0. The concept of **Newton's Method** for **finding the roots of equations** can be explored by the next graph.

**Mainly we need to: **

- Create axes with different vector units (due to the quadratic growth of the considered function).
- Plot the function:

F(x)=\frac{1}{2}(x^2-1)

- draw a dashed line starting at x_0=3 and ends at the function plot.
- draw a tangent line that starts at F(x_0) and ends at the intersection of the tangent line and the x-axis.

### Create axes with different units

Let us consider the (0,0) of the illustration the same as the (0,0) of the axes. Hence, we ill draw two perpendicular lines using the **\draw** command:

- x-axis: starts from (-1,0) and ends at (4,0) with an arrowhead and label (above the end point).
- y-axis: starts from (0,-1) and ends at (0,5.5) with an arrowhead and a label (right of the end point).
- Ticks are added using a foreach loop which draws + sign at the corresponding coordinates.

\documentclass[border=0.5cm]{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture}[thick,x=2cm] % Grid \draw[black!10,dashed,xstep=2cm] (-1,-1) grid(5,6); % x-axis \draw[-latex,name path=xaxis] (-1,0) -- (4,0) node[above]{\large $x$}; % y-axis \draw[-latex] (0,-1) -- (0,5.5) node[right]{\large $y$}; % Ticks \foreach \i in {1,2,3} {\node at (\i,0) {$+$};} \foreach \i in {1,2,3,4} {\node at (0,\i) {$+$};} \end{tikzpicture} \end{document}

Comments:

- x-axis unit is equal to 2cm. This is provided as an option to the tikzpicture environment (\verb|x=2cm|).
- The x unit of the grid is changed using \verb|xstep=2cm| to be consistent with the axes.
- The x-axis path is saved as \verb|xaxis|

### Plotting a function in TikZ

As an example, we would like to draw the following function in the domain [0,3.25]:

F(x)=\frac{1}{2}(x^2-1)

which can be achieved by the following line of code:

\draw [orange,name path=Fx] plot[smooth,domain=0:3.25] (\x, {0.5*\x^2-0.5}) node[right](Fx){$F(x)=\frac{1}{2}(x^2-1)$};

- The plot function path is named \verb|Fx|.
- To get a smooth plot, we added \verb|smooth| to the plot options.

It should be noted that we plotted the function in TikZ without using the pgfplots package described in this tutorial "How to plot a function and data in LaTeX".

### Graph of one iteration

Let's start from the initial value x_0=3. We draw a dashed line from the point (3,0) until the point (3,F(3)).

\draw [dashed] (3,4) -- (3,0) node[below=0.1cm] {$x_{0}$} ;

yields:

Then, we draw a tangent line of the function F(x) at the point (3,F(3)).

\draw[violet,name path=Tangent] plot[smooth,domain=1.5:3] (\x, {3*\x-5});

The tangent line will intersect x-axis at the point (x_1,0), which will be calculated using intersections library. Here is the final code of Newton's method:

\documentclass[border=0.5cm]{standalone} \usepackage{tikz} \usetikzlibrary{intersections} \begin{document} \begin{tikzpicture}[thick,x=2cm] % Axes \draw[black!10,dashed,xstep=2cm] (-1,-1) grid(5,6); \draw[-latex,name path=xaxis] (-1,0) -- (4,0) node[above]{\large $x$}; \draw[-latex] (0,-1) -- (0,5.5)node[right]{\large $y$}; \foreach \i in {1,2,3} {\node at (\i,0) {$+$};} \foreach \i in {1,2,3,4} {\node at (0,\i) {$+$};} % Function plot \draw[orange,name path=Fx] plot[smooth,domain=0:3.25] (\x, {0.5*\x^2-0.5}) node[right](Fx){$F(x)=\frac{1}{2}(x^2-1)$}; % Dashed line \draw [dashed] (3,4) -- (3,0) node[below=0.1cm] {$x_{0}$} ; % plot tangent line \draw[violet,name path=Tangent] plot[smooth,domain=1.5:3] (\x, {3*\x-5}); % Intersection \draw [name intersections={of=Tangent and xaxis},fill=gray] (intersection-1) circle(2pt) node[below=0.1cm,fill=white]{$x_{1}$} ; \end{tikzpicture} \end{document}

We have reached the end of this tutorial, I hope you find it useful ❤️❤️❤️!

If you have any remarks or suggestions, feel free to leave me a comment below.