正割法求解非線性方程\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