Advertisement
Guest User

Laba6

a guest
Jun 25th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const killFloat = number => Number(number.toFixed(2)); // потому что в js неправильное деление и умножение
  2. // чисел типа double
  3.  
  4. const epsilon = 0.5 * Math.pow(10, -5);
  5.  
  6. const RungeKutta3 = (xi, y, p, h, derivative) => {
  7.     const yi = y[killFloat(xi * 10)];
  8.     const pi = p[killFloat(xi * 10)];
  9.     const k1 = h * pi;
  10.     const l1 = h * (yi + 2.6 * Math.pow(xi, 3) - 2.6 * xi * xi + 5.2);
  11.     const k2 = h * (pi + l1 / 2);
  12.     const l2 = h * (yi + k1 / 2 + 2.6 * Math.pow(xi + h / 2, 3) - 2.6 * Math.pow(xi + h / 2, 2) + 5.2);
  13.     const k3 = h * (pi + 2 * l2 - l1);
  14.     const l3 = h * (yi + 2 * k2 - k1 + 2.6 * Math.pow(xi + h, 3) - 2.6 * Math.pow(xi + h, 2) + 5.2);
  15.     const yNew = yi + (k1 + 4 * k2 + k3) / 6;
  16.     const pNew = pi + (l1 + 4 * l2 + l3) / 6;
  17.     const xNew = killFloat(xi + h);
  18.     y[killFloat(xNew * 10)] = yNew;
  19.     p[killFloat(xNew * 10)] = pNew;
  20.     // console.info(xNew);
  21.     // console.info('RK-3:', xNew, yNew);
  22.     // console.info('Actual:', xNew, derivative(xNew));
  23.     if (xNew >= 5) {
  24.         resultt = yNew;
  25.         return yNew;
  26.     }
  27.     RungeKutta3(xNew, y, p, h, derivative);
  28. }
  29.  
  30. const thirdDerAnswer = x => Math.pow(Math.E, x) + 2 * Math.pow(Math.E, -x) - 2.6 * x * x * x + 2.6 * x * x - 15.6 * x;
  31.  
  32. const shooting = () => {
  33.     const h = 0.2;
  34.     const actualY = Math.pow(Math.E, 5) + 2 * Math.pow(Math.E, -5) - 338;
  35.     let left = 0;
  36.     let right = 5;
  37.     let root = 0;
  38.     const y = [];
  39.     const x = [];
  40.     const p = [];
  41.     p[0] = -16.6;
  42.     y[0] = 0;
  43.     while (Math.abs(y[y.length - 1] - actualY) > epsilon) {
  44.         const c = (left + right) / 2;
  45.         y[0] = left;
  46.         RungeKutta3(0, y, p, h, thirdDerAnswer);
  47.         const res1 = y[y.length - 1] - actualY;
  48.         y[0] = c;
  49.         RungeKutta3(0, y, p, h, thirdDerAnswer)
  50.         const res2 = y[y.length - 1] - actualY;
  51.         root = res1 * res2;
  52.         if (root >= 0) {
  53.             left = c;
  54.         } else {
  55.             right = c;
  56.         }
  57.     }
  58.     let current = 0;
  59.     while (current <= 5) {
  60.         // console.info('Actual:', current, thirdDerAnswer(current));
  61.         // console.info(y[killFloat(current * 10)]);
  62.         current = killFloat(current + h);
  63.     }
  64. }
  65.  
  66. const my_q = x => 2.6 * x * x * x - 2.6 * x * x + 5.2;
  67.  
  68. const myProgonka = () => {
  69.     const h = 0.05;
  70.     const mu = [];
  71.     const y = [];
  72.     const lambda = [];
  73.     mu[0] = 16.6 * h;
  74.     lambda[0] = 1;
  75.     let x = h;
  76.     for (let i = 1; i < killFloat(5 / h); i++) {
  77.         lambda[i] = 1 / (2 + h * h - lambda[i - 1]);
  78.         mu[i] = (my_q(x) * h * h - mu[i - 1]) / (lambda[i - 1] - 2 - h * h);
  79.         x = killFloat(x + h);
  80.     }
  81.     y[lambda.length] = Math.pow(Math.E, 5) - 2 * Math.pow(Math.E, -5) - 338;
  82.     let current = killFloat(5 - h);
  83.     for (let i = killFloat(5 / h) - 1; i > -1; i--) {
  84.         y[i] = lambda[i] * y[i + 1] + mu[i];
  85.         // console.info('Computed:', y[i]);
  86.         // console.info('Actual:', current, thirdDerAnswer(current));
  87.         current = killFloat(current - h);
  88.     }
  89.     for (let num of y) {
  90.         // console.info(num);
  91.     }
  92. }
  93.  
  94. myProgonka();
  95.  
  96. shooting();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement