Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <nag.h>
  3. #include <stdio.h>
  4. #include <nag_stdlib.h>
  5. #include <nagd02.h>
  6. #include <nagx01.h>
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. static void NAG_CALL fcn(Integer neq, double x, double eps, const double y[],
  11. double f[], Nag_User *comm);
  12. static void NAG_CALL g(Integer neq, double eps, const double ya[],
  13. const double yb[], double bc[], Nag_User *comm);
  14. #ifdef __cplusplus
  15. }
  16. #endif
  17. #define NEQ 2
  18. #define MNP 40
  19. #define Y(I, J) y[(I) *tdy + J]
  20.  
  21. int main()
  22. {
  23.     static Integer use_comm[6] = {1, 1, 1, 1, 1, 1};
  24.     double *abt = 0;
  25.     double deleps;
  26.     double tol;
  27.     double *x = 0, *y = 0;
  28.     Integer exit_status = 0;
  29.     Integer i, j;
  30.     Integer np;
  31.     Integer numbeg, nummix;
  32.     Integer neq, mnp, tdy;
  33.     Nag_User comm;
  34.     NagError fail;
  35.     INIT_FAIL(fail);
  36.     comm.p = (Pointer)&use_comm;
  37.     neq = NEQ;
  38.     mnp = MNP;
  39.     if (neq >= 1)
  40.     {
  41.     if (!(abt = NAG_ALLOC(neq, double)) ||
  42.     !(x = NAG_ALLOC(mnp, double)) ||
  43.     !(y = NAG_ALLOC(neq*mnp, double)))
  44.     {
  45.     printf("Allocation failure\n");
  46.     exit_status = -1;
  47.     goto END;
  48.     }
  49.     tdy = mnp;
  50.     }
  51.     else
  52.     {
  53.     exit_status = 1;
  54.     return exit_status;
  55.     }
  56.     tol = 1.0e-4;
  57.     np = 17;
  58.     numbeg = 1;
  59.     nummix = 0;
  60.     x[0] = 0.0;
  61.     x[np-1] = nag_pi;
  62.     deleps = 0.1;
  63.     nag_ode_bvp_fd_nonlin_gen(neq, &deleps, fcn, numbeg, nummix, g,
  64.     Nag_DefInitMesh, mnp, &np, x, y, tol, abt, NULLFN,
  65.     NULLFN, NULLFN, NULLFN, &comm, &fail);
  66.     if (fail.code != NE_NOERROR)
  67.     {
  68.     printf("Error from nag_ode_bvp_fd_nonlin_gen (d02rac).\n%s\n",
  69.     fail.message);
  70.     exit_status = 1;
  71.     goto END;
  72.     }
  73.     printf("Solution on final mesh of %"" points \n", np);
  74.     printf(" X Y(1) Y(2) Y(3)\n");
  75.     for (j = 0; j < np; ++j)
  76.     {
  77.     printf(" %9.3f ", x[j]);
  78.     for (i = 0; i < neq; ++i)
  79.     printf(" %9.4f", Y(i, j));
  80.     printf("\n");
  81.     }
  82.     printf("\n\nMaximum estimated error by components \n");
  83.     for (i = 1; i <= 3; ++i)
  84.     printf(" %11.2e", abt[i-1]);
  85.     printf(" \n");
  86.     END:
  87.     NAG_FREE(abt);
  88.     NAG_FREE(x);
  89.     NAG_FREE(y);
  90.     return exit_status;
  91. }
  92. static void NAG_CALL fcn(Integer neq, double x, double eps, const double y[],
  93. double f[], Nag_User *comm)
  94. {
  95. Integer *use_comm = (Integer *)comm->p;
  96. if (use_comm[0])
  97. {
  98. printf("(User-supplied callback fcn, first invocation.)\n");
  99. use_comm[0] = 0;
  100. }
  101. f[0] = -y[1];
  102. f[1] = y[0];
  103. }
  104.  
  105. static void NAG_CALL g(Integer neq, double eps, const double ya[],
  106. const double yb[], double bc[], Nag_User *comm)
  107. {
  108. Integer *use_comm = (Integer *)comm->p;
  109. if (use_comm[1])
  110. {
  111. printf("(User-supplied callback g, first invocation.)\n");
  112. use_comm[1] = 0;
  113. }
  114. bc[0] = yb[0]-0;
  115. bc[1] = ya[1]-1;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement