# How to plot a impulse unit function using Plots.jl and SymPy.jl?

**URL:** <https://discourse.julialang.org/t/how-to-plot-a-impulse-unit-function-using-plots-jl-and-sympy-jl/131066>\
**Category:** General Usage\
**Tags:** plots, sympy\
**Created:** [July 25, 2025, 9:04pm UTC](https://discourse.julialang.org/t/how-to-plot-a-impulse-unit-function-using-plots-jl-and-sympy-jl/131066 "2025-07-25T21:04:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [July 25, 2025, 9:04pm UTC](https://discourse.julialang.org/t/how-to-plot-a-impulse-unit-function-using-plots-jl-and-sympy-jl/131066/1 "2025-07-25T21:04:47Z")

</div>

Hello. Good evening.

I’m doing all exercises of the Sadiku’s book in julia programming.

> **[Fundamentals Of Electrics Circuits - Crisalida Livraria | Estante Virtual](https://www.estantevirtual.com.br/livro/fundamentals-of-electrics-circuits-HNA-8402-000?campaign=ev&gad_source=1&gad_campaignid=21580168296&gbraid=0AAAAADteek2wRoxlXRMnWJ8Q_tsdvLyUs&gclid=CjwKCAjw1ozEBhAdEiwAn9qbzdmZ7VsuuFTfp-CefQ_8pgcfq8TIPWqcKy4VqB04ZJuimiud0qNDQRoC2AIQAvD_BwE)**
>
> Fundamentals Of Electrics Circuits de Charles Alexander Matthew No Sadiku, Mcgraw Hill por 270.00 no Crisalida Livraria. Estante Virtual, a maior rede de sebos online do Brasil.

The code is in github

> **[GitHub - jgardona/cfundamentals: Exercises, practical problems and examples from...](https://github.com/jgardona/cfundamentals)**
>
> Exercises, practical problems and examples from Fundamentals of Electric Circuits, 7th edition using Julia programming language.

Im stucked to plot the impulse unit function after get it differentiating the unit step function.  
The plot I get is only a line in zero, as plots.jl cant understand that the interval is all zero for values != t0. May someone help?

code:

```julia
let
	@syms t t₀

	u = sympy.Piecewise(
		(0, Lt(t, 0)),
		(1, Gt(t, 0))
	)
	@info u
	uₜ₀ = subs(u, t=> t - t₀)
	vₜ = 10(subs(uₜ₀, t₀=>2) - subs(uₜ₀, t₀=>5))
	@info vₜ
	p1 = plot(vₜ, xlims=(0, 6), lw=3, legend=:none, size=(400, 300))
	xlabel!(L"t")
	ylabel!(L"v(t)")
	title!(L"Pulso de Tensão $v(t)$")

	δ = diff(vₜ)
	# Ainda não sei como plotar a função de impulso unitário.
	@info δ
    plot( δ)
end

```

Results issues

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/6/36ab1769b758bd717a62ecc7450736e849ff1d82.png)

Correct

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/6/b639d3df304af77fd39e63e33de3b2a29ef76077.png)

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [July 26, 2025, 9:18am UTC](https://discourse.julialang.org/t/how-to-plot-a-impulse-unit-function-using-plots-jl-and-sympy-jl/131066/2 "2025-07-26T09:18:58Z")

</div>

Hi,

> [@jcbritobr](#):
>
> Im stucked to plot the impulse unit function after get it differentiating the unit step function.

First of all, mathematically speaking a step function simply is not differentiable at the location of the step (according to the usual notion of differentiability), so in particular the derivative is not a dirac impulse. At every other point the derivative does exist and is zero.

> [@jcbritobr](#):
>
> The plot I get is only a line in zero, as plots.jl cant understand that the interval is all zero for values != t0.

I’m not very familiar with SymPy (and had to rely on PythonCall to get it working), but if I understand correctly, you’re explicitly not defining the function u: t \mapsto u(t) at t = 0. Consequently, `δ` will not be defined in `2` or `5`. This is not related to the fact that the shifted `u` is non-differentiable here, just that you did not include it in the domain.

```julia-repl
julia> δ
Python: 10*Piecewise((0, (t > 2) | (t < 2))) - 10*Piecewise((0, (t > 5) | (t < 5)))

julia> δ.subs(t, 2)
Python: nan

julia> δ.subs(t, 5)
Python: nan

```

If you would define `u` at `t = 0` via `Ge(t, 0)`, I get

```julia-repl
julia> δ
Python: 0

```

so we still have issues with the non-differentiability.

* * *

In any case, if you want to recreate the plot, you could just manually add arrows

```julia
using Plots, LaTeXStrings

plot([0., 6.], [0., 0.], color=:black,
     xlabel=L"t", ylabel=L"$\frac{dv}{dt}$", legend=:none)
plot!([2., 2.], [0., 10.], arrow=true, color=:black, )
plot!([5., 5.], [0., -10.], arrow=true, color=:black)

```

 ![arrows](https://global.discourse-cdn.com/julialang/original/3X/9/9/99351db7a77b8d6687d9283841a993dac0874a19.png)

(I’m not sure how to get the axes to intersect at the origin in Plots.jl.)

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [July 26, 2025, 11:40am UTC](https://discourse.julialang.org/t/how-to-plot-a-impulse-unit-function-using-plots-jl-and-sympy-jl/131066/3 "2025-07-26T11:40:49Z")

</div>

> [@jcbritobr](#):
>
> `δ = diff(vₜ)`

Your derivative returns `nan` at both 2 and 5. This function gives it a value and then plots (the arrow works with GR at least)

```julia-auto
## δ(2) is nan; so this adjusts
f(x) = isnan(δ(x)) ? float(vₜ(x+.001) - vₜ(x-.001)) : float(δ(x))
plot(0:8, f; linetype=:sticks, arrow=true, framestyle=:origin)

```

(Strangely, I thought defining `u` so it is defined at `0` might make this work, but that gives back a 0 derivative.)

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [July 26, 2025, 8:12pm UTC](https://discourse.julialang.org/t/how-to-plot-a-impulse-unit-function-using-plots-jl-and-sympy-jl/131066/4 "2025-07-26T20:12:28Z")

</div>

Hello friend. Thank you. Was a nice solution.

```julia
let
	@syms t t₀

	u = sympy.Piecewise(
		(0, Lt(t, 0)),
		(1, Gt(t, 0))
	)
	@info u
	uₜ₀ = subs(u, t=> t - t₀)
	vₜ = 10(subs(uₜ₀, t₀=>2) - subs(uₜ₀, t₀=>5))
	@info vₜ
	p1 = plot(vₜ, xlims=(0, 6), lw=3, legend=:none, size=(400, 300))
	xlabel!(L"t")
	ylabel!(L"v(t)")
	title!(L"Pulso de Tensão $v(t)$")

	δ = diff(vₜ)
	# Ainda não sei como plotar a função de impulso unitário.
	@info δ
	f(x) = isnan(δ(x)) ? float(vₜ(x+001) - vₜ(x-.001)) : float(δ(x))
	p2 = plot(0:8, f;
		 linetype=:sticks, arrow=true,
		 framestyle=:origin, legend=:none, lw=3,
		 xlabel=L"t", ylabel=L"\frac{dv}{dt}",
		 title="Função de Impulso Unitário"
		)
	plot(p1, p2, layout=(1, 2), size=(800, 300), margin=5Plots.mm)
	
end

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/a/cab3688b406f0e302d202b529b806220efc57b80.png)

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [July 26, 2025, 9:44pm UTC](https://discourse.julialang.org/t/how-to-plot-a-impulse-unit-function-using-plots-jl-and-sympy-jl/131066/5 "2025-07-26T21:44:19Z")

</div>

Hello. Thank you to help. I pretty apreciate.  
Yes, but this way Im cheating to get the graphs, and indeed, the exception in this impulse is nan, because \<= or \>= is not defined. I just got the derivative, but yes, a step function can be differentiated, and it gives me a impulse function. This exercise is meant to prove that.

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [July 26, 2025, 11:57pm UTC](https://discourse.julialang.org/t/how-to-plot-a-impulse-unit-function-using-plots-jl-and-sympy-jl/131066/6 "2025-07-26T23:57:08Z")

</div>

You can try `sympy.Heaviside(t)` for your `u`. This will integrate as expected once differentiated.

For plotting you still would have to work to get the right plot as evaluating the derivative at 2 or 5 does not return a numeric value, rather this:

```julia-auto
julia> let
       u = sympy.Heaviside(t)
       uₜ₀ = subs(u, t=> t - t₀)
       vₜ = 10(subs(uₜ₀, t₀=>2) - subs(uₜ₀, t₀=>5))
       δ = diff(vₜ)
       δ(2)
       end
10⋅δ(0)

```

Trying this function might be of interest, but you need to make sure 2 and 5 are points that are chosen to be plotted

```julia-auto
f(x) = iszero(δ(x)) ? 0.0 : convert(Float64, (δ(x).o.args[1]))

```

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [July 27, 2025, 10:15am UTC](https://discourse.julialang.org/t/how-to-plot-a-impulse-unit-function-using-plots-jl-and-sympy-jl/131066/7 "2025-07-27T10:15:54Z")

</div>

> [@jcbritobr](#):
>
> Yes, but this way Im cheating to get the graphs, and indeed, the exception in this impulse is nan, because \<= or \>= is not defined. I just got the derivative, but yes, a step function can be differentiated, and it gives me a impulse function.

Well, you have to cheat somewhere 🙂 , be it at the notion of derivative (ideally at the SymPy-level, cf. `j_verzani`’s second reply), the evaluation of some partially defined function (cf. `j_verzani`’s first reply; note that this is not a central finite difference approximation of the derivative, which would also divide by `.002`), or in the plots.
