Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Заготовка для программы решения задачи методом наименьших квадратов (МНК).
- Программа только сохраняет на диске значения x(i), y.экпер(i), y.теор(i) и
- N (число значений i) для построения в MATLAB графика, иллюстрирующего
- результаты реализации МНК.
- Передаются:
- N (1-й файл) и x, y1, y2 (2-й файл).
- */
- #include <stdlib.h>
- #include <math.h>
- #include <fstream>
- #include <iostream>
- #include <iomanip>
- //#define PRES float // разрядность "float"
- #define PRES double // разрядность "double"
- #define N 7 // число x(i)
- using namespace std;
- void gaus2(PRES** arr,PRES* yrr1,PRES* x,int n)
- {
- double d = 0;
- for (int k = 0; k < n; k++) // proxod po strokam
- {
- int stob = k;
- double* str = arr[k]; // for change str
- double max = arr[k][k]; // max element
- for (int g = k + 1; g < n; g++) // cycle for max in stolb and change str
- {
- if (max < arr[g][k])
- {
- max = arr[g][k];
- str = arr[g];
- arr[g] = arr[k];
- arr[k] = str;
- double o = yrr1[k];
- yrr1[k] = yrr1[g];
- yrr1[g] = o;
- }
- }
- for (int j = k + 1; j < n; j++) // cycle for go on stroke
- {
- d = arr[j][k] / max;
- for (int i = k + 1; i < n; i++) // cycle for calculation
- {
- arr[j][i] -= d * arr[k][i];
- }
- yrr1[j] -= d * yrr1[k];
- }
- }
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; ++j)
- {
- cout << setw(10)<< arr[i][j];
- }
- cout << endl;
- }
- cout << endl;
- for (int i = 0; i < n; i++)
- {
- cout << setw(10) << yrr1[i];
- }
- cout << endl;
- //output x
- double a = 0; // dopolnenie
- for (int j = n - 1; j > -1; j--)
- {
- x[j] = (yrr1[j] - a) / arr[j][j]; // schet elem
- a = 0;
- for (int i = j; (i < n) && (i > 0); i++)
- {
- a += x[i] * arr[j - 1][i]; // dopolnenie
- }
- }
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- cout << setw(10) << arr[i][j];
- }
- cout << endl;
- }
- cout << endl;
- for (int i = 0; i < n; i++)
- {
- cout << setw(10) << yrr1[i];
- }
- cout << endl << "Answer:" << endl;
- for (int i = 0; i < n; i++) // output answer
- {
- cout << "x[" << i + 1 << "]=" << x[i] << endl;
- }
- }
- int main(int argc, char** argv)
- {
- int i, n, m;
- PRES w;
- cin >> m;
- ofstream foun("D:/n.dat", ios_base::out | ios_base::trunc | ios_base::binary); // создаём объект класса ofstream для записи
- n = N;
- foun.write((char*)& n, sizeof n); // запись в файл
- foun.close(); // закрываем файл
- //PRES xo[N] = { 2.40, 3.50, 5.00, 6.89, 10.00 };//массив x(i)
- PRES x[N] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 };//массив x(i)
- PRES y0[N] = { 760.0, 674.8, 598.0, 528.9, 466.6, 410.6, 360.2 }; // массив y.экпер(i)
- PRES y1[N];// = { log10(760.0), log10(674.8), log10(598.0), log10(528.9),log10( 466.6), log10(410.6), log10(360.2) };
- for (int u = 0; u < N; u++)
- {
- y1[u] = log10(y0[u]);
- cout << y1[u] << ',';
- }
- double y2[N]; // массив y.теор(i)
- PRES* powerx = new PRES [m + m];
- PRES** sumx = new PRES* [m + 1];
- for (i = 0; i < m + 2; i++)
- sumx[i] = new PRES[m + 2];
- i = 0;
- for (int e = 1; e < m + m +1; e++)
- {
- PRES temp = 0;
- for (int q = 0; q < N; q++)
- {
- temp += pow(x[q], e);
- }
- powerx[e-1] = temp;
- temp = 0;
- //cout << powerx[e - 1]<<" ";
- }
- cout << endl;
- PRES* praw = new PRES[m + 1];
- for (int e = 0; e < m+1; e++) {
- PRES temp = 0;
- for (int q = 0; q < N; q++)
- {
- temp +=y1[q]* pow(x[q], e);
- }
- praw[e] = temp;
- temp = 0;
- }
- sumx[0][0] = N;
- for (i = 0; i < m + 1; i++)
- {
- for (int j = 0; j < m + 1; j++)
- {
- if (i + j == 0)
- {
- sumx[i][j] = N;
- }else
- sumx[i][j] = powerx[j + i - 1];
- }
- }
- i = 0;
- for (int e = 0; e < m + 1; e++)
- {
- sumx[e][m + 1] = praw[e];
- }
- /*cout <<"matrica"<< endl;
- for (i = 0; i < m + 1; i++)
- {
- for (int j = 0; j < m + 2; j++)
- {
- cout << sumx[i][j] << " ";
- }
- cout << i << endl;
- }*/
- PRES* reza = new PRES[m + 1];
- for (int q = 0; q < m + 1; q++)
- {
- reza[q] = praw[q];
- }
- PRES* rez = new PRES[m + 1];
- gaus2(sumx,reza,rez,m+1);
- for (int e = 0; e < N; e++) {
- PRES temp = 0;
- //cout << rez[e] << " reaulgtn ";
- for (int q = 0; q < m+1; q++)
- {
- //if(q!=1)
- temp += rez[q]*pow(x[e],q);
- }
- temp = pow(10.0, temp);
- y2[e] = temp;
- temp = 0;
- cout << "y1=";
- cout << setprecision (30)<< y0[e]<<endl;
- cout<< "y2=";
- cout << setprecision (30)<< y2[e]<<endl;
- }
- ofstream foutx("D:/x.dat", ios_base::out | ios_base::trunc | ios_base::binary); // создаём объект класса ofstream для записи
- for (i = 0; i < N; i++) // запись в файл x(i)
- {
- w = x[i];
- foutx.write((char*)& w, sizeof w);
- }
- for (i = 0; i < N; i++) // запись в файл y.экпер(i)
- {
- w = y0[i];
- foutx.write((char*)& w, sizeof w);
- }
- for (i = 0; i < N; i++) // запись в файл y.теор(i)
- {
- w = y2[i];
- foutx.write((char*)& w, sizeof w);
- }
- foutx.close(); // закрываем файл
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement