Advertisement
Guest User

Untitled

a guest
Feb 28th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.07 KB | None | 0 0
  1. //LAB 4
  2.  
  3. //LA GRANGE
  4.  
  5. #include <math.h>
  6. #include <stdio.h>
  7.  
  8. double factorial(const int x);
  9. double L(const double *X, const int k, const int n, const double x);
  10. double E(const double *X, const int xi, const int n, const double x);
  11.  
  12. double factorial(const int x)
  13. {
  14. int i;
  15. double fact = 1;
  16. for (i = 1; i <= x; ++i) {
  17. fact *= i;
  18. }
  19. return fact;
  20. }
  21. double L(const double *X, const int k, const int n, const double x)
  22. {
  23. int i;
  24. double P = 1;
  25. for (i = 0; i <= n; ++i) {
  26. if (i != k) {
  27. P *= (x - X[i]) / (X[k] - X[i]);
  28. }
  29. }
  30. return P;
  31. }
  32. double E(const double *X, const int xi, const int n, const double x)
  33. {
  34. int i;
  35. double P = cos(xi + (n + 1) * M_PI / 2) / factorial(n + 1);
  36. for (i = 0; i <= n; ++i) {
  37. P *= x - X[i];
  38. }
  39. return P;
  40. }
  41.  
  42. main()
  43. {
  44.  
  45. const double X[4] = {0, M_PI / 4, M_PI / 3, M_PI / 2};
  46. const double Y[4] = {1, sqrt(2) / 2, 1. / 2, 0};
  47. int i;
  48. double S = 0, fE = 0;
  49. for (i = 0; i < 4; ++i) {
  50. double fEi = fabs(E(X, X[i], 3, M_PI / 12));
  51. if (fE < fEi) {
  52. fE = fEi;
  53. }
  54. S += L(X, i, 3, M_PI / 12) * Y[i];
  55. }
  56. printf("cos(pi/12)=%f\n", S);
  57.  
  58. printf("|E|<=%f\n", fE);
  59.  
  60. return 0;
  61. }
  62.  
  63. //LEAST SQUARES
  64.  
  65. #include <math.h>
  66. #include <stdio.h>
  67. #include <stdlib.h>
  68.  
  69. double ** initSquareArray(const int n);
  70. void freeSquareArray(double **A, const int n);
  71. double ** cofactor(const double **A, const int n, const int row, const int col);
  72. double determinant(const double **A, const int n);
  73.  
  74. double leftSum(const double *X, const int n, const double power);
  75. double ** leftMatrix(const double *A, const int n, const int degree);
  76. double rightSum(const double *X, const double *Y, const int n, const double power);
  77. double * rightVector(const double *X, const double *Y, const int n, const int degree);
  78. double ** replaceColumn(const int nCol, const double **A, const double *col, const int n);
  79.  
  80. double ** initSquareArray(const int n)
  81. {
  82. double **A = 0;
  83. if (A = (double **)calloc(n, sizeof(*A))) {
  84. int i;
  85. for (i = 0; i < n; ++i) {
  86. if (!(A[i] = (double *)calloc(n, sizeof(**A)))) {
  87. freeSquareArray(A, n);
  88. return 0;
  89. }
  90. }
  91. }
  92. return A;
  93. }
  94. void freeSquareArray(double **A, const int n)
  95. {
  96. int i;
  97. for (i = 0; i < n; ++i) {
  98. free(A[i]);
  99. A[i] = 0;
  100. }
  101. free(A);
  102. }
  103.  
  104. double ** cofactor(const double **A, const int n, const int row, const int col)
  105. {
  106. double **co = 0;
  107. if (co = initSquareArray(n - 1)) {
  108. int i, j, r, c;
  109. for (i = 0, r = 0; i < n; ++i) {
  110. if (i == row) {
  111. continue;
  112. }
  113. for (j = 0, c = 0; j < n; ++j) {
  114. if (j == col) {
  115. continue;
  116. }
  117. co[r][c] = A[i][j];
  118. c++;
  119. }
  120. r++;
  121. }
  122. }
  123. return co;
  124. }
  125. double determinant(const double **A, const int n)
  126. {
  127. int i, j;
  128. double det = 0;
  129. if (n < 1) {
  130. printf("Invalid matrix dimension: %d\n", n);
  131. } else if (n == 1) {
  132. det = A[0][0];
  133. } else if (n == 2) {
  134. det = A[0][0] * A[1][1] - A[1][0] * A[0][1];
  135. } else {
  136. i = 0;
  137. for (j = 0; j < n; ++j) {
  138. double **co = 0;
  139. if (co = cofactor(A, n, i, j)) {
  140. det += pow(-1, (i + 1) + (j + 1)) * A[i][j] * determinant(co, n - 1);
  141. freeSquareArray(co, n - 1);
  142. } else {
  143. freeSquareArray(co, n - 1);
  144. return 0;
  145. }
  146. }
  147. }
  148. return det;
  149. }
  150.  
  151. double leftSum(const double *X, const int n, const double power)
  152. {
  153. int i;
  154. double S = 0;
  155. for (i = 0; i < n; ++i) {
  156. S += pow(X[i], power);
  157. }
  158. return S;
  159. }
  160. double ** leftMatrix(const double *A, const int n, const int degree)
  161. {
  162. double **m = 0;
  163. if (m = initSquareArray(degree + 1)) {
  164. int i, j;
  165. for (i = 0; i <= 2 * degree; ++i) {
  166. double S = leftSum(A, n, i);
  167. for (j = 0; j <= i; ++j) {
  168. if (j < degree + 1 && i - j < degree + 1) {
  169. m[j][i - j] = S;
  170. }
  171. }
  172. }
  173. }
  174. return m;
  175. }
  176. double rightSum(const double *X, const double *Y, const int n, const double power)
  177. {
  178. int i;
  179. double S = 0;
  180. for (i = 0; i < n; ++i) {
  181. S += pow(X[i], power) * Y[i];
  182. }
  183. return S;
  184. }
  185. double * rightVector(const double *X, const double *Y, const int n, const int degree)
  186. {
  187. double *v = 0;
  188. if (v = (double *)calloc(degree + 1, sizeof(*v))) {
  189. int i;
  190. for (i = 0; i <= degree; ++i) {
  191. v[i] = rightSum(X, Y, n, i);
  192. }
  193. }
  194. return v;
  195. }
  196. double ** replaceColumn(const int nCol, const double **A, const double *col, const int n)
  197. {
  198. double **m = 0;
  199. if (m = initSquareArray(n)) {
  200. int i, j;
  201. for (i = 0; i < n; ++i) {
  202. for (j = 0; j < n; ++j) {
  203. m[i][j] = (j == nCol ? col[i] : A[i][j]);
  204. }
  205. }
  206. }
  207. return m;
  208. }
  209.  
  210. main()
  211. {
  212.  
  213. const double X[9] = {2.3, 2.5, 2.8, 3.1, 3.5, 3.8, 4, 4.3, 4.6};
  214. const double Y[9] = {39, 39, 42, 43.4, 43, 44, 45, 43, 43.5};
  215. const int degree = 1;
  216. double **left = leftMatrix(X, 9, degree);
  217. double *right = rightVector(X, Y, 9, degree);
  218. double D = determinant(left, degree + 1);
  219. int i;
  220. for (i = 0; i <= degree; ++i) {
  221. double **leftI = replaceColumn(i, left, right, degree + 1);
  222. printf("a%d=%f\n", i, determinant(leftI, degree + 1) / D);
  223. freeSquareArray(leftI, degree + 1);
  224. leftI = 0;
  225. }
  226. double R = (9 * rightSum(X, Y, 9, 1) - leftSum(X, 9, 1) * leftSum(Y, 9, 1)) / sqrt((9 * leftSum(X, 9, 2) - pow(leftSum(X, 9, 1), 2)) * (9 * leftSum(Y, 9, 2) - pow(leftSum(Y, 9, 1), 2)));
  227. printf("R=%f\n", R);
  228.  
  229. free(right);
  230. right = 0;
  231. freeSquareArray(left, degree + 1);
  232. left = 0;
  233. return 0;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement