question archive A program which finds an approximate solution to an equation f(x) = 0 for some function f

A program which finds an approximate solution to an equation f(x) = 0 for some function f

Subject:Computer SciencePrice:2.84 Bought6

A program which finds an approximate solution to an equation f(x) = 0 for some function f. Use the bisection method. To solve the problem using this method first find two values of x, A and B, such that when evaluated in the function f(x) they give opposites signs for the-y value. If f(x) is continuous between these two values then we know that there is at least one x which evaluates to a 0-y value, which is between these two values A and B. Treat the positive value as an upper bound and the negative value as a lower bound. Divide the space between A and B in half and evaluate the function at that new point. If the value is positive than it replaces the existing upper-bound and if it is negative, it replaces the existing lower-bound. Continue dividing the space between the upper-bound and lower-bound in half and evaluating this new value and generating new upper and lower bounds as the case may be. Continue the evaluation process until the x value that you are plugging into the function evaluates to a y value that is zero plus or minus 0000001. Consider the possibility of finding all the real roots of any given function up to and including x raised to the fifth power. Input should consist of reading the coefficients one at a time to the powers of x up to 5 and some constant.

A desk check with a calculator on y = X2 -2 and identify the variables associated with that problem. Write the code that follows your algorithm. Then test your program on other polynomials such as 2x5 -15x4 + 35x3 -15x2-37x + 30 (roots are -1, 1, 2, 2.5, 3) and 3x5 -17x4 + 25x3 + 5x2 -28x + 12 (roots are -1,1, 2/3, 2, 3). Use at least 3 methods. One to read the 5 coefficients, one to calculate the value of the polynomial, and one to do the binary bisection search. Use the following for loop to work through the X values:for(double x = -5.0000001; x < 5.0000001; x = x + .1) 

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

The following is the response to asked question 

Step-by-step explanation

class Calc {

   static final byte N = 7; 
   static final byte M = 5;  
   static final double X[] = {-3, -2, -1,  0, 1, 2, 3}; 
   static final double Y[] = { 5, -2, -3, -1, 1, 4, 5};


   double s[]   = new double[2 * M + 1];
   double t[]   = new double[M + 1];
   double a[][] = new double[M + 1][M + 2];

  
   Calc() {
   
       for (int i = 0; i <= 2 * M; i++)
           s[i] = 0;
      
       for (int i = 0; i <= M; i++)
           t[i] = 0;
   }

  
   void calcLeastSquaresMethod() {
       try {
        
           calcST();

      
           insST();

     
           sweepOut();
       } catch(Exception e) {
           e.printStackTrace();
       }
   }


   private void calcST() {
       for (int i = 0; i < N; i++) {
           for (int j = 0; j <= 2 * M; j++)
               s[j] += Math.pow(X[i], j);
           for (int j = 0; j <= M; j++)
               t[j] += Math.pow(X[i], j) * Y[i];
       }
   }


   private void insST() {
       for (int i = 0; i <= M; i++) {
           for (int j = 0; j <= M; j++)
               a[i][j] = s[i + j];
           a[i][M + 1] = t[i];
       }
   }


   private void sweepOut() {
       for (int k = 0; k <= M; k++) {
           double p = a[k][k];
           for (int j = k; j <= M + 1; j++)
               a[k][j] /= p;
           for (int i = 0; i <= M; i++) {
               if (i != k) {
                   double d = a[i][k];
                   for (int j = k; j <= M + 1; j++)
                       a[i][j] -= d * a[k][j];
               }
           }
       }
   }

 
   void display() {
       try {
           for (int k = 0; k <= M; k++)
               System.out.printf("a%d = %10.6f\n", k, a[k][M + 1]);
           System.out.println("    x    y");
           for (double px = -3; px <= 3; px += .5) {
               double p = 0;
               for (int k = 0; k <= M; k++)
                   p += a[k][M + 1] * Math.pow(px, k);
               System.out.printf("%5.1f%5.1f\n", px, p);
           }
       } catch(Exception e) {
           e.printStackTrace();
       }
   }
}


class LeastSquaresMethod {
   public static void main (String[] args) {
       Calc obj = new Calc();

       try {
         
           obj.calcLeastSquaresMethod();

         
           obj.display();
       } catch(Exception e) {
           e.printStackTrace();
       }
   }
}