Skip to content

Instantly share code, notes, and snippets.

@tanishiking
Created July 12, 2015 17:00
Show Gist options
  • Save tanishiking/dc81d3e87594e5bbb6d2 to your computer and use it in GitHub Desktop.
Save tanishiking/dc81d3e87594e5bbb6d2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
def f(x):
return x**3 + 2*x**2 + 10*x - 20
def df(x):
return 3*x**2 + 4*x + 10
def newton(init, tolerance=0.000001):
x = init
cnt = 0
process = [(cnt, x)]
while True:
cnt += 1
x1 = x - f(x) / df(x)
process.append((cnt, x1))
if (abs(x1 - x)) < tolerance:
break
else:
x = x1
return process
def main():
print(newton(1.0))
if __name__ == '__main__':
main()
@tanishiking
Copy link
Author

大学の課題用スクリプト

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment