rk.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* This file is part of the GNU plotutils package. */
  2. /*
  3. * Copyright (C) 1982-1994, Nicholas B. Tufillaro. All rights reserved.
  4. *
  5. * GNU enhancements Copyright (C) 1996, 1997, 2005, Free Software
  6. * Foundation, Inc.
  7. */
  8. /*
  9. * Fourth-Order Runge-Kutta
  10. *
  11. */
  12. #include "sys-defines.h"
  13. #include "ode.h"
  14. #include "extern.h"
  15. void
  16. rk (void)
  17. {
  18. double t;
  19. double halfstep = HALF * tstep;
  20. double onesixth = 1.0 / 6.0;
  21. for (it = 0, t = tstart; !STOPR; t = tstart + (++it) * tstep)
  22. {
  23. symtab->sy_val[0] = symtab->sy_value = t;
  24. field();
  25. for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link)
  26. {
  27. fsp->sy_val[0] = fsp->sy_value;
  28. fsp->sy_pri[0] = fsp->sy_prime;
  29. }
  30. /* output */
  31. printq();
  32. for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link)
  33. {
  34. fsp->sy_k[0] = tstep * fsp->sy_prime;
  35. fsp->sy_value = fsp->sy_val[0] + HALF * fsp->sy_k[0];
  36. }
  37. symtab->sy_value = t + halfstep;
  38. field();
  39. for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link)
  40. {
  41. fsp->sy_k[1] = tstep * fsp->sy_prime;
  42. fsp->sy_value = fsp->sy_val[0] + HALF * fsp->sy_k[1];
  43. }
  44. symtab->sy_value = t + halfstep;
  45. field();
  46. for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link)
  47. {
  48. fsp->sy_k[2] = tstep * fsp->sy_prime;
  49. fsp->sy_value = fsp->sy_val[0] + fsp->sy_k[2];
  50. }
  51. symtab->sy_value = t + tstep;
  52. field();
  53. for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link)
  54. fsp->sy_k[3] = tstep * fsp->sy_prime;
  55. for (fsp = dqueue; fsp != NULL; fsp = fsp->sy_link)
  56. fsp->sy_value = fsp->sy_val[0]
  57. + onesixth * (fsp->sy_k[0]
  58. + TWO * fsp->sy_k[1]
  59. + TWO * fsp->sy_k[2]
  60. + fsp->sy_k[3]);
  61. }
  62. }