I am currently trying to plot tan(x)=x in Matlab but with the y=x portion only crossing the tan(x) at the intersection points.

Here is what I am trying to recreate in Matlab shown in Desmos:

enter image description here

As you can see, I have defined the domain to be $x,\:\left\{n\pi <x<\left(n+\frac{1}{2}\right)\pi \right\}$

Now here is my plot in Matlab:

enter image description here

As you can see, my lines do not cross the tan(x) like it dose in demos, but I cant really understand why. Here is my Matlab code:

f=@(x) tan(x); % defining an anyloums fucntion for tan(x)

%using a for loop to specifically show the y=x a lines of length of the
%given domian
for n=0:1:10;
    hold on;

The reason that I am doing this is because of a question that I have been given which is shown below.

By plotting the functions or otherwise, show that the equation tan( x ) = x has an infinite number of solutions $x_n$ where , $x_n,\:\left\{n\pi \le x_n\:\le \left(n+\frac{1}{2}\right)\pi \right\}$


3 Answers 3


You have an interesting programmation error :

It comes from the fact that you have written


Let us consider for example the case $n=1$. What this instruction generates is

$x=[3.1416 , 4.1416]$

The lower bound is correct, the upper bound isn't (it should be $3\pi/2=4.7124$)

Why that ? Because you have omitted to give a step like $0.01$ in x=n*pi:0.01:(n+1/2)pi.

Thus, what happens ? The default step is one ; thus, what Matlab does is

Begin by the left bound : $3.1416$, OK, then add the step

$3.1416+1=4.1416$ OK because this value is below limit $4.7124$, but,

$3.1416+1+1=5.1416$ is above limit $4.7124$, thus isn't considered.

We are left with the two values $3.1416$ and $4.1416$...

Here is a way to correct your program :

clear all;close all;hold on;
f=@(x) tan(x); 
for n=0:1:3;

You can observe that :

  • the range has been transformed into x=n*pi:0.01:(n+1/2)*pi; as announced.

  • plotting $f$ has been externalized from your loop.

Besides :

You could have used a comma instead of a colon (very valuable remark by @Lutzl) :

x=[n*pi$\color{red}{,}$ (n+0.5)*pi]; y=x; plot(x,y);

I think that you can achieve a better vizualization by reconsidering your issue as the intersection of curves with equations

$$y=\frac{\tan(x)}{x} \ \ \ \ \text{and} \ \ \ \ y=1$$

(see figure below) with the advantage to have points of interest (intersection points) all situated at the same level, closer and closer to the asymptotes with equations $(n+\frac12)\frac{\pi}{2}$.

enter image description here

  • 1
    $\begingroup$ Or construct the interval as list of two points, x=[n*pi, (n+0.5)*pi]; y=x; plot(x,y); instead of drawing a linear function with hundreds of samples. $\endgroup$ Feb 12, 2019 at 8:49
  • $\begingroup$ @Lutzl You are right ! I just realized that ! I was so much in the logic of prooving that the lack of step was the casue that I had forgotten this much better way... $\endgroup$
    – Jean Marie
    Feb 12, 2019 at 8:56

For $n \in \mathbb N$ and $x \in (n\pi, n\pi +\pi/2)$, $\tan x-x$ is continuous, strictly increasing with $\tan n\pi-n\pi< 0$ and $\lim\limits_{x \to (n+1/2)\pi^-} \tan x-x=\infty$. Hence $\tan x-x$ has a unique root on this interval.

This is enough to conclude.

  • $\begingroup$ I not entirely sure I understand what you are saying. Could you maybe expand on this. $\endgroup$
    – james2018
    Feb 11, 2019 at 20:11
  • $\begingroup$ @james2018 What do you don’t understand ? $\endgroup$ Feb 11, 2019 at 20:13
  • $\begingroup$ it the $tan n\pi-n\pi$ and the limt $\endgroup$
    – james2018
    Feb 11, 2019 at 20:23
  • 1
    $\begingroup$ $\tan n\pi=0$ for all $n \in \mathbb N$. So $\tan n\pi -n\pi=-n\pi <0$. And $\tan(n\pi+ x) =\tan x$. $\endgroup$ Feb 11, 2019 at 20:26

The equivalent equation $x=\arctan x+n\pi$ as fixed point iteration maps $\Bbb R$ to $[(n-\tfrac12)\pi,(n+\tfrac12)\pi]$, so as an endomorphism, self-map of the interval there has to be a fixed point. The next step tells that for $n>0$ the fixed point has to be inside the interval $$ x>n\pi+\arctan((n-\tfrac12)\pi)=(n+\tfrac12)\pi-\arctan\left(\frac1{(n-\tfrac12)\pi}\right)>(n+\tfrac12)\pi-\frac1{(n-\frac12)\pi}\\ x<n\pi+\arctan((n+\tfrac12)\pi)=(n+\tfrac12)\pi-\arctan\left(\frac1{(n+\tfrac12)\pi}\right)<(n+\tfrac12)\pi $$ as $\arctan(x)<x$ for $x>0$. So you could draw even smaller intervals.


You must log in to answer this question.

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