试位法求解非线性方程

//用试位法求解非线性方程
#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值