Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN C++ 1) Newton-Raphson The problem of computing the square root of a number is

ID: 3683586 • Letter: I

Question

IN C++

1) Newton-Raphson The problem of computing the square root of a number is a specific case of the more general root-finding problem. Root-finding occurs in many places in electrical engineering, as well as in other science and engineering problems. Recall that the roots (or zeros) of any function are the parametric value(s) for which the function produces a zero result: x:(x)-0 Finding the square root of some number K is equivalent to finding the principal zero of the following equation. f(x)= -K We could just as easily compute the cube root of K in the same fashion by solving the equation below Generalizing, we can find the nth root of any value K by solving The Newton-Raphson algorithm is an efficient and ingenious method for finding the principal root of any continuously differentiable function. The method is ascribed to Sir Isaac Newton, although Joseph Raphson was the first to publish it in a more refined form. The method is a generalized version of the Babylonian "guess and check" approach that you explored in a previous lab The method generalizes the Babylonian approach by employing the derivative of the function. Given an old guess, x, the new ("better") guess, is computed by: Ti Note that the Babylonian Algorithm is simply the specific case of Newton-Raphson for f(x)- - K For the generalized nth root problem, f(x) is: and the derivative, f'(x) is: Substituting these formulae, the Newton-Raphson update rule for computing the nth root of any value Kis n.T TL

Explanation / Answer

#include <iostream>
#include <cmath>

using namespace std;

double rootN(double num, int root)
{
double x = 0.0;
double xn = 0.0;
double val;

int repeat = 0;
int i;

for (i = 0; i <= num; i++)
{
val = i*i-num;
if (val > 0.0)
{
xn = (i+(i-1))/2.0;
break;
}
}     

   while (!(repeat++ >= 100 || x == xn))
   {
       x = xn;
   xn = x - (pow(x, (double)root) - num) / (root * pow(x, (double)root-1));
   }

   return xn;
}

     
int main()
{
   char ch='y';

   while(ch=='y'){

       double num;
   int root;

   string root_name;

   cout << "Enter a number: ";
   cin >> num;

   cout << "Enter the number of the root you want to find for this number: ";
   cin >> root;

   if (root <= 1)
   {
   cout << "The root must be greater than 1. ";
   return 0;
   }

   if (root == 2) root_name = "nd";

   if (root == 3) root_name = "rd";

   if (root > 3) root_name = "th";

   cout << "The " << root << root_name << " root of " << num << " is " << rootN(num, root) << ". ";

   cout << "continue? y/n: ";
   cin >> ch;
   }

   return 0;
}