Vcoderz Community
We create websites that have it all, beauty & brains
Lebanon Web Design & Development - Coddict
 

Go Back   Vcoderz Community > Computer Zone > Computers & Information Technologies

Notices

Computers & Information Technologies « Everything related to computers and internet. »

Reply
 
Share Thread Tools Search this Thread
Old 12-14-2009   #1
J()e
Registered Member
 
J()e's Avatar
 
Last Online: 06-13-2011
Join Date: Jan 2007
Posts: 3,530
Thanks: 1,762
Thanked 1,429 Times in 897 Posts
Groans: 0
Groaned at 0 Times in 0 Posts
Default 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


__________________
[
J()e is offline   Reply With Quote
Old 12-15-2009   #2
Google

 
Google's Avatar
 
Last Online: 05-30-2013
Join Date: Jan 2008
Posts: 1,788
Thanks: 10,018
Thanked 1,100 Times in 651 Posts
Groans: 1
Groaned at 6 Times in 6 Posts
Default

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;
}
__________________

Google is offline   Reply With Quote
The Following User Says Thank You to Google For This Useful Post:
J()e (12-16-2009)
Reply

  Vcoderz Community > Computer Zone > Computers & Information Technologies

Tags
method, secant



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:54 AM.


Lebanon web design and development
Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Share