Experiments-7

Write a program to find the roots of an

equation using Newton Raphson Method.

Newton Raphson Method (C++)



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

using namespace std;

double f(double x) //for the function x^4 - 10
{
       return (x*x*x*x - x - 10);
}

double ff(double x) //Differentiating function f(x)
{
    return (4*x*x*x - 1);
}

void NR(double aerr, int maxitr, double a)
{
     int i = 0;
     double b, prev;
     while (i <= maxitr)
     {
           b = a - (f(a))/ff(a);
           cout << "Iter " << i << ": x = " << b << endl;
           i++;
           if (fabs(b - a) < aerr)
              break;
           a = b;
     }
   
}

int main()
{
    double aerr, a, b;
    int maxitr;
    cout << "Enter aerr, maxitr, a." << endl;
    cin >> aerr;
    cin >> maxitr;
    cin >> a;
    NR(aerr, maxitr, a);
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.