diff --git a/Maths/numerical_differentiation.cpp b/Maths/numerical_differentiation.cpp new file mode 100644 index 0000000..86e0913 --- /dev/null +++ b/Maths/numerical_differentiation.cpp @@ -0,0 +1,391 @@ +#include +#include +using namespace std; +#define MAX 20 + +//Print the forward and stirling differentiation tables +void printDiffTable(float y[][MAX], int n) { + for (int i = 0; i < n; i++) { + for (int j = 0; j < n - i; j++) + cout << y[i][j] << "\t "; + cout << endl; + } +} + +//Print the backward differentiation tables +void printBackDiffTable(float y[][MAX], int n) { + for (int i = 0; i < n; i++) { + for (int j = 0; j <= i; j++) + cout << y[i][j] << "\t"; + cout << endl; + } +} + +//Selecting the type of data points to be entered +int getState() { + int equiState = 0; + cout << "1 - Equisacped\n2 - Not Equisacped" << endl; + cout << "Select the type of data points to be entered: "; + cin >> equiState; + return equiState; +} + +//Setting the state of data point inputs +bool setState(int equiState) { + if(equiState == 1) { + return true; + } + else if(equiState == 2) { + return true; + } + return false; +} + +//Calculating the factorial +int fact(int n) { + int f = 1; + for (int i = 2; i <= n; i++) + f *= i; + return f; +} + +//Newton forward differentiation method +//Pterm to be used by forward function +float forwardPterm(float p,int it){ + float prod = 1, temp_sum = 0; + int tag = 0; + for (int i = 0; i floor(-it/2); i--) { + if((p-i)!=0){ + prod = prod * (p - i); + } + else + tag = 1; + } + + if(tag==1) + return prod; + + for(int i=0; i= i; j--) + y[j][i] = y[j][i - 1] - y[j - 1][i - 1]; + } +} + +//Applying the newton stirling differentiation formulae +float backward(float y[][MAX], float p, int n, int index) { + float derivate = y[index][1]; + for(int i = 2; i <= index; i++){ + cout<> lenght; + + if(equiState == 1) { + // The input of data points for equi-spaced data + float interval = 1, minDiff; + int method = 2, minInd = 0; + cout << "Enter the first value of X: "; + cin >> x[0]; + cout << "Enter the interval: "; + cin >> interval; + for(int i = 1; i < lenght; i++) { + x[i] = x[i - 1] + interval; + } + cout << "Enter the Y values: " << endl; + for(int i = 0; i < lenght; i++) { + cin >> y[i][0]; + } + cout << "The first differential at x = "; + cin >> z; + + // Figuring out the best possible method for given data points + minDiff = z - x[0]; + for(int i = 0; i < lenght/2; i++) { + if( z>=x[i] && z= lenght/2; i--) { + if(x[i]>=z && x[i-1] lenght * 0.6) { + method = 3; + } + + // The methods applied + if(method == 1) { + // Forward + float p = (z - x[minInd]) / interval; + forwardDiffTable(y, lenght); + cout << "Forward Table: " << endl; + printDiffTable(y, lenght); + cout << "Newton Forward Differential = " << forward( y, p, lenght, minInd) / interval << endl; + } + else if(method == 2){ + // Stirling + float p = (z - x[lenght/2]) / interval; + strilingDiffTable(y, lenght); + cout << "Stirling Table: " << endl; + printDiffTable(y, lenght); + cout << "Stirling Differential = " << stirling(y, p, lenght) / interval << endl; + } + else if(method == 3) { + // Backward + float p = (z - x[minInd]) / interval; + backwardDiffTable(y, lenght); + cout << "Backward Table: " << endl; + printBackDiffTable(y, lenght); + cout << "Newton Backward Differential = " << backward(y, p, lenght, minInd) / interval << endl; + } + else { + cout << "The value cannot be evaluated" << endl << "Out of bound" << endl; + } + } + else { + // The input of data points for unequi-spaced data + cout << "Enter the X values: " << endl; + for(int i = 0; i < lenght; i++) { + cin >> x[i]; + } + cout << "Enter the Y values: " << endl; + for(int i = 0; i < lenght; i++) { + cin >> y[i][0]; + } + cout << "The first differential at x = "; + cin >> z; + + // Newton + newtonDiffTable(x, y, lenght); + cout << "Newton Dividend Differential Table:" << endl; + printDiffTable(y, lenght); + cout << "Newton Dividend Differential = " << newton(z, x, y, lenght) << endl; + + // Lagrange + cout << "Lagrange Differential = " << lagrange(x, y, z, lenght) << endl; + } + return 0; +}