Last active
March 20, 2018 20:33
-
-
Save PiotrWegrzyn/b5822cc23c2ef08f22f6a74e17017464 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
double newton(int ** &resultsTable, int * tabX, int* tabY, int k , int i) { | |
if (resultsTable[i][k] == NULL) { | |
if (k == 0) { | |
cout << "Dla rzedu=" << k << "[X" << i << ", X" << i+k << "]="<<1<<endl; | |
resultsTable[i][k]=1; | |
return 1; | |
} | |
if (k == 1) { | |
cout << "Dla rzedu=" << k << "[X" << i << ", X" << i+k << "]="<< (tabY[i + 1] - tabY[i]) / (tabX[i + k] - tabX[i]) << endl; | |
resultsTable[i][k] = (tabY[i + 1] - tabY[i]) / (tabX[i + k] - tabX[i]); | |
return (tabY[i + 1] - tabY[i]) / (tabX[i + k] - tabX[i]); | |
} | |
cout << "Dla rzedu=" << k << "[X" << i << ", X" << i+k << "]="<<(newton(resultsTable, tabX, tabY, k - 1, i + 1) - newton(resultsTable, tabX, tabY, k - 1, i)) / (tabX[i + k] - tabX[i]) << endl; | |
resultsTable[i][k] = (newton(resultsTable, tabX, tabY, k - 1, i + 1) - newton(resultsTable, tabX, tabY, k - 1, i)) / (tabX[i + k] - tabX[i]); | |
return(newton(resultsTable, tabX, tabY, k - 1, i + 1) - newton(resultsTable, tabX, tabY, k - 1, i)) / (tabX[i + k] - tabX[i]); | |
} | |
else return resultsTable[i][k]; | |
} | |
int main() { | |
int wynik; | |
int tabX[] = { 0,2,3,5,6 }; | |
int tabY[] = { 0,8,27,125,216 }; | |
int pointsQuantity = 5; | |
double result = 0; | |
double tmp = 0; | |
int ** resultsTable = new int*[pointsQuantity]; | |
for (int i = 0; i < pointsQuantity; i++) { | |
resultsTable[i] = new int[pointsQuantity]; | |
for(int j = 0; j < pointsQuantity; j++){ | |
resultsTable[i][j] = NULL; | |
} | |
} | |
//tabela 1 | |
for (double x=0; x < 6.5; x += 0.5) { | |
result =tabY[0]; | |
for (int i = 0; i < pointsQuantity; i++) | |
{ | |
tmp = 0; | |
for (int j = 0; j < i; j++) | |
{ | |
if (j == 0)tmp = x - tabX[0]; | |
else tmp *= x - tabX[j]; | |
} | |
result += tmp*newton(resultsTable,tabX, tabY, i, 0); | |
} | |
cout <<"Wynik dla x="<<x<<" to "<< result<<endl; | |
} | |
//tabela 2 | |
int tab2X[] = { 0,2,4}; | |
int tab2Y[] = { 1,5,17 }; | |
pointsQuantity = 3; | |
for (int i = 0; i < pointsQuantity; i++) { | |
for (int j = 0; j < pointsQuantity; j++) { | |
resultsTable[i][j] = NULL; | |
} | |
} | |
cout << "Tabela 2:#######################################################"; | |
for (double x = 0; x < 6.5; x += 0.5) { | |
result = tab2Y[0]; | |
for (int i = 0; i < pointsQuantity; i++) | |
{ | |
tmp = 0; | |
for (int j = 0; j < i; j++) | |
{ | |
if (j == 0)tmp = x - tab2X[0]; | |
else tmp *= x - tab2X[j]; | |
} | |
result += tmp*newton(resultsTable, tab2X, tab2Y, i, 0); | |
} | |
cout << "Wynik dla x=" << x << " to " << result << endl; | |
} | |
system("pause"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment