python,步进法预测函数最小值

# -*- coding: utf-8 -*-
"""
Created on Fri May 29 09:50:15 2020

@author: guangjie2333
"""
def fun(x):
    return (x+5)*(x+5)
    


def local_min(x0,setp,precision,max_iters):
    x = x0
    y0 = fun(x)
    for i in range(max_iters): 
        y1 = fun(x-setp)
        print("第",i,"次的结果",abs(y1-y0),"       x = ", x)
        
        if abs(y1-y0) < precision :
            print('在规定的次数中结束')
            break
        x = x - setp
        y0 = y1
        
if __name__ == '__main__':  
    
    local_min(3,0.01,0.0001,10000)

你可能感兴趣的:(python课堂实验)