Ladies_Man

diff equation accepted

Jun 17th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include <iostream>
  2. #include "stdio.h"
  3. #include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. #define xtype double
  8.  
  9. #define N 11
  10.  
  11. //y''(x) + p(x)y'(x) + q(x)y(x) = f(x)
  12. //y(a) = A
  13. //y(b) = B
  14.  
  15. xtype y(xtype x) { return 3*x*x*x; }
  16. xtype dy(xtype x) { return 9*x*x; }
  17. xtype ddy(xtype x) { return 18*x; }
  18.  
  19. xtype pFunc(xtype x) { return 1; }
  20. xtype qFunc(xtype x) { return -1; }
  21.  
  22.  
  23. xtype f[N], p[N], q[N];
  24.  
  25. xtype x_A = 1;
  26. xtype x_B = 2;
  27.  
  28. xtype y_A = y(x_A);
  29. xtype y_B = y(x_B);
  30.  
  31. xtype h = (x_B - x_A) / (N-1);
  32.  
  33.  
  34.  
  35. void solveMatrix(int n, xtype* a, xtype* b, xtype* c, xtype* d, xtype* x) {
  36.  
  37.     int i;
  38.     xtype alpha[n], beta[n];
  39.     alpha[0] = -c[0] / b[0];
  40.     beta[0] = d[0] / b[0];
  41.  
  42.     for (i = 1; i < n; i++) {
  43.         alpha[i] = (-c[i]) / (a[i] * alpha[i-1] + b[i]);
  44.         beta[i] = (d[i] - a[i] * beta[i-1]) / (a[i] * alpha[i-1] + b[i]);
  45.     }
  46.  
  47.     x[n-1] = beta[n-1];
  48.  
  49.     for (i = n-2; i >= 0; i--) {
  50.         x[i] = alpha[i] * x[i+1] + beta[i];
  51.     }
  52. }
  53.  
  54. void countFunc(int n) {
  55.     for (int i = 0; i < n; i++) {
  56.  
  57.         xtype x = x_A + i*h;
  58.  
  59.         f[i] = ddy(x) + pFunc(x)*dy(x) + qFunc(x)*y(x);
  60.         p[i] = pFunc(x);
  61.         q[i] = qFunc(x);
  62.     }
  63.  
  64. }
  65.  
  66. void fillAndSolveMatrix(int n) {
  67.  
  68.     xtype a[n], b[n], c[n], d[n], e[n], res[n];
  69.  
  70.     /*    a[0] = 0;
  71.     b[0] = q[1]*h*h - 2;
  72.     c[0] = 1 + p[1]*h/2;
  73.     d[0] = f[1]*h*h - y_A*(1 - p[1]*h/2);
  74.  
  75.     a[n-3] = 1 - p[n-2]*h/2;
  76.     b[n-3] = q[n-2]*h*h - 2;
  77.     c[n-3] = 0;
  78.     d[n-3] = f[n-2]*h*h - y_B*(1 + p[n-2]*h/2);*/
  79.  
  80.  
  81.     for (int i = 0; i < n; i++) {
  82.  
  83.         a[i] = 1 - p[i+1]*h/2;
  84.         b[i] = q[i+1]*h*h - 2;
  85.         c[i] = 1 + p[i+1]*h/2;
  86.         d[i] = f[i+1]*h*h;
  87.     }
  88.  
  89.     a[0] = 0;
  90.     b[0] = q[1]*h*h - 2;
  91.     c[0] = 1 + p[1]*h/2;
  92.     d[0] = f[1]*h*h - (1 - p[1]*h/2)*y_A;
  93.  
  94.     a[n-2] = 1 - p[n-1]*h/2;
  95.     b[n-2] = q[n-2]*h*h - 2;
  96.     c[n-2] = 0;
  97.     d[n-2] = f[n-1]*h*h - (1 + p[n-1]*h/2)*y_B;
  98.  
  99.  
  100.  
  101.     solveMatrix(n-1, a, b, c, d, e);
  102.  
  103.     for (int i = 0; i < n-1; i++) res[i+1] = e[i];
  104.     res[0] = y_A;
  105.     res[n] = y_B;
  106.  
  107.     for (int i = 0; i <= n; i++) {
  108.         printf("x[%d]=%.6f y[%d]=%.6f res[%d]=%.6f d[%d]=%.6f\n",
  109.                 i, x_A+i*h, i, y(x_A+i*h), i, res[i], i, fabs(y(x_A+i*h)-res[i]));
  110.     }
  111. }
  112.  
  113.  
  114.  
  115. int main()
  116. {
  117.  
  118.     countFunc(N);
  119.     fillAndSolveMatrix(N-1);
  120.  
  121.     return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment