Advertisement
cd62131

Runge-Kutta

Feb 21st, 2014
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #define _USE_MATH_DEFINES
  3. #include <math.h>
  4. #define G 9.80665
  5. #define N 1000
  6. double f(double t) {
  7.   return 780. * cos(M_PI_4);
  8. }
  9. double g(double t) {
  10.   return 780. * sin(M_PI_4) - G * t;
  11. }
  12. void runge(int n, double t_begin, double t_end, double x0, double y0, double *x,
  13.     double *y) {
  14.   int i;
  15.   double t, h, h2, f1, f2, f3, f4, g1, g2, g3, g4;
  16.   t = t_begin;
  17.   h = (t_end - t_begin) / n;
  18.   h2 = h / 2;
  19.   *x = x0;
  20.   *y = y0;
  21.   for (i = 1; i <= n; i++) {
  22.     f1 = h * f(t);
  23.     f2 = h * f(t + h2);
  24.     f3 = h * f(t + h2);
  25.     f4 = h * f(t + h);
  26.     g1 = h * g(t);
  27.     g2 = h * g(t + h2);
  28.     g3 = h * g(t + h2);
  29.     g4 = h * g(t + h);
  30.     t += h;
  31.     *x += (f1 + 2 * f2 + 2 * f3 + f4) / 6;
  32.     *y += (g1 + 2 * g2 + 2 * g3 + g4) / 6;
  33.   }
  34. }
  35. int main(void) {
  36.   double t_begin = 0., t_end = 1000., x0 = 0., y0 = 0., x, y;
  37.   runge(N, t_begin, t_end, x0, y0, &x, &y);
  38.   printf("% -lf % -lf\n", x, y);
  39.   printf("% -lf % -lf\n", 780. * cos(M_PI_4) * 1000.,
  40.     780. * sin(M_PI_4) * 1000. - G / 2. * 1000. * 1000.);
  41.   return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement