1
0

sample.julia.txt 709 B

1234567891011121314151617181920212223
  1. # good style
  2. function fixedpointmap(f; iv, tolerance=1E-7, maxiter=1000)
  3. # setup the algorithm
  4. x_old = iv
  5. normdiff = Inf
  6. iter = 1
  7. while normdiff > tolerance && iter <= maxiter
  8. x_new = f(x_old) # use the passed in map
  9. normdiff = norm(x_new - x_old)
  10. x_old = x_new
  11. iter = iter + 1
  12. end
  13. return (value = x_old, normdiff=normdiff, iter=iter) # A named tuple
  14. end
  15. # define a map and parameters
  16. p = 1.0
  17. β = 0.9
  18. f(v) = p + β * v # note that p and β are used in the function!
  19. sol = fixedpointmap(f, iv=0.8, tolerance=1.0E-8) # don't need to pass
  20. println("Fixed point = $(sol.value), and |f(x) - x| = $(sol.normdiff) in $(sol.iter)"*
  21. " iterations")