Question: Write a function to implement this. Correct answer immediate upvote The sqrt) function of a number can be calculated using the following recursive definition, which

Write a function to implement this. Correct answer immediate upvote

Write a function to implement this. Correct answer immediate upvote The sqrt)

The sqrt) function of a number can be calculated using the following recursive definition, which is based on Newton's method. We can write a recursive algorithm that finds the square root of a real valued (double or floating point) number to within a given tolerance epsilon (where epsilon might be 0.0001 if you want a result that is correct to 4 decimal places) Suppose x is a nonnegative real number, and a is the approximate square root of x and epsilon is the tolerance. Start with an approximate guess of a x 1. if a2-x| epsilon , then a is the square root of x within the tolerance. (this is your base case) 2. otherwise, replace a with and repeat step 1 Write a function to implement this algorithm to find the square root of a nonnegative real number. This function will take two double numbers as input parameters, and it will return a double result, which should be the square root of the value initially asked for (to within epsilon tolerance). The two parameters are the value x we want to find the square root of, and the value a, which is the current guess/iteration. So for example, if you want to find the square root of 5, you would first call your function with 5, and an initial guess of a-x-5, like this double x-5; double a-x / the initial guess double root; root sqrt (x, a); You can either define epsilon as a const double in your function, like const double epsilon-0.0001; or an even better approach is to have a third default parameter for your sqrt0) function call epsilon that defaults to the usual desired tolerance

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!