fork download
  1. | f g det2x2 partialDerivative jacobian aN bN newtonMethod x0 y0 h epsilon |
  2.  
  3. " Функции для вычисления определителя матрицы 2х2 и частной производной "
  4. f := [:x :y | x * x + y * y - 4].
  5. g := [:x :y | x - y].
  6. det2x2 := [:dx1 :dy1 :dx2 :dy2 | dx1 * dy2 - dx2 * dy1].
  7. partialDerivative := [:func :x :y :h :derivativeType |
  8. derivativeType = 'x'
  9. ifTrue: [(func value: x + h value: y) - (func value: x value: y) / h]
  10. ifFalse: [derivativeType = 'y'
  11. ifTrue: [(func value: x value: y + h) - (func value: x value: y) / h]
  12. ifFalse: [Error signal: 'Invalid derivative type']]].
  13.  
  14. jacobian := [:func1 :func2 :x :y :h |
  15. | dx1 dy1 dx2 dy2 |
  16. dx1 := partialDerivative value: func1 value: x value: y value: h value: 'x'.
  17. dy1 := partialDerivative value: func1 value: x value: y value: h value: 'y'.
  18. dx2 := partialDerivative value: func2 value: x value: y value: h value: 'x'.
  19. dy2 := partialDerivative value: func2 value: x value: y value: h value: 'y'.
  20. ^ det2x2 value: dx1 value: dy1 value: dx2 value: dy2.
  21. ].
  22.  
  23. aN := [:func1 :func2 :x :y :h |
  24. | fx fy dfy1 dfy2 |
  25. fx := func1 value: x value: y.
  26. fy := func2 value: x value: y.
  27. dfy1 := partialDerivative value: func1 value: x value: y value: h value: 'y'.
  28. dfy2 := partialDerivative value: func2 value: x value: y value: h value: 'y'.
  29. ^ det2x2 value: fx value: dfy1 value: fy value: dfy2.
  30. ].
  31.  
  32. bN := [:func1 :func2 :x :y :h |
  33. | dfx1 fx dfx2 fy |
  34. dfx1 := partialDerivative value: func1 value: x value: y value: h value: 'x'.
  35. fx := func1 value: x value: y.
  36. dfx2 := partialDerivative value: func2 value: x value: y value: h value: 'x'.
  37. fy := func2 value: x value: y.
  38. ^ det2x2 value: dfx1 value: fx value: dfx2 value: fy.
  39. ].
  40.  
  41. " Функция для вычисления метода Ньютона "
  42. newtonMethod := [:func1 :func2 :x0 :y0 :h :epsilon |
  43. | x y continueIteration |
  44. x := x0.
  45. y := y0.
  46. continueIteration := true.
  47. [continueIteration] whileTrue: [
  48. | jacobianValue aNValue bNValue newX newY |
  49. jacobianValue := jacobian value: func1 value: func2 value: x value: y value: h.
  50. aNValue := aN value: func1 value: func2 value: x value: y value: h.
  51. bNValue := bN value: func1 value: func2 value: x value: y value: h.
  52. newX := x - aNValue / jacobianValue.
  53. newY := y - bNValue / jacobianValue.
  54. (newX - x) abs <= epsilon and: [(newY - y) abs <= epsilon] ifTrue: [continueIteration := false] ifFalse: [x := newX. y := newY].
  55. ].
  56. ^ Array with: x with: y.
  57. ].
  58.  
  59. " Начальные значения и параметры "
  60. x0 := 1.0.
  61. y0 := 1.0.
  62. h := 1e-6.
  63. epsilon := 1e-10.
  64.  
  65. " Вычисление метода Ньютона "
  66. | result |
  67. result := newtonMethod value: f value: g value: x0 value: y0 value: h value: epsilon.
  68. Transcript show: 'x = ', result first printString, ', y = ', result second printString.
Success #stdin #stdout 0.02s 10616KB
stdin
Standard input is empty
stdout
Object: BlockClosure new "<0x7f4a7d8a4660>" error: did not understand #value:value:value:value:value:value:
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
BlockClosure(Object)>>doesNotUnderstand: #value:value:value:value:value:value: (SysExcept.st:1448)
UndefinedObject>>executeStatements (prog:66)
Object: nil error: did not understand #first
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
UndefinedObject(Object)>>doesNotUnderstand: #first (SysExcept.st:1448)
UndefinedObject>>executeStatements (prog:68)