# Plotting a complex function

**URL:** <https://discourse.julialang.org/t/plotting-a-complex-function/98981>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [May 17, 2023, 4:21am UTC](https://discourse.julialang.org/t/plotting-a-complex-function/98981 "2023-05-17T04:21:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@Anil\_Kumar](https://discourse.julialang.org/u/Anil_Kumar)\
**Post date:** [May 17, 2023, 4:21am UTC](https://discourse.julialang.org/t/plotting-a-complex-function/98981/1 "2023-05-17T04:21:08Z")

</div>

Dear All,  
I am trying to put this function

```julia
using Plots
function test(t,t0,E,Ω)
    if t<t0
        E0=E*t0
        test=1im*E0/Ω *(1-exp.(1im*Ω*t))
        println("t is less than t0")
    else
        test=0
        println("t is grater than t0")
    end
end
plot(t->test(t,2,100,1), xlims=(0,10), label="test(t)";)

```

in a program but it shows me an error “MethodError: no method matching Float64(::Nothing)” while plotting.  
However, if I use without 1im, it works well, e.g.

```julia
using Plots
function test(t,t0,E,Ω)
    if t<t0
        E0=E*t0
        test=E0/Ω *(1-exp.(Ω*t))
    else
        test=0
    end
end
plot(t->test(t,2,100,1), xlims=(0,10), label="test(t)";)

```

Please let me know how can I plot a complex function?

---

<div class="post-metadata">

**Author:** ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)\
**Post date:** [May 17, 2023, 5:23am UTC](https://discourse.julialang.org/t/plotting-a-complex-function/98981/2 "2023-05-17T05:23:10Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [May 17, 2023, 12:33pm UTC](https://discourse.julialang.org/t/plotting-a-complex-function/98981/3 "2023-05-17T12:33:21Z")

</div>

@goerz: The blackboard bold real and complex symbols are neat! Do you have a macro to type the Unicode characters in Markdown? The only way I could figure to reproduce them was to copy-paste them:

```julia
julia> '\u2102'
'ℂ': Unicode U+2102 (category Lu: Letter, uppercase)

julia> '\u211d'
'ℝ': Unicode U+211D (category Lu: Letter, uppercase)

```

---

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@Anil\_Kumar](https://discourse.julialang.org/u/Anil_Kumar)\
**Post date:** [May 17, 2023, 12:53pm UTC](https://discourse.julialang.org/t/plotting-a-complex-function/98981/4 "2023-05-17T12:53:35Z")

</div>

@goerz Perfect! Thanks.

---

<div class="post-metadata">

**Author:** ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)\
**Post date:** [May 17, 2023, 1:08pm UTC](https://discourse.julialang.org/t/plotting-a-complex-function/98981/5 "2023-05-17T13:08:43Z")

</div>

> [@frylock](#):
>
> Do you have a macro to type the Unicode characters in Markdown?

Yes: [The "U.S. International - Scientific" Keyboard Layout - Michael Goerz](https://michaelgoerz.net/notes/the-us-international-scientific-keyboard-layout/index.html) 😊

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [May 17, 2023, 1:23pm UTC](https://discourse.julialang.org/t/plotting-a-complex-function/98981/6 "2023-05-17T13:23:44Z")

</div>

neat!
