You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

13 lines
494 B

3 years ago
  1. #include <stdbool.h>
  2. #include <stdint.h>
  3. #include "least_square_method.h"
  4. void least_square_method(int xstart, int xtedp, int xend, gety_t gety, float *c, float *k) {
  5. double xi = 0, x2 = 0, yi = 0, xy = 0;
  6. int xnow = 0;
  7. int maxn = 0;
  8. for (xnow = xstart; xnow <= xend; xnow += xtedp) {
  9. xi += xnow, x2 += xnow * xnow, yi += gety(xnow), xy += xnow * gety(xnow);
  10. maxn++;
  11. }
  12. *c = (yi * x2 - xy * xi) / (x2 * maxn - xi * xi);
  13. *k = (yi * xi - xy * maxn) / (xi * xi - x2 * maxn);
  14. }