Created
July 12, 2015 17:00
-
-
Save tanishiking/dc81d3e87594e5bbb6d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
大学の課題用スクリプト