试位法求解非线性方程
2013-08-07 21:43
218 查看
//用试位法求解非线性方程
#include <iostream>
#include <math.h>
#include <process.h>
using namespace std;
class falsi
{
private:
int iteration;
double eps, error, f, f_low, f_high, f_new, x_low, x_high, x_new;
double d, fric, Q, s, W;
public:
falsi()
{
iteration = 0;
fric = 0.01;
Q = 10;
s = 1e-3;
W = 10;
}
void solution();
double function(double d)
{
double term1 = d * pow((W * d / (W + 2 * d)), (2.0 / 3.0));
double term2 = fric * Q / (W * sqrt(s));
f = term1 - term2;
return f;
}
};
void main()
{
falsi regula;
regula.solution();
}
void falsi::solution()
{
cout << "\n输入下限:";
cin >> x_low;
cout << "\n输入上限:";
cin >> x_high;
f_low = function(x_low);
f_high = function(x_high);
if ((f_low * f_high) > 0)
{
cout << "\n错误的归类。" << endl;
exit(0);
}
cout << "\n输入公差:";
cin >> eps;
do
{
iteration++;
x_new = (x_low * f_high - x_high * f_low) / (f_high - f_low);
f_new = function(x_new);
error = fabs(f_new);
if ((f_new * f_low) < 0)
{
x_high = x_new;
}
else
{
x_low = x_new;
f_low = f_new;
}
}while (error >= eps);
cout << "\n解是:" << x_new << endl;
cout << "\n收敛于" << iteration << "次迭代" << endl;
}
#include <iostream>
#include <math.h>
#include <process.h>
using namespace std;
class falsi
{
private:
int iteration;
double eps, error, f, f_low, f_high, f_new, x_low, x_high, x_new;
double d, fric, Q, s, W;
public:
falsi()
{
iteration = 0;
fric = 0.01;
Q = 10;
s = 1e-3;
W = 10;
}
void solution();
double function(double d)
{
double term1 = d * pow((W * d / (W + 2 * d)), (2.0 / 3.0));
double term2 = fric * Q / (W * sqrt(s));
f = term1 - term2;
return f;
}
};
void main()
{
falsi regula;
regula.solution();
}
void falsi::solution()
{
cout << "\n输入下限:";
cin >> x_low;
cout << "\n输入上限:";
cin >> x_high;
f_low = function(x_low);
f_high = function(x_high);
if ((f_low * f_high) > 0)
{
cout << "\n错误的归类。" << endl;
exit(0);
}
cout << "\n输入公差:";
cin >> eps;
do
{
iteration++;
x_new = (x_low * f_high - x_high * f_low) / (f_high - f_low);
f_new = function(x_new);
error = fabs(f_new);
if ((f_new * f_low) < 0)
{
x_high = x_new;
}
else
{
x_low = x_new;
f_low = f_new;
}
}while (error >= eps);
cout << "\n解是:" << x_new << endl;
cout << "\n收敛于" << iteration << "次迭代" << endl;
}
相关文章推荐
- 微软公司内部培训程序员资料---求解非线性方程组的类
- Matlab非线性方程求解
- Newton-Raphson 法求解非线性方程组
- 割线法求解非线性方程
- 逐次代入法求解非线性方程
- 优化与求解非线性方程组(单变量问题)
- Python求解非线性方程
- python/scipy求解非线性方程(fsolve/root)
- fsolve函数求解非线性方程
- 数值计算——求解非线性方程组
- 使用matlab进行非线性方程组求解
- 非线性方程组求解算法的C++实现
- python用fsolve、leastsq对非线性方程组进行求解
- R语言非线性方程组求解dfsane,nleqslv
- 用R语言求解非线性方程
- 非线性方程二分法求解
- 对分法(二分法)求解非线性方程-M语言
- matlab实现牛顿迭代法求解非线性方程组
- 求解多元非线性方程组的解(功分器dB值与功分比转化)
- 牛顿下山法求解非线性方程(组)(C实现)