# [erlang-questions] Newton's Forward Difference Formula implementation available at GitHub

Sun Feb 15 22:30:09 CET 2009

```If anyone's interested, I put a naive implementation of Newton's Forward
Difference Formula up at GitHub:

For those who don't know the formula, you give it a list of known values and
it will return the coefficients of a function f(x) needed to reproduce the
sequence you entered and produce any subsequent numbers.

As an example, say you're asked to find an f(x) whose first three values
(x=1,2,3) are [15, 28, 39].  You would just need to ask:

---
newton:solve([15, 28, 39]).
---

...and it would answer [-1, 16, 0], meaning [f(x) = -x^2 + 16x + 0] or [f(x)
= 16x - x^2].  The return value of newton:solve/1 can be fed into the
following function [for Coefficients] to produce other values:

---
fofx(X, Coefficients) -> Len = length(Coefficients), Exponents =
lists:seq(Len-1, 0, -1), lists:sum(lists:zipwith(fun(Coefficient, Exponent)
->  Coefficient * tmath:pow(X, Exponent) end, Coefficients, Exponents)).
---

Notes:

-I wrote this for use in the shell, so stability in a real-world application
is unknown.
-It currently only solves when it's given values starting from x=1.
-I have only tested it up to producing equations of degree 4, but it should
work for others.
-newton:solve/1 provides a solution as accurate as it can determine, which
is dependent on: lack of typos and initial inputs.

If anyone finds it useful, great!  If not, no biggie--I'm just using it to
learn how to use GitHub. :)

Best,

Steven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090215/ba2b6b3c/attachment.htm>
```