0

我之前遇到过这个问题已经解决了,但我觉得由于我的新问题的性质与成功编译无关,而是与代码的实际逻辑有关,因此可以接受创建一个新主题。到目前为止,这是我的代码:

#include "assign4.h"
#include <iostream>

using namespace std;

int main(int argc, char * argv[]){
   solution s;
   double root;

   cout << "Enter interval endpoints: ";
   cin >> s.xLeft >> s.xRight;

   cout << "Enter tolerance: ";
   cin >> s.epsilon;

   root = s.bisect (s.xLeft, s.xRight, s.epsilon, &solution::f, s.error);

   if (!(s.error))
      cout << "Root found at " << root << "\nValue of f(x) at root is: " << s.f(root) << "\n";
   else {
      cout << "The solution of a quadratic equation with coefficients: " << endl;
//      cout << "a = " << a << ", b = " << b << ", c = " << c << endl;
      cout << "has not been found." << endl;
   }
   return 0;
}

///////////////////////////////////////// ///////////////////////////////////////// /////////////////////////////////////

#include "assign4.h"
#include <iostream>
#include <math.h>

using namespace std;

 double solution::bisect (double xLeft, double xRight, double epsilon, double (solution::*f)(double), bool& error) {
   double xMid;
   double fLeft, fRight;
   double fMid;

   fLeft = (this->*f)(xLeft);
   fRight = (this->*f)(xRight);

   error = (fLeft * fRight) < 0;
   if (error)
       return -999.0;

   for (double i = 0; i < 20; i++) {
      xMid = (xLeft + (xLeft + 1.0)) / 2.0;
      fMid = (this->*f)(xMid);

      if (fLeft * fMid > 0.0) {
         xLeft = xMid + 0.5;
         xRight = xLeft + 1.0;
         fLeft = fMid;
      }
      else if (fLeft * fMid < 0.0){
         xRight = xMid;
         fRight = fMid;
      }
      else {
         return xMid;
      }

      cout << "New Interval is [" << xLeft << ", " << xRight << "]" << endl;
   }

   return (xLeft + xRight) / 2.0;
}

double solution::f (double x) {
   return ((1 * pow(x,2.0)) + (5 * x) + 2);
}

///////////////////////////////////////// ///////////////////////////////////////// /////////////////////////////////////

#ifndef ASSIGN4_H
#define ASSIGN4_H

class solution {

public:
   double xLeft, xRight;
   double epsilon;
   bool error;

   double bisect(double, double, double, double (solution::*f)(double), bool&);
   double f(double);
};
#endif // ASSIGN4_H

///////////////////////////////////////// ///////////////////////////////////////// /////////////////////////////////////

我完成这项任务的目标是找到存在的任何根源。我的问题是,我发现的每个二等分示例都只讨论如何一次找到一个根。我要使用的区间是 [-10.0, 10.0] ,最终我将接收通过封装在结构中的数组传递的方程的系数,但现在我已经对系数进行了硬编码。

所以我的问题是我目前可以得到我硬编码的方程的第一个根的 0.2 (x^2 + 5x + 2) 但我不确定如何越过那个根并继续寻找另一个根直到我的间隔结束。我也不确定如何准确地击中根源而不是稍微偏离。

为文字墙道歉,任何帮助表示赞赏!:)

4

1 回答 1

0

您可以输入间隔,使其包括两个根。然后调用bisect两次——一次用于较低的根,一次用于较高的根。

 int main(int argc, char * argv[]){
    solution s;
    double root;

    cout << "Enter interval endpoints: ";
    cin >> s.xLeft >> s.xRight;

    cout << "Enter tolerance: ";
    cin >> s.epsilon;

    double xMid = 0.5*(s.xLeft + s.xRight);
    root = s.bisect (s.xLeft, xMid, s.epsilon, &solution::f, s.error);
    if (!(s.error))
       cout << "Root found at " << root << "\nValue of f(x) at root is: " << s.f(root) << endl;
    else
    {
       cout << "The solution of a quadratic equation with coefficients: " << endl;
       cout << "has not been found." << endl;
    }

    root = s.bisect (xMid, s.xRight, s.epsilon, &solution::f, s.error);
    if (!(s.error))
       cout << "Root found at " << root << "\nValue of f(x) at root is: " << s.f(root) << endl;
    else
    {
       cout << "The solution of a quadratic equation with coefficients: " << endl;
       cout << "has not been found." << endl;
    }


    return 0;
 }
于 2014-04-16T19:37:17.480 回答