Newton's Method

BabbleDay posted @ 2015年6月29日 22:02 in 见过的PL们 with tags python , 833 阅读

寻找函数零点:Quickly finds accurate approximations to zeros of differenciable functions.

只要问题是可微的,则问题是可解的。

Wikipedia上操作演示

https://en.wikipedia.org/wiki/Newton's_method#/media/File:NewtonIteration_Ani.gif

 

实现指南:

高阶函数为的是抽象出功能,不要被参数混乱思路

python中整数相除会返回整数,所以注意传入浮点数做参数

 

def square_root(a):
    def f(x):
        return x * x - a

    def df(x):
        return 2 * x

    return find_zero(f, df)

def find_zero(f, df):
    def near_zero(x):
        return appro_equal(f(x), 0)
    return improve(update(f, df), near_zero, guess=1.0, max_update=100)

def update(f, df):
    def u(x):
        return x - f(x)/df(x)
    return u

def improve(update, close, guess, max_update):
    k = 0
    while not close(guess) and k < max_update:
        print guess, k
        guess = update(guess)
        k += 1
    return guess  # the guess improved

def appro_equal(x, y, tolerance=1e-15):
    print abs(x-y) < tolerance
    return abs(x-y) < tolerance

 

推广至求n次根

>>> def power(x, n):
        """Return x * x * x * ... * x for x repeated n times."""
        product, k = 1, 0
        while k < n:
            product, k = product * x, k + 1
        return product
>>> def nth_root_of_a(n, a):
        def f(x):
            return power(x, n) - a
        def df(x):
            return n * power(x, n-1)
        return find_zero(f, df)

 

实际应用 Function Decorator

 

def trace(fn):
    def t(x):
        print "Calling", fn, "on argument", x
        return fn(x)
    return t

@trace
def square(x):
    return x * x
Avatar_small
PSC Result 2022 Comi 说:
2022年8月30日 21:47

Government of the People’s Republic of Bangladesh, Directorate of Primary Education (DPE), is going to announce PSC Result 2022 in student wide on 30th December 2022 for all divisional Grade 5 exam result with Ebtedayee Result 2022 for annual final terminal examinations, The Primary School Certificate Examination Result 2022 will be announced for both of General and Madhrsah students in division wise to all education board known as Prathomik Somaponi Result 2022. PSC Result 2022 Comilla Board The DPE has successfully conducted the class 5th grade PSC and Ebtedayee Examination tests from 17th to 24th November 2022 under all education boards of Dhaka, Chittagong, Comilla, Rajshahi, Sylhet, Barisal, Jessore, Dinajpur and Madrasah Board, and the DPE Grade-5 exams are successfully conducted at all 7,194 centers across the country.

Avatar_small
192.168.1.1 说:
2023年2月02日 03:02

Do you own a router or modem, then you might have heard about the term Internal configuration and router configuration which are very important to do when you first buy a new router or when you have just reset a router. 192.168.1.1 Frankly speaking, there are hundreds of router brands across the world and every one of them has high end router models that have a different configuration process.

Avatar_small
boardmodelpaper.com 说:
2024年1月11日 21:52

Board Model Papers 2024 provide all states of 6th to 10th text books 2024 Candidates who are Searching for 6th to 10th and 11th to 12th text books and syllabus, sample questions, exam pattern, and Co-Curricular Subject textbooks can refer to this entire article. boardmodelpaper.com and question papers for following the website and Arts, Science, Commerce Stream Subject Wise Solved Question Bank for Hindi & English Medium Students with Exam Pattern & Blueprint and subject Wise with 11th & 12th Question Bank 2024 for General & Vocational Course. Here, we have gathered all subjects of Board textbooks for all Class along with the direct download links.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter