Advertisement
NyanCoder

Euler.c

May 23rd, 2022
585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.59 KB | None | 0 0
  1. #include "stdlib.h"
  2. #include "stdio.h"
  3. #include "math.h"
  4.  
  5. double f(double x, double y) {
  6.     return x*y;
  7. }
  8.  
  9. double Y(double x) {
  10.     return exp(x*x/2);
  11. }
  12.  
  13. int main(int argc, char ** argv) {
  14.     const double x0 = 0;
  15.     const double y0 = 1;
  16.     const double b = 1;
  17.     const double eps = 0.0001;
  18.     const int m = 1;
  19.     const double h = pow(eps, 1.0 / m);
  20.     const int n = (b - x0) / h;
  21.  
  22.     double x = x0;
  23.     double y = y0;
  24.     for (int i = 1; i <= n; i++) {
  25.         y += f(x, y) * h;
  26.         x = x0 + i * h;
  27.     }
  28.  
  29.     printf("%f", fabs(Y(b) - y));
  30.  
  31.     return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement