方程求根方法(适用于超越方程)

主要还是迭代缩小误差

有牛顿法、弦截法、二分法......

#include<iostream>
using namespace std;
#include<cmath>
float f1(float x) {
return x * exp(x) - 1;
}

float bisolve(float low, float high, float e) {//e是精度限定

while (1) {
float mid = low / 2 + high / 2;
if (f1(mid) * f1(low) <= 0) {

if (high - low <= e) {
return mid;
}
high = mid;
}
else {
if (high - low <= e) {
return mid;
}
low = mid;
}
}

}

float newtonsolve(float x, int n) {
for (int j = 0; j < n; j++) {
x = x - (x - 1 / exp(x)) / (1.000 + x);
}
return x;
}

float quotient(float x1, float x2) {
return(f1(x1) - f1(x2)) / (x1 - x2);
}
float secant(float X_k1, float X_k,int n) {//弦截法
for (int j = 0; j < n; j++) {
float X_k2;
X_k2 = X_k - (1 / quotient(X_k, X_k1)) * f1(X_k);
X_k1 = X_k;
X_k= X_k2;
}
return X_k;
}

猜你喜欢

转载自www.cnblogs.com/geniusJinming/p/12802919.html