I have this function

f[x_] := ((2 x  Cos[4 x] )/(1 + (x^2) ) + Sqrt[
     Cot[3 x]^4 - (Cos[4 x]^2) (Csc[3 x]^2) ])/(4 /(x^2)  + 
     Cot[3 x]^2);

where $0<x<5$. I want to plot the Real and Imag part of this function. I use the following codes:

Plot[Re[f[x]], {x, 0, 5}]
Plot[Im[f[x]], {x, 0, 5}]
Plot[f[x], {x, 0, 5}]

I am confused with these plots. According the first plot, the function is always real-valued, but according to the second and the third one, there are some imaginary parts as well. could someone explain this behavior? Does the third plot indicate that the real part of this function is discontinuous? or should I consider the first plot?

  • $\begingroup$ It is more idiomatic to use f[x_] := ... to define your function. And then use f[x] in the other places. The third Plot seems to be plotting Abs[f[x]] (I am not sure if this is correct, it seems odd to me). $\endgroup$ Mar 26, 2020 at 17:03
  • $\begingroup$ If you have f=x^2-I*x^2 and you Plot[Re[f],{x,-2,2}] it shows you the positive real x^2 and you Plot[Im[f],{x,-2,2}] it shows you the negative imaginary -x^2. How do you think the first plot shows you that f is only real? You are asking it to only show you the real part. You can also try Plot[{Re[f],Im[f]},{x,-2Pi,2Pi},PlotPoints->1000] on your original f to see the real and imaginary in two colors on top of each other. $\endgroup$
    – Bill
    Mar 26, 2020 at 17:34
  • $\begingroup$ @Bill In case of your example, real and imaginary parts are clearly separated. So, what is your idea about finding the real part of my function? $\endgroup$
    – user70481
    Mar 26, 2020 at 17:42
  • $\begingroup$ Re tries to take the points to be plotted and discards the imaginary part, whether the real and imaginary parts are clearly separated or clearly combined or even hidden from you, it does the work throw away any imaginary part. $\endgroup$
    – Bill
    Mar 26, 2020 at 17:53
  • $\begingroup$ FunctionDomain[{f[x], 0 < x < 5}, x] will show the intervals within 0 < x < 5 where the function is purely real. $\endgroup$
    – Bob Hanlon
    Mar 26, 2020 at 22:17

1 Answer 1


If you plot it this way:

Plot[{Re[f[x]], Im[f[x]]}, {x, 0, 5}, PlotStyle -> {Red, Blue}]

with the folloowing effect:

enter image description here

you will see that the function has everywhere the real part (red), and in most part has also the imaginary one (blue), though there are several intervals where the imaginary part is equal to zero. No contradiction.

Have fun!

  • $\begingroup$ So, what is your idea about the third plot? why is it discontinuous? $\endgroup$
    – user70481
    Mar 26, 2020 at 17:46
  • 2
    $\begingroup$ Use Plot[{Re[f[x]], Im[f[x]]}, {x, 0, 5}, PlotStyle -> {Red, Blue},PlotPoints->1000] where the PlotPoints->1000 tells it to work harder and take longer to show more detail demonstrating the function is likely continuous. $\endgroup$
    – Bill
    Mar 26, 2020 at 17:50
  • $\begingroup$ @Bill - Alternatively, use MaxRecursion option, e.g., Plot[{Re[f[x]], Im[f[x]]}, {x, 0, 5}, PlotStyle -> {Red, Blue}, MaxRecursion -> 10] $\endgroup$
    – Bob Hanlon
    Mar 26, 2020 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.