Plotting a complex function

  1. Your first test function returns nothing. You should return a value. The second test function works because you don’t have the println statement in there, and you’re exploiting the fact that a function without an explicit return returns the result of the last statement. Still, you should have a return

  2. If I understand correctly, you have a function ℝ→ℂ. Plotting such a function doesn’t make sense. You can plot the real part, or the imaginary value, or the absolute value (or abs2). In general, you can only plot a function with a real output value. If your function was ℂ→ℝ, you could have a 3D plot.

  3. Some minor points:

    • Remove the print statements. You don’t want to print something every time the function evaluates
    • Consider making your function type stable. Don’t return 0, return zero(1im) so that it always returns a ComplexF64 independent of the value of t.
    • plot can do some automatic guessing for the values of t to plug in, but it’s better to give it the values over which you want to plot explicitly: plot(tvals, abs2.(test.(tvals)), …)
1 Like