working derivative version. Found a bug?
correction to the numerical differentiaions
better derivative-based