Question

In: Computer Science

Write three functions that compute the square root of an argument x using three different methods....

Write three functions that compute the square root of an argument x using three different methods. The methods are increasingly sophisticated, and increasingly efficient. The square root of a real number x is the value s such that s*s == x. For us, the values will be double precision variables and so may not be perfectly accurate. Also, for us, assume that x is in the range 0.0 to 100.0. You program should have a main() that asks the user for x and an accuracy bound, and then prints out the approximate square root for each of the three methods. If x is negative write an error message and exit. One. Write a pure function double sqrtLS(x, accuracy) that computes the square root of x using Linear Search. Do this in a loop that starts s at 0.0 and bestError at x. Each iteration of the loop computes error = |x-s*s|. If error< bestError, set bestError to error, and bestS to the current s. Then increment s by accuracy and move on. At the end of the loop, return bestS. This method is linear search, it searches along the number line for the best s that is within accuracy of the true value. Use the function double fabs(double) from the math library to compute absolute value. Two. Write a pure function double sqrtBS(x,accuracy) that computes the square root of x using Binary Search. (This method is also called bisection.) Start a variable low at 0.0 and a variable high at 10.0. This assumes that x will be no larger than 100.0 In a while loop do this: Compute the midpoint mid=(low+high)/2. If |x-mid*mid| ≤ accuracy return mid. If mid*mid < x set low = mid. Else set high = mid. Three. Write a pure function double sqrtNM(x, accuracy) that computes the square root of x using Newton’s Method. Recall the method: Start out an initial guess s=1.0 (or any value). Now repeatedly improve the guess: s = (s + x/s)/2 When the guess meets the ending condition |x-s*s| is ≤ bound return s. M:\ClassApps>gcc -lm squareRoot.c M:\ClassApps>.\a Enter x --> 47.5 Enter error bound --> 1e-5 sqrtLS(47.500)= 6.892020; s*s = 47.499940 sqrtBS(47.500)= 6.892024; s*s = 47.499999 sqrtNM(47.500)= 6.892024; s*s = 47.500001 Use ANSI-C syntax. Do not mix tabs and spaces. Do not use break or continue.

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h>

#include <math.h>

float sqrtLS(float x,float accuracy)
{
int arr[100],i=0;

float bestError=x,s=0,error,bestS;
for( s=0;s<100;s+=accuracy)
{
error=abs(x-s*s);
if(error<bestError)
{
bestError=error;
bestS=s;
}
}
return bestS;
}
float sqrtBS(float x,float accuracy)
{
float low=0,high=10.0;
float mid=(low+high)/2;
  
while(1)
{
  
mid=(low+high)/2;
if(abs(x-mid*mid)<=accuracy)
{
return mid;
}
else if(mid*mid<x)
{
low=mid;
}
else high=mid;
}
}
  
float sqrtNM(float x,float accuracy)
{
float s=1.0;
while(1)
{
s = (s + x/s)/2;
if((abs(x-s*s))<= accuracy)
{
return s;
}
}
  
}
  
  

int main() {
   float x;
   float accuracy,ans;
   printf("Enter x");
   scanf("%f",&x);
   if(x<0)
   {
   printf("Invalid Input\n");
   }
   else
   {
   printf("Enter Accuracy\n");
   scanf("%f",&accuracy);
   ans= sqrtLS(x,accuracy);
   printf("Square Root by Linear Search %f\n",ans);
   ans= sqrtBS(x,accuracy);
   printf("Square Root by Binary Search %f\n",ans);
   ans= sqrtNM(x,accuracy);
   printf("Square Root by Newton Method %f\n",ans);
     
   }
  
   return 0;
}


Related Solutions

write a function to determine the square root of a number. The square root of a...
write a function to determine the square root of a number. The square root of a number can be approximated by repeated calculation using the formula NG = 0.5(LG + N/LG) where NG stands for the next guess and LG stands for the last guess. The loop should repeat until the difference between NG and LG is less than 0.00001. Use an initial guess of 1.0. Write a driver program to test your square root function. I WANT THIS PROGRAM...
Java please. Write a static method sqrt()that takes a double argument and returns the square root...
Java please. Write a static method sqrt()that takes a double argument and returns the square root of that number using newton's method to compute result.
Write a program to compute the root of the function f(x) = x3 + 2 x2...
Write a program to compute the root of the function f(x) = x3 + 2 x2 + 10 x - 20 by Newton method ( x0 =2 ). Stop computation when the successive values differ by not more than 0.5 * 10-5 . Evaluate f(x) and f '(x) using nested multiplication. The output should contain: (1) A table showing at each step the value of the root , the value of the function,and the error based upon successive approximation values...
Write a function double mysqrt( double x ) which computes the value of the square root...
Write a function double mysqrt( double x ) which computes the value of the square root of x using the bisection method. First you need to set the left and right bounds for x. If 0<x<1 then lt = x and rt = 1 and the sqrt(x) is somewhere in between. If x > 1 then lt = 1 and rt = x and sqrt(x) is somewhere in between. In a loop you need to compute the mid value between...
Estimate the area A between the graph of the function f(x)= square root of x and...
Estimate the area A between the graph of the function f(x)= square root of x and the interval [0,49]. Use an approximation scheme with n=2,5, and 10 rectangles. Use the right endpoints. Round your answers to three decimal places. A2= A5= A10= Click
A positive number is added to the reciprocal of its square root. Use Calculus methods to...
A positive number is added to the reciprocal of its square root. Use Calculus methods to answer the following questions:what if anything, is the smallest such sum? What, if any, positive x-values will yield the smallest sum? Give exact answers as well as answers accurate to Five decimal place. Be sure to give a mathematical justification that you answer represents a minimum and not a maximum.
Use f(x) = ?2x, g(x) = square root of x and h(x) = |x| to find...
Use f(x) = ?2x, g(x) = square root of x and h(x) = |x| to find and simplify expressions for the following functions and state the domain of each using interval notation. a . (h ? g ? f)(x) b. (h ? f ? g)(x) (g ? f ? h)(x)
Babylonian Algorithm. The Babylonian algorithm to compute the square root of a positive number n is as follows:
Chapter 3 Exercise 1Babylonian Algorithm. The Babylonian algorithm to compute the square root of a positive number n is as follows:      1. Make a guess at the answer (you can pick n/2 as your initial guess).      2. Computer = n / guess.      3. Set guess = (guess +r) / 2.      4. Go back to step 2 until the last two guess values are within 1% of each other.Write a program that inputs an integer for n, iterates through the Babylonian algorithm until the guess...
Find the domain of the function g(x) = square root of 3x+3
Find the domain of the function g(x) = square root of 3x+3
An investor's utility function for money (Bernoulli utility function) is the square root of money: u(x)=√x....
An investor's utility function for money (Bernoulli utility function) is the square root of money: u(x)=√x. Her decision making can be modeled by assuming that she maximizes her expected utility. Her current wealth is 100. (All quantities are in hundreds of dollars.) She has the opportunity to buy a security that either pays 8 (the "good outcome") or loses 1 (the "bad outcome"). She can buy as many units as she wishes. For example, if she buys 5 units, she...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT