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