# How to take the derivative of a function?

Let’s say I have a function like this:

``````T = 10^6
η = 10^9*T^(-3/2)
x = range(-10, 10, step=1)
t = [0, 1, 2, 4]
B_z(x, t) = @. erf(x/sqrt(4*η*t))
``````

I wanto differentiate `B_z` with respect to x and assign it to a new function called `dBdx(x, t)`. Is there a way to do this?

There are several options. A good starting point is probably

``````T = 10^6
η = 10^9*T^(-3/2)
x = range(-10, 10, step=1)
t = [0, 1, 2, 4]
B_z(x, t) = @. erf(x/sqrt(4*η*t))
dB_zdx(x, t) = ForwardDiff.derivative(B_z(x, t), x)
``````

Ok, so I have attempted to set up the derivative as shown here. This does not seem to work as I intended though, because I expect `dB_zdx(5, 0) = 0` and `dB_zdx(0, 0) = infinity` but when I run these I have

``````MethodError: objects of type Float64 are not callable
``````

Do you know what I may be missing here?

certainly. you need to provide the derivative method with a function of `x`, i.e.

``````dB_zdx(x, t) = ForwardDiff.derivative(x̃ -> B_z(x̃, t), x)
``````
4 Likes