Vcoderz Community

Vcoderz Community (http://forum.vcoderz.com/index.php)
-   Computers & Information Technologies (http://forum.vcoderz.com/forumdisplay.php?f=6)
-   -   Secant Method using C++ (http://forum.vcoderz.com/showthread.php?t=17289)

J()e 12-14-2009 07:00 PM

Secant Method using C++
 
Hello guyz,I urgently need your help, I have an assignment and it's due date is today at 12:00 PM.

Long story short,the doctor explained a few lectures and I didn't attend class for several reasons(illness and doctor examinations) and he explained the file manipulator thingy and I have no idea what it talks about so please save my a$$ today,this assignment has a grade of 25% of the total grade so Once again your help is needed.
================================================== ========
“The Secant Method”

In numerical analysis, the secant method is a root-finding algorithm that uses a succession of roots of secant lines to better approximate a root of a function . The secant method does not need a formula for the derivative and it can be coded so that only one new function evaluation is required per iteration.


Assignment


Again I really appreciate your help and thanks in advance:)

Google 12-15-2009 10:52 PM

Code:

/* This program uses the secant method to compute a root of the
/* provided function.  The majority of this program deals with parsing
/* input and formatting output -- after all, someone has to look at
/* the output, so it may as well be formatted nicely. */

// Secant Method Program
// Homework Assignment #1
// Author: Prof. Gray
// Language: C++
// Usage: secant < x0> < x1>

#include < iostream.h>
#include < iomanip.h>
#include < math.h>

// --- PROTOTYPES ---
double f(double);
void output_header();
void output_status(int, double, double, double);
void close_table(double, double);
// ------------------

main (int argc, char *argv[]) {
  double xn,xnp1,tempx;
  double fxn,fxnp1,df;
  double xtol = 1.0e-8;
  double ftol = 1.0e-10;
  double error;

  int iteration;
 
  if (argc != 3) // arguments were not given on the command line:
    cout << "Usage: secant <x0> <x1>" << endl;
  else {
    xn = atof(argv[1]);
    xnp1 = atof(argv[2]);
 
    cout << "Using " << xn << " and "
    << xnp1  <<" as initial values." << endl << endl;
    cout << "Using x difference stopping criterion: " << xtol << endl
    << endl ;
  }

  fxn  = f(xn);
  fxnp1 = f(xnp1);

  // at this point, everything is initialized.
  // output header.
  output_header();
  iteration = 0;


  /* The remaining portion of the program computes the actual
  /* secant iterations.  Note that computation terminates when
  /* |x_n-x_(n-1)| < xtol. */

  while ((error = fabs(xn - xnp1)) > xtol ) {

    tempx = xnp1;
    df = fxn-fxnp1;

    // there is danger of division by zero.  Try to head this
    // possibitity off by checking prior to division.
    if (fabs(df) < ftol) {
      cout << "Error -- division by zero possible. " << endl
      << "Terminating program.";
      return -1;
    }


    xnp1 = xn - fxn*(xn-xnp1)/df;
    xn = tempx;
    fxn = fxnp1;
    fxnp1 = f(xnp1);
    output_status(++iteration,xn,fxn,error);
   
  }
  close_table(xnp1,fxnp1); // END OF COMPUTATIONS
}


/* Definition of the function whose roots are sought is provided
/* within the function f */

double f(double val) {
  return pow(val,6) - val - 1;
}


/* output_header prints the header line for the results */
void output_header() {
  cout << "iter    x_n          f(x_n)    "
      <<  "  abs(x_n-x_(n-1))" << endl;
  cout << "---------------------------"
      << "---------------------------" << endl;
}

/* output_status is responsible for displaying the current state of
/* the secant method in a user-readable format.  Providing a
/* formated display of the current state allows for easier visual
/* analysis of the convergence of the method. */

void output_status(int iteration,
          double xn,
          double fxn,
          double error) {

    cout.setf(ios::fixed,ios::floatfield);
    cout << setw(3) << iteration ;

    cout.unsetf(ios::fixed); cout.unsetf(ios::floatfield);
    cout.setf(ios::fixed,ios::floatfield);
    cout << "  " << setprecision(8) << xn;

    cout.unsetf(ios::fixed); cout.unsetf(ios::floatfield);
    cout.setf(ios::scientific);
    cout << "  " << setprecision(8) << fxn;
    cout << "    " << setprecision(10) << error << endl;

}

/* close_table closes the table form */

void close_table(double c, double fc) {
  cout.unsetf(ios::fixed); cout.unsetf(ios::floatfield);
  cout.setf(ios::scientific);
  cout << "---------------------------"
      << "---------------------------" << endl << endl;

  cout << "Secant method: " << endl << '\t'
      <<"Root: " << setprecision(10) << c << endl << '\t'
      <<"Function value: "  << setprecision(12) << fc << endl<< endl;
}



All times are GMT +1. The time now is 01:19 PM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger