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

Write your own squareroot function named double my_ squareroot _1(double n) usin

ID: 3847777 • Letter: W

Question

Write your own squareroot function named double my_ squareroot _1(double n) using the following pseudocode: x = 1 repeat 10 times: x = (x + n/x)/2 return x and then write a main which prints n, squareroot (n), and my_ squareroot _1(n) for n = 3.14159 times 10 to the k^th power for k = -100, -10, -1, 0, 1, 10, and 100. Use this C++11 code (which may not work on older versions of Visual Studio): for(auto k: {-100, -10, -1, 0, 1, 10, 100}){n = 3.14159 * pow(10.0, k);//cout goes here Modify problem 1 to 1also print the relative error as a per cent, by adding a column relative error per cent = 100 *((my_ squareroot _1(n) - squareroot (n))/ squareroot (n). Line up the columns using setw(), etc. Name your problem hw2pr2.cpp. Modify problem 2 to scale n so the squareroot will possibly be more accurate, as follows: Name a function my_ squareroot _2(n), which does this: Set result to 1 If n is greater than 8/5, divide n by 4 and multiply result by 2;repeat until n is not greater than 8/5 If n is less than 2/5, multiply n by 4 and divide result by 2;repeat until n is not less than 2/5 Return result times my_ squareroot _1(n). The main should print n, squareroot (n), my_ squareroot _2(n), and the relative error of my_ squareroot _2(n) as a per cent. Name your program hw2pr4.cpp.

Explanation / Answer

hw2pr1.cpp

#include "std_lib_facilities_3.h"

//Square root function based on the Taylor Series centered at x=0
double my_sqrt_1(double n)
{
   double x = n-1;
   x = 1 + x/2 - (1.0/8.0)*pow(x,2) + (1.0/16.0)*pow(x,3) - (5.0/128.0)*pow(x,4);  

   return x;
}

int main()
{
   double n;
  
   for(auto k : {-100, -10, -1, 0, 1, 10, 100}){
       n = 3.14159 * pow(10.0, k);
       cout << "n: " << n << " sqrt(n): " << sqrt(n) << " my_sqrt_1(n): " << my_sqrt_1(n) << " ";
   }
   return 0;
}

hw2pr2.cpp


#include "../bin/std_lib_facilities_3.h"

//Square root function based on the Taylor Series centered at x=0
double my_sqrt_1(double n)
{
   double x = n-1;
   x = 1 + x/2 - (1.0/8.0)*pow(x,2) + (1.0/16.0)*pow(x,3) - (5.0/128.0)*pow(x,4);

   return x;
}

int main()
{
   double n;
   double relative_error;
   cout << setw(16) << "n" << setw(16) << "sqrt(n)" << setw(16) << "my_sqrt_1(n)" << setw(16) << "error ";

   for(auto k : {-100, -10, -1, 0, 1, 10, 100}){
       n = 3.14159 * pow(10.0, k);
       relative_error = ((my_sqrt_1(n) - sqrt(n))/(sqrt(n)))*100;
       cout << setw(16) << n << setw(16) << sqrt(n) << setw(16) << my_sqrt_1(n) << setw(16) << relative_error << " ";
   }

   return 0;
}

hw2pr4.cpp

#include "std_lib_facilities_3.h"

//Square root function based on the Taylor Series centered at x=0
double my_sqrt_1(double n)
{
   double x = n-1;
   x = 1+ x/2 - (1.0/8.0)*pow(x,2) + (1.0/16.0)*pow(x,3) - (5.0/128.0)*pow(x,4);  

   return x;
}

//Creates a scalar multiple that will bring the my_sqrt_1 value much closer to the actual value
double my_sqrt_2(double n)
{
   //If a number is greater than 8/5, will repeatedly do the function until it is less than 8/5
   double result = 1;
   if(n > 8.0/5.0) {
       while(true) {
           n /= 4.0;
           result *= 2.0;
           if(n <= 8.0/5.0) {
               break;
           }
       }
      
   }
   //If a number is less than 2/5, will repeatedly do the function until it is greater than 2/5
   if(n < 2.0/5.0) {
       while(true){
           n *= 4.0;
           result /= 2.0;
           if(n >= 2.0/5.0) {
               break;
           }
       }
   }
   else {
   }
   return result * my_sqrt_1(n);
  
}

int main()
{
   double n;
   double relative_error;
   cout << setw(16) << "n" << setw(16) << "sqrt(n)" << setw(16) << "my_sqrt_2(n)" << setw(16) << "error ";
  
   for(auto k : {-100, -10, -1, 0, 1, 10, 100}){
       n = 3.14159 * pow(10.0, k);
       relative_error = ((my_sqrt_2(n) - sqrt(n))/(sqrt(n)))*100;
       cout << setw(16) << n << setw(16) << sqrt(n) << setw(16) << my_sqrt_2(n) << setw(16) << relative_error << " ";
   }

   return 0;
}