Zmieniono nazwę klasy na Interpolacja
Zaimplementowano metodę Interpolacja.oblicz() do obliczenia interpolacjI Newtona
This commit is contained in:
83
interpolacja/Interpolacja.cpp
Normal file
83
interpolacja/Interpolacja.cpp
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <vector>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Interpolacja
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void readFromFile(string filename)
|
||||||
|
{
|
||||||
|
string str;
|
||||||
|
fstream file(filename);
|
||||||
|
if (!file)
|
||||||
|
{
|
||||||
|
cerr << "Nie udalo sie otworzyc pliku o nazwie: " << filename << "\n";
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
while (getline(file, str))
|
||||||
|
{
|
||||||
|
istringstream iss(str);
|
||||||
|
if (iss >> a >> b) {
|
||||||
|
x.push_back(a);
|
||||||
|
y.push_back(b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
h = x[1] - x[0];
|
||||||
|
liczba_wezlow = x.size();
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void oblicz()
|
||||||
|
{
|
||||||
|
tablica_roznic.assign(liczba_wezlow, vector<float>(liczba_wezlow));
|
||||||
|
|
||||||
|
for (int i = 0; i < liczba_wezlow; i++) {
|
||||||
|
tablica_roznic[i][0] = y[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int j = 1; j < liczba_wezlow; j++) {
|
||||||
|
for (int i = 0; i < liczba_wezlow - j; i++) {
|
||||||
|
tablica_roznic[i][j] = (tablica_roznic[i + 1][j - 1] - tablica_roznic[i][j - 1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suma = tablica_roznic[0][0];
|
||||||
|
for (int i = 1; i < liczba_wezlow; i++) {
|
||||||
|
czynnik = tablica_roznic[0][i];
|
||||||
|
for (int j = 0; j < i; j++) {
|
||||||
|
czynnik *= (x[j] - x[0]);
|
||||||
|
}
|
||||||
|
suma += czynnik;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void wyswietl_tabela()
|
||||||
|
{
|
||||||
|
// nagłowki
|
||||||
|
cout << setw(5) << "i" << setw(10) << "x" << setw(10) << "y";
|
||||||
|
for (int j = 1; j < liczba_wezlow; ++j)
|
||||||
|
cout << setw(10) << "Δ^" + to_string(j);
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
|
// wartości
|
||||||
|
for (int i = 0; i < liczba_wezlow; i++) {
|
||||||
|
cout << setw(5) << i << setw(10) << x[i];
|
||||||
|
for (int j = 0; j < liczba_wezlow - i; j++) {
|
||||||
|
cout << setw(10) << tablica_roznic[i][j];
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "Interpolowany wynik: " << suma << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
float a, b, h, suma, czynnik;
|
||||||
|
int liczba_wezlow;
|
||||||
|
vector<float> x, y;
|
||||||
|
vector<vector<float> > tablica_roznic;
|
||||||
|
};
|
||||||
6
interpolacja/input.txt
Normal file
6
interpolacja/input.txt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
1 2
|
||||||
|
1.5 2.5
|
||||||
|
2 3.5
|
||||||
|
2.5 4
|
||||||
|
3 5.3
|
||||||
|
3.5 6.0
|
||||||
7
main.cpp
7
main.cpp
@@ -1,5 +1,5 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "newton/Newton.cpp"
|
#include "interpolacja/Interpolacja.cpp"
|
||||||
#include "horner/horner_class.cpp"
|
#include "horner/horner_class.cpp"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
@@ -25,9 +25,10 @@ int main()
|
|||||||
|
|
||||||
// Newton test part
|
// Newton test part
|
||||||
|
|
||||||
Newton first;
|
Interpolacja first;
|
||||||
first.readFromFile("newton/input.txt");
|
first.readFromFile("interpolacja/input.txt");
|
||||||
// first.wyswietl_x_y();
|
// first.wyswietl_x_y();
|
||||||
|
first.oblicz();
|
||||||
first.wyswietl_tabela();
|
first.wyswietl_tabela();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -1,62 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
#include <fstream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
class Newton
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
void readFromFile(string filename)
|
|
||||||
{
|
|
||||||
string str;
|
|
||||||
// stopien = 0;
|
|
||||||
|
|
||||||
fstream file(filename);
|
|
||||||
if (!file)
|
|
||||||
{
|
|
||||||
cerr << "Nie udalo sie otworzyc pliku o nazwie: " << filename << "\n";
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
while (getline(file, str))
|
|
||||||
{
|
|
||||||
std::istringstream iss(str);
|
|
||||||
if (iss >> a >> b) {
|
|
||||||
x.push_back(a);
|
|
||||||
y.push_back(b);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
file.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
void oblicz()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void wyswietl_x_y()
|
|
||||||
{
|
|
||||||
for (int i = 0; i < x.size(); i++)
|
|
||||||
{
|
|
||||||
cout << "x" << i << ": " << x[i] << "\t \t" << "y" << i << ": " << y[i] << "\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void wyswietl_tabela()
|
|
||||||
{
|
|
||||||
std::cout << std::setw(5) << "i" << std::setw(10) << "x" << std::setw(10) << "y" << std::endl; // Table headers
|
|
||||||
std::cout << std::string(26, '-') << std::endl;
|
|
||||||
|
|
||||||
for (size_t i = 0; i < x.size(); ++i)
|
|
||||||
{
|
|
||||||
std::cout << std::setw(5) << i << std::setw(10) << x[i] << std::setw(10) << y[i] << std::endl; // Table rows
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
|
||||||
float a, b;
|
|
||||||
vector<float> x ,y;
|
|
||||||
};
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
1 2
|
|
||||||
1.5 2.5
|
|
||||||
2 3.5
|
|
||||||
2.5 4
|
|
||||||
Reference in New Issue
Block a user