Advertisement
jukaukor

NewtonIterJuliaPi.jl

Mar 17th, 2022
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. # Newton-iteroinnilla funktion sin(x) = 0 nollakohta Pi
  2. # Juhani Kaukoranta 17.3.2022
  3. tarkkuus10=101 # desimaaliluvun tarkkuus
  4. precision = floor(Int,tarkkuus10*log2(10))+1 # bittitarkkuus
  5. setprecision(BigFloat,precision)
  6. epsilon = 1/big(10)^tarkkuus10 # vastauksen tarkkuus
  7. h = 1/big(10)^(tarkkuus10-2)
  8. function Newton(x0)
  9. f(x) = sin(big(x)) # funktio, jonka nollakohta etsitään
  10. xold = big(x0) # alkuarvo
  11. while true
  12. fold = f(xold)
  13. xnew = xold - h*fold/(f(xold+h)-fold) # x(n+1) = x(n)- f(x(n))/f'(x(n))
  14. if abs(xnew - xold) < epsilon
  15. return xnew
  16. end
  17. xold = xnew
  18. end
  19. end
  20.  
  21.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement