LoginSignup
11
6

More than 5 years have passed since last update.

JuliaでNewton法書いてみた(1次元)

Posted at

2018年始に急激なJulia熱を感じたので波に乗ってみました(`・ω・´)

newton.jl
function nd(f, x, a)
  return (f(x+a) - f(x))/a
end

function newton(f, x0)
  x = x0 - f(x0) / nd(f, x0, 1e-7)
  if abs(f(x)) < 1e-7
    return x
  else
    return newton(f, x)
  end
end

function main()
  f(x) = x^2 - 4.0
  print(newton(f, 1.0))
end

main()
%julia newton.jl
2.0000000000000044

https://github.com/bicycle1885/Julia-Tutorial
ここを見て勘で書いてみましたが動いてるっぽいです!

11
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
6