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. det2x2
  15. value: (partialDerivative value: func1 value: x value: y value: h value: 'x')
  16. value: (partialDerivative value: func1 value: x value: y value: h value: 'y')
  17. value: (partialDerivative value: func2 value: x value: y value: h value: 'x')
  18. value: (partialDerivative value: func2 value: x value: y value: h value: 'y')].
  19. aN := [:func1 :func2 :x :y :h |
  20. det2x2
  21. value: (func1 value: x value: y)
  22. value: (partialDerivative value: func1 value: x value: y value: h value: 'y')
  23. value: (func2 value: x value: y)
  24. value: (partialDerivative value: func2 value: x value: y value: h value: 'y')].
  25. bN := [:func1 :func2 :x :y :h |
  26. det2x2
  27. value: (partialDerivative value: func1 value: x value: y value: h value: 'x')
  28. value: (func1 value: x value: y)
  29. value: (partialDerivative value: func2 value: x value: y value: h value: 'x')
  30. value: (func2 value: x value: y)].
  31.  
  32. " Функция для вычисления метода Ньютона "
  33. newtonMethod := [:func1 :func2 :x0 :y0 :h :epsilon |
  34. | x y continueIteration |
  35. x := x0.
  36. y := y0.
  37. continueIteration := true.
  38. [continueIteration] whileTrue: [
  39. | jacobianValue aNValue bNValue newX newY |
  40. jacobianValue := jacobian value: func1 value: func2 value: x value: y value: h.
  41. aNValue := aN value: func1 value: func2 value: x value: y value: h.
  42. bNValue := bN value: func1 value: func2 value: x value: y value: h.
  43. newX := x - aNValue / jacobianValue.
  44. newY := y - bNValue / jacobianValue.
  45. (newX - x) abs <= epsilon and: [(newY - y) abs <= epsilon] ifTrue: [continueIteration := false] ifFalse: [x := newX. y := newY].
  46. ].
  47. ^ Array with: x with: y].
  48.  
  49. " Начальные значения и параметры "
  50. x0 := 1.0.
  51. y0 := 1.0.
  52. h := 1e-6.
  53. epsilon := 1e-10.
  54.  
  55. " Вычисление метода Ньютона "
  56. | result |
  57. result := newtonMethod value: f value: g value: x0 value: y0 value: h value: epsilon.
  58. Transcript show: 'x = ', result first printString, ', y = ', result second printString.
Success #stdin #stdout 0.02s 10476KB
stdin
Standard input is empty
stdout
Object: BlockClosure new "<0x7f4a7d8a3e70>" 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:56)
Object: nil error: did not understand #first
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
UndefinedObject(Object)>>doesNotUnderstand: #first (SysExcept.st:1448)
UndefinedObject>>executeStatements (prog:58)