割线法解非线性方程\n
割线法也用于解非线性方程。此方法类似于牛顿拉夫森法,但这里我们不需要求函数 f(x) 的导数。我们仅能使用 f(x) 通过使用牛顿差商公式在数值上求得 f’(x)。根据牛顿拉夫森公式,
我们知道,

现在,使用差商公式,我们得到,
![]()

![]()
用新 f’(x) 替换牛顿拉夫森公式中的 f’(x),我们可以找到割线公式来解非线性方程。

注意:对于此方法,我们需要任何两个初始猜测来开始求非线性方程的根。
输入和输出
Input: The function f(x) = (x*x) - (4*x) - 10 Output: The root is: -1.74166
算法
secant(x1, x2)
输入:两个根的初始猜测。
输出:非线性方程 f(x) 的近似根。
Begin f1 := f(x1) f2 := f(x2) x3 := ((f2*x1) – (f1*x2)) / (f2 – f1) while relative error of x3 and x2 are > precision, do x1 := x2 f1 := f2 x2 := x3 f2 := f(x2) x3 := ((f2*x1) – (f1*x2)) / (f2 – f1) done root := x3 return root End
示例
#include<iostream>
#include<cmath>
using namespace std;
double absolute(double value) { //to find magnitude of value
if(value < 0)
return (-value);
return value;
}
double f(double x) { //the given function x^2-4x-10
return ((x*x)-(4*x)-10);
}
double secant(double x1, double x2) {
double x3, root;
double f1, f2;
f1 = f(x1);
f2 = f(x2);
x3 = (f2*x1-f1*x2)/(f2-f1);
while(absolute((x3-x2)/x3) > 0.00001) { //test accuracy of x3
x1 = x2; //shift x values
f1 = f2;
x2 = x3;
f2 = f(x2); //find new x2
x3 = (f2*x1-f1*x2)/(f2-f1); //calculate x3
}
root = x3;
return root; //root of the equation
}
main() {
double a, b, res;
a = 0.5;
b = 0.75;
res = secant(a, b);
cout << "The root is: " << res;
}输出
The root is: -1.74166
广告
数据结构
网络技术
关系型数据库
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP