From 53439780563f7f618daf3efffb38fb7f86ed04ee Mon Sep 17 00:00:00 2001 From: anacalinescu Date: Sat, 20 Oct 2018 16:29:41 +0300 Subject: [PATCH 1/4] Neville --- python/NevillePoint.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 python/NevillePoint.py diff --git a/python/NevillePoint.py b/python/NevillePoint.py new file mode 100644 index 0000000..40f1838 --- /dev/null +++ b/python/NevillePoint.py @@ -0,0 +1,22 @@ +# Function that calculates the value of the Neville interpolation polynomial +# P_ij(x) for a vector of points (a, b) with a:xs, b:xs in the abscissa x; +# [NOTE] It doesn't use explicit caching (just the internal Octave caching +# system) + + +def nevillepoint(i, j, x, xs, ys): + # Base case: P_ii(x) = f(x_i) + # ys is 1 - indexed, so we have to add one to the index + if i == j: + y = ys[i] + return y + + # x_j - x_i + delta = xs[j] - xs[i] + # P(i, j - 1, x) + pij_ = (xs[j] - x) * nevillepoint(i, j - 1, x, xs, ys) + # P(i + 1, j, x) + pi_j = (x - xs[i]) * nevillepoint(i + 1, j, x, xs, ys) + # P(i, j, x) + y = (pij_ + pi_j) / delta + return y From 9a1f27f7566f070d1e14dc0d6ce864d4417acad6 Mon Sep 17 00:00:00 2001 From: anacalinescu Date: Sat, 20 Oct 2018 16:33:55 +0300 Subject: [PATCH 2/4] NevilleInterpolation --- python/NevilleInterpolation.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 python/NevilleInterpolation.py diff --git a/python/NevilleInterpolation.py b/python/NevilleInterpolation.py new file mode 100644 index 0000000..c71f3fb --- /dev/null +++ b/python/NevilleInterpolation.py @@ -0,0 +1,29 @@ +import numpy as np +import matplotlib.pyplot as plt +from NevillePoint import nevillepoint + + +# [USES] interpolations/NevillePoint +# Function that calculates the Neville interpolation polynomial for a vector +# of points (a, b) with a:xs, b:ys in "elements" points between the first of xs +# and the last of xs; it also plots the interpolation +def nevilleinterpolation(xs, ys, elements=100): + n = len(xs) + xss = np.linspace(xs[0], xs[n - 1], elements) + # create an empty vector to store the values of the interpolation in the + # xss' points + yss = [] + + for i in range(0, len(xss)): + # Calculate the approximation of the function in xss(i) + yss.append(nevillepoint(0, n - 1, xss[i], xs, ys)) + + + + print(yss) + # plot the interpolated function + plt.plot(xss, yss) + # plot the initial points as circles + plt.plot(xs, ys, 'o', "linewidth", 3) + plt.show() + From eb77c33a6e85a6c1c3fe95a2821801d03b053102 Mon Sep 17 00:00:00 2001 From: anacalinescu Date: Sat, 20 Oct 2018 17:34:11 +0300 Subject: [PATCH 3/4] NewtonRaphson --- python/NewtonRaphson.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 python/NewtonRaphson.py diff --git a/python/NewtonRaphson.py b/python/NewtonRaphson.py new file mode 100644 index 0000000..812eeed --- /dev/null +++ b/python/NewtonRaphson.py @@ -0,0 +1,30 @@ +import numpy as np + + +def NewtonRaphson(f, x0, tol, max_iter): + # solves f(x) = 0 by doing max_iter steps; + # the method requires one initial value (x0) + # which should be chosen close to the root; + # y = np.polyval(f, x) returns the value of a polynomial evaluated at x; + # y = np.polyder(f) returns the coefficients of the derivative of the polynomial + # whose coefficients are given by the vector p; + fd = np.polyder(f) + for i in range(0, max_iter): + fx = np.polyval(f, x0) + fxd = np.polyval(fd, x0) + # calculate the value of the new approximation, xi, using the formula; + xi = x0 - fx / fxd + # calculate the value of the polynomial evaluated at xi; + fxi = np.polyval(f, xi) + # check if xi is a root of the polynomial; + if abs(fxi) < np.spacing(1): + x = xi + return [x, i] + epsilon = abs((xi - x0) / xi) + # stop if the method reached its convergence limit; + if epsilon < tol: + x = xi + return [x, i] + # update the last computed value; + x0 = xi + print("Maximum number of iterations reached") \ No newline at end of file From 24f9207ef34b1b32f21f12a9a86d5ffe34fb4649 Mon Sep 17 00:00:00 2001 From: anacalinescu Date: Sat, 20 Oct 2018 17:40:01 +0300 Subject: [PATCH 4/4] remove Neville --- python/NevilleInterpolation.py | 29 ----------------------------- python/NevillePoint.py | 22 ---------------------- 2 files changed, 51 deletions(-) delete mode 100644 python/NevilleInterpolation.py delete mode 100644 python/NevillePoint.py diff --git a/python/NevilleInterpolation.py b/python/NevilleInterpolation.py deleted file mode 100644 index c71f3fb..0000000 --- a/python/NevilleInterpolation.py +++ /dev/null @@ -1,29 +0,0 @@ -import numpy as np -import matplotlib.pyplot as plt -from NevillePoint import nevillepoint - - -# [USES] interpolations/NevillePoint -# Function that calculates the Neville interpolation polynomial for a vector -# of points (a, b) with a:xs, b:ys in "elements" points between the first of xs -# and the last of xs; it also plots the interpolation -def nevilleinterpolation(xs, ys, elements=100): - n = len(xs) - xss = np.linspace(xs[0], xs[n - 1], elements) - # create an empty vector to store the values of the interpolation in the - # xss' points - yss = [] - - for i in range(0, len(xss)): - # Calculate the approximation of the function in xss(i) - yss.append(nevillepoint(0, n - 1, xss[i], xs, ys)) - - - - print(yss) - # plot the interpolated function - plt.plot(xss, yss) - # plot the initial points as circles - plt.plot(xs, ys, 'o', "linewidth", 3) - plt.show() - diff --git a/python/NevillePoint.py b/python/NevillePoint.py deleted file mode 100644 index 40f1838..0000000 --- a/python/NevillePoint.py +++ /dev/null @@ -1,22 +0,0 @@ -# Function that calculates the value of the Neville interpolation polynomial -# P_ij(x) for a vector of points (a, b) with a:xs, b:xs in the abscissa x; -# [NOTE] It doesn't use explicit caching (just the internal Octave caching -# system) - - -def nevillepoint(i, j, x, xs, ys): - # Base case: P_ii(x) = f(x_i) - # ys is 1 - indexed, so we have to add one to the index - if i == j: - y = ys[i] - return y - - # x_j - x_i - delta = xs[j] - xs[i] - # P(i, j - 1, x) - pij_ = (xs[j] - x) * nevillepoint(i, j - 1, x, xs, ys) - # P(i + 1, j, x) - pi_j = (x - xs[i]) * nevillepoint(i + 1, j, x, xs, ys) - # P(i, j, x) - y = (pij_ + pi_j) / delta - return y