Files
sjy01-image-proc/vendor/github.com/nuknal/goNum/NewtonIterate.go
2024-10-24 15:46:01 +08:00

83 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// NewtonIterate
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-01
版本 : 0.0.0
------------------------------------------------------
牛顿迭代求解非线性方程 f(x)=0 在区间[a, b]内的根
理论:
(局部收敛定律)
1. f(x)在区间[a, b]具有二阶连续导数;
2. 当xE[a, b]f'(x) != 0
(非局部收敛定律)
1. 当xE[a, b]f'(x)、f''(x)连续且不变号
2. 选取初值x0E[a, b]使f(x0)*f''(x0) > 0
平方收敛
------------------------------------------------------
输入 :
fn f(x)函数定义为等式左侧部分右侧为0
fn1 f'(x)函数
a, b 求解区间
c 求解初值
N 步数上限
tol 误差上限
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import "math"
// NewtonIterate 牛顿迭代求解非线性方程 f(x)=0 在区间[a, b]内的根
func NewtonIterate(fn, fn1 func(float64) float64, a, b, c float64, N int, tol float64) (float64, bool) {
/*
牛顿迭代求解非线性方程 f(x)=0 在区间[a, b]内的根
输入 :
fn f(x)函数定义为等式左侧部分右侧为0
fn1 f'(x)函数
a, b 求解区间
c 求解初值
N 步数上限
tol 误差上限
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
var sol float64
var err bool = false
// 判断端点和初值是否为所求之解
switch {
case math.Abs(fn(a)) < tol:
sol = a
err = true
return sol, err
case math.Abs(fn(b)) < tol:
sol = b
err = true
return sol, err
case math.Abs(fn(c)) < tol:
sol = c
err = true
return sol, err
}
//求解
sol = c - fn(c)/fn1(c)
for i := 0; i < N; i++ {
if math.Abs(sol-c) < tol {
err = true
return sol, err
}
c = sol
sol = c - fn(c)/fn1(c)
}
return sol, err
}