From 584d80a0dc98704a80667a3b9b47cca6cb3d7f97 Mon Sep 17 00:00:00 2001 From: Ahmed-Mosharafa Date: Tue, 10 May 2016 18:26:33 +0200 Subject: [PATCH 1/4] Adding Cubic Spline and Newton Interpolation --- Interpolation.cpp | 124 ++++++++++++++++++++++++++++++++++++++++++++++ Interpolation.h | 45 +++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 Interpolation.cpp create mode 100644 Interpolation.h diff --git a/Interpolation.cpp b/Interpolation.cpp new file mode 100644 index 0000000..993396b --- /dev/null +++ b/Interpolation.cpp @@ -0,0 +1,124 @@ +#include "Interpolation.h" +#include +#include +#include +#include + +using namespace std; + +Interpolation::Interpolation() +{ + //ctor +} + +Interpolation::~Interpolation() +{ + //dtor +} + +double Interpolation::Spline(double x[], double y[], double a, int n){ + int interval, i; + double h[n], alpha[n], l[n], u[n], z[n], c[n], b[n], d[n], interpolant,aj,bj,cj,dj; + //setting length of the intervals + for (i=0; i=0; i--){ + c[i] = z[i] - (u[i] * c[i+1]) ; + b[i] = ( (y[i+1] - y[i]) / (h[i]) ) - ((h[i]/3) * (c[i+1] + (2*c[i]))); + d[i] = (c[i+1] - c[i]) / (3*h[i]); + } + sort(x,x+n); + if (a < x[0] || a > x[n-1]){ + cout << "Not in range"; + return 0; + } + else{ + interval = findingInterval(x, a, n); + //calculated coefficients of Sj(x) = aj + bj(x-xj) + cj(x-xj)^2 + dj(x-xj) ^3 + aj = y[interval]; + bj = b[interval]; + cj = c[interval]; + dj = d[interval]; + interpolant = aj + bj + pow( cj * (a - x[interval]) , 2) + pow( dj * (a - x[interval]) , 3); + //cout << "\naj = " << aj << "\nbj = " << bj << "\ncj = " << cj << "\ndj = " << dj << "\n" << "x[interval] = " << x[interval] << "\n " << "a: " << a << "\n"; + return interpolant; + } +} +double Interpolation::findingInterval(double x[], double a, int n) { + for (int i = n-1; i > 0; i--){ + if (a < x[i] && a > x[i-1]) { + return i-1; + } + } +} +double Interpolation::NewtInt(double x[], double y[], double a, int n) +{ + double fdd[n]; //initializng an array of Fdds + double sum,mult; + int i,j; + //initializing FDDs with Ys + for (i=0; ij;i--){ + fdd[i] = ( fdd[i]-fdd[i-1] )/ ( x[i]-x[i-j-1]) ; + } + } + //evaluating fn(x) while calculating coefficients + for(i=n-1;i>=0;i--) + { + mult = 1; + for(j=0;j Date: Tue, 10 May 2016 18:53:23 +0200 Subject: [PATCH 2/4] Example added --- Interpolation.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Interpolation.cpp b/Interpolation.cpp index 993396b..70614ec 100644 --- a/Interpolation.cpp +++ b/Interpolation.cpp @@ -104,21 +104,21 @@ void printArray(double a[], int n){ cout << "\n"; } int main(){ - double newton_test[] = {1,4,6,5,3,1.5,2.5,3.5}; - double newton_values[] = {0.0,1.3862944,1.7917595,1.6094379,1.0986123,0.4054641,0.9162907,1.2527630}; - double spline_test[] = {1,2,3,4,5}; - double spline_values[] = {0.75,0,-1,3,4.75}; - double newton_point = 2; - double spline_point = 1.2; + double newton_test[] = {1,4,6,5,3,1.5,2.5}; //3.5 + double newton_values[] = {0.0,1.3862944,1.7917595,1.6094379,1.0986123,0.4054641,0.9162907}; //1.2527630 + double spline_test[] = {3,4.5,7}; + double spline_values[] = {2.5,1,2.5}; + double newton_point = 3.5; + double spline_point = 5; Interpolation interpolate ; cout << "Test Data of Newton Interpolation\n"; - printArray(newton_test, 8); - printArray(newton_values, 8); - cout << "Newton Interpolation of point " << newton_point << " is: " << interpolate.NewtInt(newton_test, newton_values, newton_point ,8) << "\n\n"; + printArray(newton_test, 7); + printArray(newton_values, 7); + cout << "Newton Interpolation of point " << newton_point << " is: " << interpolate.NewtInt(newton_test, newton_values, newton_point ,7) << "\n\n"; cout << "Test Data of Cubic Spline\n"; - printArray(spline_test, 5); - printArray(spline_values, 5); - cout << "\nSpline Interpolation of point " << spline_point << " is: " << interpolate.Spline(spline_test, spline_values, spline_point ,5); + printArray(spline_test, 3); + printArray(spline_values, 3); + cout << "\nSpline Interpolation of point " << spline_point << " is: " << interpolate.Spline(spline_test, spline_values, spline_point ,3); return 0; } From 6803eb3f9b2c8b7765fd9e315bf790c4f8645347 Mon Sep 17 00:00:00 2001 From: Ahmed-Mosharafa Date: Tue, 17 May 2016 17:29:24 +0200 Subject: [PATCH 3/4] Adding Modularity to Newton Interpolation --- Interpolation.cpp | 79 ++++++++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 29 deletions(-) diff --git a/Interpolation.cpp b/Interpolation.cpp index 70614ec..ed8dee2 100644 --- a/Interpolation.cpp +++ b/Interpolation.cpp @@ -15,6 +15,15 @@ Interpolation::~Interpolation() { //dtor } +////////////// Spline Interpolation //////////////////////// + +double Interpolation::findingInterval(double x[], double a, int n) { + for (int i = n-1; i > 0; i--){ + if (a < x[i] && a > x[i-1]) { + return i-1; + } + } +} double Interpolation::Spline(double x[], double y[], double a, int n){ int interval, i; @@ -58,45 +67,58 @@ double Interpolation::Spline(double x[], double y[], double a, int n){ cj = c[interval]; dj = d[interval]; interpolant = aj + bj + pow( cj * (a - x[interval]) , 2) + pow( dj * (a - x[interval]) , 3); - //cout << "\naj = " << aj << "\nbj = " << bj << "\ncj = " << cj << "\ndj = " << dj << "\n" << "x[interval] = " << x[interval] << "\n " << "a: " << a << "\n"; + cout << "\naj = " << aj << "\nbj = " << bj << "\ncj = " << cj << "\ndj = " << dj << "\n" << "x[interval] = " << x[interval] << "\n " << "a: " << a << "\n"; return interpolant; } } -double Interpolation::findingInterval(double x[], double a, int n) { - for (int i = n-1; i > 0; i--){ - if (a < x[i] && a > x[i-1]) { - return i-1; - } - } -} -double Interpolation::NewtInt(double x[], double y[], double a, int n) -{ - double fdd[n]; //initializng an array of Fdds - double sum,mult; - int i,j; - //initializing FDDs with Ys - for (i=0; ij;i--){ - fdd[i] = ( fdd[i]-fdd[i-1] )/ ( x[i]-x[i-j-1]) ; - } - } - //evaluating fn(x) while calculating coefficients - for(i=n-1;i>=0;i--) + +double Interpolation::evaluatingNewtonPoint(double a, double fdd[], int n, double x[]){ + double sum, mult; + for(int i=n-1;i>=0;i--) { mult = 1; - for(j=0;jj;i--){ + fdd[i] = ( fdd[i]-fdd[i-1] )/ ( x[i]-x[i-j-1]) ; + } + } + return fdd; +} +double Interpolation::NewtInt(double x[], double y[], double a, int n) +{ + double *fdd; //initializng an array of Fdds + double interpolant; + int i,j; + + //calculating FDDs for each iteration + fdd = calculateFdds(x, y , n); + + //evaluating fn(x) while calculating coefficients + interpolant = evaluatingNewtonPoint(a, fdd, n, x); + return interpolant; +} + +/** + @param a[] array to be printed + @param n size of the array + helper function for printing the array +*/ void printArray(double a[], int n){ for (int i = 0; i Date: Tue, 17 May 2016 17:30:03 +0200 Subject: [PATCH 4/4] Adding Modularity to Newton Interpolation --- Interpolation.h | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/Interpolation.h b/Interpolation.h index ca92f0a..5d3798c 100644 --- a/Interpolation.h +++ b/Interpolation.h @@ -26,7 +26,9 @@ class Interpolation protected: private: - /** + ///////////////Spline Interpolation///////////////// + + /** @param x[] array of interval limits @param a point to be interpolated @param n number of elements in array x[] @@ -34,12 +36,28 @@ class Interpolation Finds the interval of the interpolated point to calculate the interpolant */ double findingInterval(double x[], double a, int n); + + + + //////////////Newton Interpolation////////////////////// + + /** + @param a point to be interpolated + @param fdd[] array of finite divided differences acting as coefficients for the equation + @param n number of points given for training data + @param x input array containing the training data + + Interpolates the point a given the finite divided differences according to the following equation + fn(x) = b0 + b1(x - x0) +···+ bn(x - x0)(x - x1)···(x - xn-1) + whereas b0, b1.. are the finite divided differences + */ + double evaluatingNewtonPoint(double a, double fdd[], int n, double x[]); /** - @param a[] array to be printed - @param n size of the array + @param x[] array of input values + @param y[] array of output values for initializing fdds + @param n size of the array */ - void printArray(double a[], n); + double* calculateFdds(double x[], double y[], int n); }; #endif // INTERPOLATION_H -