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 := [:a :b :c :d | a * d - b * c].
  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. jacobian := [:func1 :func2 :x :y :h |
  14. | dx1 dy1 dx2 dy2 |
  15. dx1 := partialDerivative value: func1 value: x value: y value: h value: 'x'.
  16. dy1 := partialDerivative value: func1 value: x value: y value: h value: 'y'.
  17. dx2 := partialDerivative value: func2 value: x value: y value: h value: 'x'.
  18. dy2 := partialDerivative value: func2 value: x value: y value: h value: 'y'.
  19. ^ det2x2 value: dx1 value: dy1 value: dx2 value: dy2.
  20. ].
  21. aN := [:func1 :func2 :x :y :h |
  22. | fx fy dfy1 dfy2 |
  23. fx := func1 value: x value: y.
  24. fy := func2 value: x value: y.
  25. dfy1 := partialDerivative value: func1 value: x value: y value: h value: 'y'.
  26. dfy2 := partialDerivative value: func2 value: x value: y value: h value: 'y'.
  27. ^ det2x2 value: fx value: dfy1 value: fy value: dfy2.
  28. ].
  29. bN := [:func1 :func2 :x :y :h |
  30. | dfx1 fx dfx2 fy |
  31. dfx1 := partialDerivative value: func1 value: x value: y value: h value: 'x'.
  32. fx := func1 value: x value: y.
  33. dfx2 := partialDerivative value: func2 value: x value: y value: h value: 'x'.
  34. fy := func2 value: x value: y.
  35. ^ det2x2 value: dfx1 value: fx value: dfx2 value: fy.
  36. ].
  37.  
  38. " Функция для вычисления метода Ньютона "
  39. newtonMethod := [:func1 :func2 :x0 :y0 :h :epsilon |
  40. | x y continueIteration |
  41. x := x0.
  42. y := y0.
  43. continueIteration := true.
  44. [continueIteration] whileTrue: [
  45. | jacobianValue aNValue bNValue newX newY |
  46. jacobianValue := jacobian value: func1 value: func2 value: x value: y value: h.
  47. aNValue := aN value: func1 value: func2 value: x value: y value: h.
  48. bNValue := bN value: func1 value: func2 value: x value: y value: h.
  49. newX := x - aNValue / jacobianValue.
  50. newY := y - bNValue / jacobianValue.
  51. (newX - x) abs <= epsilon and: [(newY - y) abs <= epsilon] ifTrue: [continueIteration := false] ifFalse: [x := newX. y := newY].
  52. ].
  53. ^ Array with: x with: y.
  54. ].
  55.  
  56. " Начальные значения и параметры "
  57. x0 := 1.0.
  58. y0 := 1.0.
  59. h := 1e-6.
  60. epsilon := 1e-10.
  61.  
  62. " Вычисление метода Ньютона "
  63. | result |
  64. result := newtonMethod value: f value: g value: x0 value: y0 value: h value: epsilon.
  65. Transcript show: 'x = ', result first printString, ', y = ', result second printString.
  66. " Вычисление метода
Success #stdin #stdout #stderr 0.01s 10548KB
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:63)
Object: nil error: did not understand #first
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
UndefinedObject(Object)>>doesNotUnderstand: #first (SysExcept.st:1448)
UndefinedObject>>executeStatements (prog:65)
stderr
./prog:66: Unterminated comment, attempting recovery