Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. /*
  2. Заготовка для программы решения задачи методом наименьших квадратов (МНК).
  3. Программа только сохраняет на диске значения x(i), y.экпер(i), y.теор(i) и
  4. N (число значений i) для построения в MATLAB графика, иллюстрирующего
  5. результаты реализации МНК.
  6. Передаются:
  7. N (1-й файл) и x, y1, y2 (2-й файл).
  8. */
  9.  
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <fstream>
  13. #include <iostream>
  14. #include <iomanip>
  15.  
  16. //#define PRES float // разрядность "float"
  17. #define PRES double // разрядность "double"
  18.  
  19. #define N 7 // число x(i)
  20.  
  21. using namespace std;
  22. void gaus2(PRES** arr,PRES* yrr1,PRES* x,int n)
  23. {
  24. double d = 0;
  25. for (int k = 0; k < n; k++) // proxod po strokam
  26. {
  27. int stob = k;
  28. double* str = arr[k]; // for change str
  29. double max = arr[k][k]; // max element
  30. for (int g = k + 1; g < n; g++) // cycle for max in stolb and change str
  31. {
  32. if (max < arr[g][k])
  33. {
  34. max = arr[g][k];
  35. str = arr[g];
  36. arr[g] = arr[k];
  37. arr[k] = str;
  38. double o = yrr1[k];
  39. yrr1[k] = yrr1[g];
  40. yrr1[g] = o;
  41. }
  42. }
  43. for (int j = k + 1; j < n; j++) // cycle for go on stroke
  44. {
  45. d = arr[j][k] / max;
  46. for (int i = k + 1; i < n; i++) // cycle for calculation
  47. {
  48. arr[j][i] -= d * arr[k][i];
  49. }
  50. yrr1[j] -= d * yrr1[k];
  51. }
  52. }
  53. for (int i = 0; i < n; i++)
  54. {
  55. for (int j = 0; j < n; ++j)
  56. {
  57. cout << setw(10)<< arr[i][j];
  58. }
  59. cout << endl;
  60. }
  61. cout << endl;
  62. for (int i = 0; i < n; i++)
  63. {
  64. cout << setw(10) << yrr1[i];
  65. }
  66. cout << endl;
  67. //output x
  68. double a = 0; // dopolnenie
  69. for (int j = n - 1; j > -1; j--)
  70. {
  71. x[j] = (yrr1[j] - a) / arr[j][j]; // schet elem
  72. a = 0;
  73. for (int i = j; (i < n) && (i > 0); i++)
  74. {
  75. a += x[i] * arr[j - 1][i]; // dopolnenie
  76. }
  77. }
  78. for (int i = 0; i < n; i++)
  79. {
  80. for (int j = 0; j < n; j++)
  81. {
  82. cout << setw(10) << arr[i][j];
  83. }
  84. cout << endl;
  85. }
  86. cout << endl;
  87. for (int i = 0; i < n; i++)
  88. {
  89. cout << setw(10) << yrr1[i];
  90. }
  91. cout << endl << "Answer:" << endl;
  92. for (int i = 0; i < n; i++) // output answer
  93. {
  94. cout << "x[" << i + 1 << "]=" << x[i] << endl;
  95.  
  96. }
  97. }
  98.  
  99. int main(int argc, char** argv)
  100. {
  101. int i, n, m;
  102. PRES w;
  103. cin >> m;
  104. ofstream foun("D:/n.dat", ios_base::out | ios_base::trunc | ios_base::binary); // создаём объект класса ofstream для записи
  105. n = N;
  106. foun.write((char*)& n, sizeof n); // запись в файл
  107. foun.close(); // закрываем файл
  108.  
  109. //PRES xo[N] = { 2.40, 3.50, 5.00, 6.89, 10.00 };//массив x(i)
  110. PRES x[N] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 };//массив x(i)
  111. PRES y0[N] = { 760.0, 674.8, 598.0, 528.9, 466.6, 410.6, 360.2 }; // массив y.экпер(i)
  112.  
  113. PRES y1[N];// = { log10(760.0), log10(674.8), log10(598.0), log10(528.9),log10( 466.6), log10(410.6), log10(360.2) };
  114. for (int u = 0; u < N; u++)
  115. {
  116. y1[u] = log10(y0[u]);
  117. cout << y1[u] << ',';
  118. }
  119. double y2[N]; // массив y.теор(i)
  120.  
  121. PRES* powerx = new PRES [m + m];
  122.  
  123. PRES** sumx = new PRES* [m + 1];
  124. for (i = 0; i < m + 2; i++)
  125. sumx[i] = new PRES[m + 2];
  126.  
  127.  
  128. i = 0;
  129. for (int e = 1; e < m + m +1; e++)
  130. {
  131. PRES temp = 0;
  132. for (int q = 0; q < N; q++)
  133. {
  134. temp += pow(x[q], e);
  135. }
  136. powerx[e-1] = temp;
  137. temp = 0;
  138. //cout << powerx[e - 1]<<" ";
  139. }
  140. cout << endl;
  141. PRES* praw = new PRES[m + 1];
  142. for (int e = 0; e < m+1; e++) {
  143. PRES temp = 0;
  144. for (int q = 0; q < N; q++)
  145. {
  146. temp +=y1[q]* pow(x[q], e);
  147. }
  148. praw[e] = temp;
  149. temp = 0;
  150. }
  151. sumx[0][0] = N;
  152. for (i = 0; i < m + 1; i++)
  153. {
  154. for (int j = 0; j < m + 1; j++)
  155. {
  156. if (i + j == 0)
  157. {
  158. sumx[i][j] = N;
  159. }else
  160. sumx[i][j] = powerx[j + i - 1];
  161. }
  162. }
  163. i = 0;
  164. for (int e = 0; e < m + 1; e++)
  165. {
  166. sumx[e][m + 1] = praw[e];
  167. }
  168.  
  169. /*cout <<"matrica"<< endl;
  170. for (i = 0; i < m + 1; i++)
  171. {
  172. for (int j = 0; j < m + 2; j++)
  173. {
  174. cout << sumx[i][j] << " ";
  175. }
  176. cout << i << endl;
  177. }*/
  178.  
  179. PRES* reza = new PRES[m + 1];
  180. for (int q = 0; q < m + 1; q++)
  181. {
  182. reza[q] = praw[q];
  183. }
  184. PRES* rez = new PRES[m + 1];
  185. gaus2(sumx,reza,rez,m+1);
  186.  
  187. for (int e = 0; e < N; e++) {
  188. PRES temp = 0;
  189. //cout << rez[e] << " reaulgtn ";
  190. for (int q = 0; q < m+1; q++)
  191. {
  192. //if(q!=1)
  193. temp += rez[q]*pow(x[e],q);
  194.  
  195. }
  196. temp = pow(10.0, temp);
  197. y2[e] = temp;
  198. temp = 0;
  199. cout << "y1=";
  200. cout << setprecision (30)<< y0[e]<<endl;
  201. cout<< "y2=";
  202. cout << setprecision (30)<< y2[e]<<endl;
  203. }
  204.  
  205. ofstream foutx("D:/x.dat", ios_base::out | ios_base::trunc | ios_base::binary); // создаём объект класса ofstream для записи
  206.  
  207. for (i = 0; i < N; i++) // запись в файл x(i)
  208. {
  209. w = x[i];
  210. foutx.write((char*)& w, sizeof w);
  211. }
  212.  
  213. for (i = 0; i < N; i++) // запись в файл y.экпер(i)
  214. {
  215. w = y0[i];
  216. foutx.write((char*)& w, sizeof w);
  217. }
  218.  
  219. for (i = 0; i < N; i++) // запись в файл y.теор(i)
  220. {
  221. w = y2[i];
  222. foutx.write((char*)& w, sizeof w);
  223. }
  224.  
  225. foutx.close(); // закрываем файл
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement