الگوريتم نوتن-رافسون و تصنيف به زبان سي ++

aida khanom

New Member
سلام
اين برنامه رو براي پياده كردن الگوريتم نوتن رافسون پيدا كردم ممنون ميشم در مورد عملكردش توضيح بديد.
HTML:
// Newton-Raphson method of finding roots                                     //
//   Passing references to functions f(x) and f'(x) as function parameters    //
//   also demonstrates use of a function template                             //

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

using namespace std;

//----------------------------------------------------------------------------//
// Function template: Newton-Raphson method find a root of the equation f(x)  //
//  see http://en.wikipedia.org/wiki/Newton's_method                          //
// Parameters in:  &x            reference to first approximation of root     //
//                 (&f)(x)       reference to function f(x)                   //
//                 (fdiv)(x)     reference to function f'(x)                  //
//                 max_loop      maxiumn number of itterations                //
//                 accuracy      required accuracy                            //
//            out: &x            return root found                            //
// function result: > 0 (true) if root found, 0 (false) if max_loop exceeded  //
template <class T1>
 int newton(T1 &x, T1 (&f)(T1), T1 (&fdiv)(T1),
                  int max_loop, const double accuracy)
 {
    T1 term;
    do
        {
         // calculate next term f(x) / f'(x) then subtract from current root  
         term = (f)(x) / (fdiv)(x);
         x = x - term;                                               // new root
        }
    // check if term is within required accuracy or loop limit is exceeded
    while ((abs(term / x) > accuracy) && (--max_loop));
    return max_loop;
 }

//----------------------------------------------------------------------------//
// test functions
                                               
double func_1(double x)                       // root is 1.85792
  {  return (cosh(x) + cos(x) - 3.0); }       // f(x) = cosh(x) + cos(x) - 3 = 0

double fdiv_1(double x)
  {  return (sinh(x) - sin(x));  }            // f'(x) = sinh(x) - sin(x)

double func_2(double x)                       // root is 5.0
  {  return (x*x - 25.0);  }                  // f(x) = x * x - 25 = 0   

double fdiv_2(double x)
  {  return (2.0 * x);  }                     // f'(x) = 2x      
  
complex<double> func_3(complex<double> x)     // roots 5 + or - 3
  { return  x*x - 10.0*x + 34.0;   }          // f(x) x^2 - 10x + 34                               

complex<double> fdiv_3(complex<double> x)      
  { return  2.0*x -10.0;   }                  // f'(x) 2x - 10                              

double func_4(double x)                             // three real roots 4, -3, 1
  {  return 2*x*x*x - 4*x*x - 22*x + 24 ;  }    // f(x) = 2x^3 - 4x^2 - 22x + 24   

double fdiv_4(double x)
  {  return 6*x*x - 8*x - 22;  }                       // f'(x) = 6x^2 - 8x - 22     

//----------------------------------------------------------------------------//
// Main program to test above function
int main()
{
    cout << "\nFind root of f(x) = cosh(x) + cos(x) - 3 = 0";
    double x = 1.0;                                   // initial 'guess' at root
    if (newton(x, func_1, fdiv_1, 100, 1.0e-8))
          cout << "\n   root x = " << x << ", test of f(x) = " << func_1(x);
    else  cout << "\n   failed to find root ";
 
    cout << "\n\nFind root of f(x) = x * x - 25 = 0";
    x = 1.0;                                          // initial 'guess' at root
    if ( newton(x, func_2, fdiv_2, 100, 1.0e-8))
          cout << "\n   root x = " << x << ", test of f(x) = " << func_2(x);
    else  cout << "\n   failed to find root ";

    cout << "\n\nFind root of f(x) = x^2 - 10x + 34  = 0";
    complex<double> xc = complex<double>(1.0, 1.0);   // initial 'guess' at root
    if ( newton(xc, func_3, fdiv_3, 100, 1.0e-8))
          cout << "\n   root x = " << xc << ", test of f(x) = " << func_3(xc);
    else  cout << "\n   failed to find root ";

    cout << "\n\nFind root of f(x) = x^2 - 10x + 34  = 0";
    xc = complex<double>(1.0, -1.0);                  // initial 'guess' at root
    if ( newton(xc, func_3, fdiv_3, 100, 1.0e-8))
          cout << "\n   root x = " << xc << ", test of f(x) = " << func_3(xc);
    else  cout << "\n   failed to find root ";

    cout << "\n\nFind root of f(x) = 2x^3 - 4x^2 - 22x + 24 = 0";
    x = 5.0;                                          // initial 'guess' at root
    if ( newton(x, func_4, fdiv_4, 100, 1.0e-8))
          cout << "\n   root x = " << x << ", test of f(x) = " << func_4(x);
    else  cout << "\n   failed to find root ";

    cin.get();
    return 0;
}
 

جدیدترین ارسال ها

بالا