NyanCoder

LaGrange.c

May 15th, 2022 (edited)
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.93 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "math.h"
  3.  
  4. float calculateX(float xFrom, float i, float step) {
  5.     return xFrom + i*step;
  6. }
  7. float yFormula(float x) {
  8.     return exp(-(x - 2) * (x - 2));
  9. }
  10.  
  11. void printLBase(int i, int n, float xFrom, float step) {
  12.     printf("(");
  13.     for (int j = 0; j <= n; j++) {
  14.         if (j == i)
  15.             continue;
  16.         printf("(x - %f)", calculateX(xFrom, j, step));
  17.     }
  18.     printf(")/(");
  19.     for (int j = 0; j <= n; j++) {
  20.         if (j == i)
  21.             continue;
  22.         printf("(%f - %f)", calculateX(xFrom, i, step), calculateX(xFrom, j, step));
  23.     }
  24.     printf(")");
  25. }
  26.  
  27. int main(int argc, char** argv) {
  28.     float xFrom = 2;
  29.     float xTo = 4;
  30.     float step = 0.2;
  31.  
  32.     int n = (int)((xTo-xFrom) / step);
  33.  
  34.     printf("L(x) = ");
  35.     for (int i = 0; i <= n; i++) {
  36.         printLBase(i, n, xFrom, step);
  37.         if (i < n)
  38.             printf(" + ");
  39.     }
  40.  
  41.     return 0;
  42. }
Add Comment
Please, Sign In to add comment