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

Can you please help me with this problem by using C++ A . Use your my_cbrt_2 fun

ID: 3797716 • Letter: C

Question

Can you please help me with this problem by using C++

A. Use your my_cbrt_2 function from hw2pr4 to define a more accurate cube root for all values of n using this pseudocode: double my_cbrt_3(double n)
   If n is zero return zero.
   If n is negative return -my_cbrt_3(-n).
   Otherwise,
       set x to my_cbrt_2(n)
       do this 10 times: x = (2/3)x + n/(3*x*x)
       return x

Write a main which repeatedly reads a number n and prints cbrt(n), my_cbrt_3(n), and the relative error of my_cubrt_3(n). (The relative error should be quite small.)

B. In problem A the loop doesn't need to be executed 10 times for maximum accuracy. Write a program named hw3pr4.cpp to determine the smallest number k such that looping k times is just as accurate as looping 10 times.Hint: For k = 1 to 10, find the maximum absolute relative error for n from 1.05 to 1.55 in steps of 1/1024. Use those 10 numbers to determine the smallest number k such that looping k times is just as accurate as looping 10 times.

The needed code is :

#include "std_lib_facilities_4.h"

#include

#include

#define _USE_MATH_DEFINES

class wrong_values {}; // creating a class for unwanted values that might cause the program to get us wrong values

double my_cbrt_1(double n)

{

double x = 0,num=0,deno=0;//initializing the needed variables to use it in the equation which was givien in hw

num=(101.639*n*n)+(207.853*n)+(29.7541);

deno=(-1*n*n*n)+(42.945*n*n)+(215.165*n)+(82.1357);

x=num/deno;

return x;

}

int main()

try{

   cout <<" N CUBE CBRT ";

for(auto k : {-100,-10,-1,0,1,10,100})

{

  

double n=M_PI * pow(10.0,k);

double check= my_cbrt_1( n);

if (n>0&& check<0) throw wrong_values{}; // in case the outpur of the function turnedout to be negative it should be thrown into wrong values and then displaying the error message.

  

cout << n <<" "<< cbrt(n)<<" " << my_cbrt_1(n)<<" ";

}

return 0;

}

catch (wrong_values){ // here is catch to catch any errors while running.

  

cerr<<"Error, Both the input and the outputfor cube root have oppisite sign "<

return -2;

  

}

catch (...){ // another catch to catch whatever it in counters and not in the wrong list. this step is to protect the program

  

  

cerr<<"Unknown exception ! "<

return -1;

}

Explanation / Answer

Here is the code for you:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double my_cbrt_1(double n)
{
return (((101.639*pow(n, 2)) + (207.853*n) + 29.7541) / (-pow(n, 3) + (42.945*pow(n, 2) + (215.1) * 65 * n) + 82.1357));
}
double my_cbrt_2(double n)
{
double result = 1;
if(n > 1.55)
{
while(n <= 1.55)
{
result *= (9.0/8.0);
n /= pow((9.0/8.0), 3);
}
}
if(n < 1.05)
{
while(n >= 1.05)
{
result /= (9.0/8.0);
n *= pow(9.0/8.0, 3);
}
}
return (result*my_cbrt_1(n));
}
double my_cbrt_3(double n)
{
double ans = 0;
if(n == 0)
{
ans = 0;
}
else if(n < 0)
{
ans = -my_cbrt_3(-n);
}
else
{
double x = my_cbrt_2(n);
for(int i = 0; i <= 9; i++)
{
x = (((2.0/3)*x) + (n/(3*x*x*x)));
}
ans = x;
}
return ans;
}
int main()
{
double n;
cout<<" "<<setw(15)<<" "<<setw(23)<<"cbrt(n)"<<setw(24)<<"my_cbrt_3(n)"<<setw(30)<<"relative_error_per_cent"<<endl<<endl;
for(auto k : {-100, -10, -1, 0, 1, 10, 100})
//int values[] = {-100, -10, -1, 0, 1, 10, 100};
//for(int k = 0; k < 7; k++)
{
n = M_PI * pow(10.0, values[k]);
  
double relative_error_per_cent = 100 * ((my_cbrt_3(n) - cbrt(n)) / cbrt(n));
cout<<setw(20)<<n<<setw(20)<<cbrt(n)<<setw(20)<<my_cbrt_3(n)<<setw(25)<<relative_error_per_cent<<endl;
}
}