Is it possible to write code in Mathematica that implements various differentiation methods (like forward, central, extrapolated, etc.)?
To find the finite-difference formula using
I have a demo that uses this method in the pipeline for the demonstration web site, but if you like you can try it from here
A report on this using Mathematica is here. Examples of using polynomial method:
Short answer: yes.
Slightly longer answer: this is the simplest way that I could think of, several years ago. I hope the code still works on newer versions of mma.
You can test it.
And so on with the other schemes. Edit: please note that with every differentiation you lose a point in your data. These schemes are not indicated for implementation of multiple differentiations (I am not aware if they can be useful at all in the real world, perhaps this form can be used in the microcontroller's code of an embedded system...) More efficient methods compute an approximation of the (nth) derivative using Taylor's polynomial, the (easily computable) derivatives of a suitable interpolating polynomial or, if you really want to show off :-) a Cauchy integral.
In my experience almost all finite difference formulas can be implemented very efficiently using
We can check that they are performing the correct behavior via a simple symbolic test
With that said, there is also a very nice function hidden inside of the
Let's test it out:
In the spirit of sharing ancient code I wrote on this topic that is still working and relevant today, here is some code in a notebook I called "difference differentials". You should like this, I certainly got a kick out of it when I did it. It was a 4 beer late night curiosity event that was never shared until now. This was from the early undergrad days, a private curiosity, and I was basically "hacking" the Mathematica with few skills, so don't pick on me, I actually have grown by light years since this.
My original description:
"This code uses central differences to approximate the derivatives of a function. Change lo, hi, del, and f[x_] as you please, then go to Evaluation->Evaluate Notebook for a new result."
I am entertained by the result of that output even today, but there was alot I did not know about writing good code at that time that could be improved.
Here is the table output (WOW there was so much I did not know back then!):