Question

In: Computer Science

Newtons method for approximating square roots. The next iteration is the average of the previous iteration...

Newtons method for approximating square roots.

The next iteration is the average of the previous iteration and the ratio of the number in question to the previous iteration.x_i = ( x_(i-1) + number/x_(i-1) ) / 2

if i is 0 the approximation is half the number.

Pre: number & interations are non-negative integers

Post: return an approximation of sqrt(number) , the return value is double

double newton(size_t number, size_t iterations){}

Solutions

Expert Solution

In this program, we have to approximate square root of a number using newtons method.

Initially set squareRoot = 0.5*number

then create a loop that repeats iterations times. Inside the loop set sqaureRoot = (squareRoot + (double)number/squareRoot)/2

After the loop terminates, return squareRoot

function:

double newton(size_t number, size_t iterations){
double squareRoot = (double)number/2;
for(int i = 1; i <= iterations; i++)
squareRoot = (squareRoot + (double)number/squareRoot)/2;
return squareRoot;
}

sample program to test this function:

#include <stdio.h>
#include <math.h>


double newton(size_t number, size_t iterations){
double squareRoot = (double)number/2;
for(int i = 1; i <= iterations; i++)
squareRoot = (squareRoot + (double)number/squareRoot)/2;
return squareRoot;
}

int main(){
double number; int iterations;
printf("Enter a number: ");
scanf("%lf", &number);
printf("Number of iterations: ");
scanf("%d",&iterations);
printf("\n\n Real square root: %lf\n",sqrt(number));
printf("Approximated square root: %lf\n",newton(number,iterations));
}

sample inputs and outputs:


Related Solutions

The “divide and average” method, an old time method for approximating the square root of any...
The “divide and average” method, an old time method for approximating the square root of any positive number a, can be formulated as x = (x + a/x) / 2 Write a well-structured M-file function based on the while…break loop structure to implement this algorithm. At each step estimate the error in your approximation as ε = abs(( Xnew − Xold )/Xnew Repeat the loop until e is less than or equal to a specified value. Design your program so...
I am asked to find the square roots using the bisection method for x * x...
I am asked to find the square roots using the bisection method for x * x - a = 0. I was wondering how the bisection method is performed. Let's suppose a = 9, so I would need to find the roots of x * x - 9 = 0. Also, from the 1st equation, when would the bisection method NOT output a root?
What are the square roots of 3+4i?
What are the square roots of 3+4i?
Do you think it would be a good method to use the average of the previous...
Do you think it would be a good method to use the average of the previous three months actual expenses as the current periods estimate? What are some advantages and disadvantages of this approach?
Explain when EXACTLY you would prefer the previous balance method to the average daily balance including...
Explain when EXACTLY you would prefer the previous balance method to the average daily balance including new purchases method in determining your finance charge.
use newtons method with value of x1=3 to approximate the root of equation to 6 decimal...
use newtons method with value of x1=3 to approximate the root of equation to 6 decimal places x^4=4x^2+25
7. Finding Roots Using the Bisection Method Write a function that implements the "bisection method" for...
7. Finding Roots Using the Bisection Method Write a function that implements the "bisection method" for finding the roots of function. The signature of your function should look like def find_root(f,a,b,n): where n is the maximum number of iterations of to search for the root. The code should follow this algorithm: We are given a continuous function f and numbers a and b and with a<b with f(a)<0<f(b). From the intermediate value theorem we know that there exists a c...
Create a python code that calculates fixed point iteration method using a for loop.
Create a python code that calculates fixed point iteration method using a for loop.
Use the Fixed-Point Iteration Method to find the root of f ( x ) = x...
Use the Fixed-Point Iteration Method to find the root of f ( x ) = x e^x/2 + 1.2 x - 5 in the interval [1,2].
2.6 Consider all the possible sets of two square roots s, t of 1 (mod 35)...
2.6 Consider all the possible sets of two square roots s, t of 1 (mod 35) where s ≢ t (mod 35) (there are six of them, since addition is commutative (mod 35). For all possible combinations, compute gcd(s + t, 35). Which combinations give you a single prime factor of 35? 2.7 Using CRT notation, show what is going on for all the combinations you considered in #2.6. Explain why gcd(s + t, 35) sometimes gave you a factor,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT