Guest User

Untitled

a guest
Dec 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. const E = 0.1;
  2.  
  3. function Vec2(x1, x2) {
  4. this.x1 = x1;
  5. this.x2 = x2;
  6. }
  7.  
  8. const calcMatrix = (m, s) => {
  9. return [[m[0][0](s), m[0][1](s)], [m[1][0](s), m[1][1](s)]];
  10. };
  11.  
  12. const calcInversed = (m) => {
  13. const k = 1.0 / (m[0][0] * m[1][1] - m[0][1] * m[1][0]);
  14.  
  15. return [[k * m[1][1], -k * m[0][1]], [-k * m[1][0], k * m[0][0]]];
  16. };
  17.  
  18. const f = (p) =>
  19. 2.0 * Math.pow(p.x1, 4) +
  20. Math.pow(p.x2, 4) +
  21. Math.pow(p.x1, 2) * p.x2 -
  22. 5.0 * p.x1 * p.x2 +
  23. 3 * Math.pow(p.x1, 2) +
  24. Math.pow(p.x2, 2) +
  25. 8 * p.x2;
  26.  
  27. const grad = (p) =>
  28. new Vec2(
  29. 8.0 * Math.pow(p.x1, 3) + 2.0 * p.x1 * (p.x2 + 3) - 5.0 * p.x2,
  30. Math.pow(p.x1, 2) - 5 * p.x1 + 4.0 * Math.pow(p.x2, 3) + 2.0 * p.x2 + 8
  31. );
  32.  
  33. const GESSE = [
  34. [(p) => 24.0 * Math.pow(p.x1, 2) + 2.0 * p.x2 + 3.0, (p) => 2.0 * p.x1 - 5.0],
  35. [(p) => 2.0 * p.x1 - 5.0, (p) => 12.0 * Math.pow(p.x2, 2) + 2.0]
  36. ];
  37.  
  38. let S = new Vec2(2.0, 1.0);
  39.  
  40. let h = 0.1;
  41.  
  42. let current = f(S);
  43.  
  44. let last,
  45. g,
  46. i = 0;
  47.  
  48. while (true) {
  49. last = f(S);
  50. const lS = S;
  51.  
  52. g = grad(S);
  53.  
  54. if (Math.sqrt(g.x1 * g.x1 + g.x2 * g.x2) <= E) {
  55. break;
  56. }
  57.  
  58. const D1 = GESSE[0][0](S);
  59. const D2 = GESSE[0][0](S) * GESSE[1][1](S) - GESSE[0][1](S) * GESSE[1][0](S);
  60.  
  61. const H = calcMatrix(GESSE, S);
  62. const INVH = calcInversed(H);
  63.  
  64. const p = new Vec2(
  65. INVH[0][0] * g.x1 + INVH[0][1] * g.x2,
  66. INVH[1][0] * g.x1 + INVH[1][0] * g.x2
  67. );
  68.  
  69. S = new Vec2(S.x1 - p.x1, S.x2 - p.x2);
  70.  
  71. console.log(`Итерация ${i}<br>`);
  72. console.log(
  73. `<i>x<sup>(${i})</sup> =`,
  74. `(${lS.x1.toFixed(3)}, ${lS.x2.toFixed(3)})<sup>T</sup></i>`,
  75. '&nbsp;&nbsp;&nbsp;&nbsp;'
  76. );
  77. console.log(
  78. `<i>f(x<sup>(${i})</sup>) = ${last.toFixed(3)}</i>`,
  79. '&nbsp;&nbsp;&nbsp;&nbsp;'
  80. );
  81. console.log(
  82. `<i>∇f(x<sup>(${i})</sup>) =`,
  83. `(${lS.x1.toFixed(3)}, ${lS.x2.toFixed(3)})<sup>T</sup></i><br>`
  84. );
  85. console.log(
  86. `Матрица Гессе в точке <i>x<sup>(${i})</sup>: H(x<sup>(${i})</sup>) =`,
  87. `<table style="border-left: 1px solid black; border-right: 1px solid black"><tr>`,
  88. `<td>${H[0][0].toFixed(3)}</td>`,
  89. `<td>${H[0][1].toFixed(3)}</td></tr><tr>`,
  90. `<td>${H[1][0].toFixed(3)}</td>`,
  91. `<td>${H[1][1].toFixed(3)}</td></tr></table></i>`
  92. );
  93.  
  94. console.log(
  95. `<i>p<sup>(${i})</sup> =`,
  96. `(${-p.x1.toFixed(3)}, ${-p.x2.toFixed(3)})<i>`,
  97. '&nbsp;&nbsp;&nbsp;&nbsp;'
  98. );
  99. console.log(
  100. `<i>x<sup>(${i})</sup> =`,
  101. `(${S.x1.toFixed(3)}, ${S.x2.toFixed(3)})`,
  102. `<br><br>`
  103. );
  104.  
  105. i++;
  106. }
  107.  
  108. console.log(
  109. `Результат: <i>f(x<sup>(k)</sup>)<sub>min</sub> =`,
  110. `${f(S).toFixed(3)}</i>, при <i>x<sup>(k)</sup> =`,
  111. `(${S.x1.toFixed(3)}, ${S.x2.toFixed(3)})</i>`
  112. );
Add Comment
Please, Sign In to add comment