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

write c++ program for finding roots Newton\'s Method or finding roots of equatio

ID: 3808014 • Letter: W

Question

write c++ program for finding roots Newton's Method or finding roots of equations is a standard approximation way of doing so, although it needs a derivative of the original equation. Assuming your equation is "well behaved", this method quickly converges to the correct root. The term "well behaved" means the slopes of the curve are not too steep, not are there any dis-continuous parts in the equation. The equation y = 3_Squareroot x is not "well behaved" (see notes from class). Newton's Method equation is: x_n + 1 = x_n - f (x_n)/f' (x_n) until | f (x_n + 1) |

Explanation / Answer

C++ code for first function

#include<iostream>
#include <math.h>
using namespace std;
double mod(double y) // function to find the absolute value of a number
{
    if(y<0) return (-1*y);
    return y;
}
int main()
{
   float C[6],x,y,dy,tol,error;// declearing floating point variables
   int i,iter =0,Max_iter;// declering integer variable
   cout << "Enter the Tolerence: ";// prompting the user to enter the tolerence
   cin >> tol; // reading the value of tolerence from user
   cout << "Enter the Maximum iterations: ";// prompting the user to enter the maimun iterations allowed
   cin >> Max_iter;// reading the value from the user
   x = 0.5; // initial guess
   y = acos(x)-cos(x); // evaluating the function
   dy = sin(x)-1/(sqrt(1-x*x));// evaluating dy for initial guess
   while( mod(y)>tol && iter<=Max_iter && dy!=0) //loop for further rectification of root
   {
       x = x - y/dy;// next x value
       y = acos(x)-cos(x);// evaluating y for new x
       dy = sin(x)-1/(sqrt(1-x*x));// evaluating dy for new x
      iter++; // increase iteration
   }
   if(dy == 0)cout<< "Division by zeor" << endl;// division by zero occured
   if(iter>Max_iter) cout<< "Method faild to converge" << endl;// method diverges
   if(mod(y)<=tol) cout<< "Root = " << x << " Iterations used = "<< iter << endl;// method converges
}


Testing the code

$ ./a.out
Enter the Tolerence: 0.00001
Enter the Maximum iterations: 100
Root = 0.739085   Iterations used = 3

$ ./a.out
Enter the Tolerence: 0.00000001
Enter the Maximum iterations: 200
Root = 0.739085   Iterations used = 3

C++ code for second function

#include<iostream>
#include <math.h>
using namespace std;
double mod(double y) // function to find the absolute value of a number
{
    if(y<0) return (-1*y);
    return y;
}
int main()
{
   float C[6],x,y,dy,tol,error;// declearing floating point variables
   int i,iter =0,Max_iter;// declering integer variable
   cout << "Enter the Tolerence: ";// prompting the user to enter the tolerence
   cin >> tol; // reading the value of tolerence from user
   cout << "Enter the Maximum iterations: ";// prompting the user to enter the maimun iterations allowed
   cin >> Max_iter;// reading the value from the user
   x = 0.5; // initial guess
   y = acos(x)-1/cos(x); // evaluating the function
   dy = -2*sin(x)/(cos(2*x)+1)-1/(sqrt(1-x*x));// evaluating dy for initial guess
   while( mod(y)>tol && iter<=Max_iter && dy!=0) //loop for further rectification of root
   {
       x = x - y/dy;// next x value
       y = acos(x)-1/cos(x);// evaluating y for new x
       dy = -2*sin(x)/(cos(2*x)+1)-1/(sqrt(1-x*x));// evaluating dy for new x
      iter++; // increase iteration
   }
   if(dy == 0)cout<< "Division by zeor" << endl;// division by zero occured
   if(iter>Max_iter) cout<< "Method faild to converge" << endl;// method diverges
   if(mod(y)<=tol) cout<< "Root = " << x << " Iterations used = "<< iter << endl;// method converges
}

Testing the code

$ ./a.out
Enter the Tolerence: 0.00001
Enter the Maximum iterations: 100
Root = 0.44605   Iterations used = 2


$ ./a.out
Enter the Tolerence: 0.00000001
Enter the Maximum iterations: 200
Method faild to converge

C++ code for Third function

#include<iostream>
#include <math.h>
using namespace std;
double mod(double y) // function to find the absolute value of a number
{
    if(y<0) return (-1*y);
    return y;
}
int main()
{
   float C[6],x,y,dy,tol,error;// declearing floating point variables
   int i,iter =0,Max_iter;// declering integer variable
   cout << "Enter the Tolerence: ";// prompting the user to enter the tolerence
   cin >> tol; // reading the value of tolerence from user
   cout << "Enter the Maximum iterations: ";// prompting the user to enter the maimun iterations allowed
   cin >> Max_iter;// reading the value from the user
   x = 0.5; // initial guess
   y = asin(x)-1/sin(x); // evaluating the function
   dy = -2*cos(x)/(cos(2*x)-1)+1/(sqrt(1-x*x));// evaluating dy for initial guess
   while( mod(y)>tol && iter<=Max_iter && dy!=0) //loop for further rectification of root
   {
       x = x - y/dy;// next x value
       y = asin(x)-1/sin(x);// evaluating y for new x
       dy = -2*cos(x)/(cos(2*x)-1)+1/(sqrt(1-x*x));// evaluating dy for new x
      iter++; // increase iteration
   }
   if(dy == 0)cout<< "Division by zeor" << endl;// division by zero occured
   if(iter>Max_iter) cout<< "Method faild to converge" << endl;// method diverges
   if(mod(y)<=tol) cout<< "Root = " << x << " Iterations used = "<< iter << endl;// method converges
}

Testing the code

$ ./a.out
Enter the Tolerence: 0.00001
Enter the Maximum iterations: 100
Root = 0.94404   Iterations used = 4


$ ./a.out
Enter the Tolerence: 0.00000001
Enter the Maximum iterations: 200
Method faild to converge