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 valueWithArguments: {func1. x. y. h. 'x'}.
  16. dy1 := partialDerivative valueWithArguments: {func1. x. y. h. 'y'}.
  17. dx2 := partialDerivative valueWithArguments: {func2. x. y. h. 'x'}.
  18. dy2 := partialDerivative valueWithArguments: {func2. x. y. h. 'y'}.
  19. ^ det2x2 valueWithArguments: {dx1. dy1. dx2. 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 valueWithArguments: {func1. x. y. h. 'y'}.
  26. dfy2 := partialDerivative valueWithArguments: {func2. x. y. h. 'y'}.
  27. ^ det2x2 valueWithArguments: {fx. dfy1. fy. dfy2}.
  28. ].
  29. bN := [:func1 :func2 :x :y :h |
  30. | dfx1 fx dfx2 fy |
  31. dfx1 := partialDerivative valueWithArguments: {func1. x. y. h. 'x'}.
  32. fx := func1 value: x value: y.
  33. dfx2 := partialDerivative valueWithArguments: {func2. x. y. h. 'x'}.
  34. fy := func2 value: x value: y.
  35. ^ det2x2 valueWithArguments: {dfx1. fx. dfx2. 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 valueWithArguments: {func1. func2. x. y. h}.
  47. aNValue := aN valueWithArguments: {func1. func2. x. y. h}.
  48. bNValue := bN valueWithArguments: {func1. func2. x. y. h}.
  49. newX := x - (aNValue / jacobianValue).
  50. newY := y - (bNValue / jacobianValue).
  51. ((newX - x) abs <= epsilon and: [(newY - y) abs <= epsilon])
  52. ifTrue: [continueIteration := false]
  53. ifFalse: [x := newX. y := newY].
  54. ].
  55. ^ Array with: x with: y.
  56. ].
  57.  
  58. " Начальные значения и параметры "
  59. x0 := 1.0.
  60. y0 := 1.0.
  61. h := 1e-6.
  62. epsilon := 1e-10.
  63.  
  64. " Вычисление метода Ньютона "
  65. | result |
  66. result := newtonMethod valueWithArguments: {f. g. x0. y0. h. epsilon}.
  67. result ifNotNil: [
  68. Transcript show: 'x = ', result first printString, ', y = ', result second printString; cr.
  69. ] ifNil: [
  70. Transcript show: 'Метод Ньютона не сошелся'; cr.
  71. ].
  72.  
Success #stdin #stdout 0.01s 10608KB
stdin
Standard input is empty
stdout
Object: -5.00000200017781 error: return from a dead method context
SystemExceptions.BadReturn(Exception)>>signal (ExcHandling.st:254)
SystemExceptions.BadReturn class(Exception class)>>signal (ExcHandling.st:151)
FloatD(Object)>>badReturnError (Object.st:1389)
[] in UndefinedObject>>executeStatements (prog:46)
BlockClosure>>whileTrue: (BlkClosure.st:328)
[] in UndefinedObject>>executeStatements (prog:43)
UndefinedObject>>executeStatements (prog:66)
Метод Ньютона не сошелся