Advertisement
NyanCoder

Euler.c

May 23rd, 2022
455
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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
RAW Paste Data Copied
Advertisement