Question

Write three functions that compute the square root of an argument using three different methods. The methods are increasingly
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* One : Square root of a number using linear search */

#include<stdio.h>
#include<math.h>
double sqartLs(double x, double accuracy )   
{

if (x == 0.0 || x == 1.0)
return x;
  
   double s = 1.0, result = 1.0,Error=0.0,bestS,bestError;
bestError=x;   
while (result <= x)
{
s=s+0.1;
result = s * s;
if(result<=accuracy)
   {
   Error=x-s*s;
   printf("\nError is:%lf\n",Error);
   if(Error<bestError)
   {
       Error=bestError;
   }
}
}
bestS=s-0.1;
return fabs(bestS);
}
  

int main()
{
double x,accuracy;
   printf("Enter the value of x");
   scanf("%lf",&x);
   if(x>=0.0 && x<=100.0)
   {
       accuracy=x;
printf("\n Square root is :%lf",sqartLs(x,accuracy)) ;
}
else
{
   printf("Range is not satisfied");
   }
return 0;
}
/* Pure function of square root with linear search ( above program part)*/

double sqartLs(double x, double accuracy )   
{
   if (x == 0.0 || x == 1.0)
return x;
    double s = 1.0, result = 1.0,Error=0.0,bestS,bestError;
bestError=x;   
while (result <= x)
{
s=s+0.1;
result = s * s;
if(result<=accuracy)
   {
   Error=x-s*s;
   printf("\nError is:%lf\n",Error);
   if(Error<bestError)
   {
       Error=bestError;
   }
}
}
bestS=s-0.1;
return fabs(bestS);
}

output(linear):

/* Two: Square root of a number using Binary search */

#include<stdio.h>
#include<math.h>
double sqartBs(double x, double accuracy )
{
if (x == 0.0 || x == 1.0)
return x;
  
double low =1.0, high = 100.0, ans=0.0;
while (low <= high)
{   
double mid = (low + high) / 2;
  
if ((mid*mid - x)<=accuracy)
{
return mid;  
       }


if (mid*mid < x)
{
low = mid + 10;
ans = mid;
}
else // If mid*mid is greater than x
high = mid-1.0;   
}
return ans;
}

int main()
{
double x,accuracy;
   printf("Enter the value of x");
   scanf("%lf",&x);
   if(x>=0.0 && x<=100.0)
   {
       accuracy=x;
printf("\n Square root is :%lf",sqartBs(x,accuracy)) ;
}
else
{
   printf("Range is not satisfied");
   }
return 0;
}

/* Pure function of square root with binary search ( from above program part)*/

double sqartBs(double x, double accuracy )
{
if (x == 0.0 || x == 1.0)
return x;
  
double low =1.0, high = 100.0, ans=0.0;
while (low <= high)
{   
double mid = (low + high) / 2;
  
if ((mid*mid - x)<=accuracy)
{
return mid;  
       }


if (mid*mid < x)
{
low = mid + 10;
ans = mid;
}
else // If mid*mid is greater than x
high = mid-1.0;   
}
return ans;
}
output(binary search):

/* Three: Newton method to calculate square root of a number */

#include <stdio.h>
double fabsolute(double number )
{   
if(number < 0){
number = -number;
}
return number;
}

double sqartNt(double x, double accuracy )
{
const double guess = 0.00001;
double s= 1.0;
while(fabsolute(s * s - x) >= guess)
{
   s = (x/s + s)/2.0;
}
return s;
}

int main()
{
double x,accuracy;
   printf("Enter the value of x");
   scanf("%lf",&x);
   if(x>=0.0 && x<=100.0)
   {
       accuracy=x;
printf("\n Square root is :%lf",sqartNt(x,accuracy)) ;
}
else
{
   printf("Range is not satisfied");
   }
return 0;   
}

/* Pure function of square root with Newton method ( from above program part)*/

double sqartNt(double x, double accuracy )
{
const double guess = 0.00001;
double s= 1.0;
while(fabsolute(s * s - x) >= guess)
{
   s = (x/s + s)/2.0;
}
return s;
}

output(Newton method)

Add a comment
Know the answer?
Add Answer to:
Write three functions that compute the square root of an argument using three different methods. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Matlab problem using newton raphson to find square root of number

    Need help modifying my Matlab script below (myscript calculates the square root of a number. using a Newton-Raphson method with 1 as the initial guess, calculates true and estimated error, and shows each iteration).-I need to create three new functions each of which should be called in the main script. These functions are needed to replace code that is currently in my script shown below.-I need to create these functions:A function to find f(x)A function to find f '(x) ?A...

  • find the root(s) of the following functions using both Newton's method and the secant method, using...

    find the root(s) of the following functions using both Newton's method and the secant method, using tol = eps. 3 Find the root s of the following functions using both Newton's ulethod and the anat inethod using tol epa. . You will vood to experiment with the parameters po, pl, ad maxits. . For each root, visualize the iteration history of both methods by plotting the albsolute errors, as a function . Label the two curves (Newton's method and secaut...

  • 45-3. Modify the code used in Example 4 to find the root only at f(x)<0.01 using...

    45-3. Modify the code used in Example 4 to find the root only at f(x)<0.01 using Newton-Rephson Method without showing any iteration. Also find the root of equation, f(x) = x 9-3x -10, take initial guess, Xo=2 العقدة College of 9:05 mybb.qu.edu.ca Numerical Methods (Lab.) GENG 300 Summer 2020 5.1.2 Open Methods - Newton-Raphson Method f(x) *1+1 = x; - Matlab Code Example:4 function mynewtraph.t1.x0,-) XXO for ilin x - x - x)/1 x) disp 1 x) <0.01 break end...

  • You will write three static methods to manipulate an input String in different ways using various...

    You will write three static methods to manipulate an input String in different ways using various String methods. You need to provide the code for each of the three static methods in class StringPlay (requirements for each listed below). You will also change the control statement in the test harness to allow for variations in the sentinel value. You need to modify the loop control condition in Lab09.java so that user inputs of ‘finish’, “FINISH”, “FiniSH”, “fINISH”, etc. will end...

  • Using MATLAB or FreeMat ---------------------------- Bisection Method and Accuracy of Rootfinding Consider the function f(0) =...

    Using MATLAB or FreeMat ---------------------------- Bisection Method and Accuracy of Rootfinding Consider the function f(0) = 3 cos 2r cos 4-2 cos Garcos 3r - 6 cos 2r sin 2r-5.03r +5/2. This function has exactly one root in the interval <I<1. Your assignment is to find this root accurately to 10 decimal places, if possible. Use MATLAB, which does all calculations in double precision, equivalent to about 16 decimal digits. You should use the Bisection Method as described below to...

  • Lab 7 Add three instance variables to your class RightTriangle from lab 5. The first two...

    Lab 7 Add three instance variables to your class RightTriangle from lab 5. The first two are of type int and are called xLoc and yLoc and the third is of type int called ID. Also add a static variable of type double called scaleFactor. This should be initialized to a default value of 1. Update the constructor to set the three new instance variables and add appropriate get and set methods for the four new variables. All set methods...

  • python code for guessing game users enter the number the computer will guess 8:38 PM 100...

    python code for guessing game users enter the number the computer will guess 8:38 PM 100 nstructure.com 1. Number guessing game : (5 points) 1) Requirements: Your program will ask the user to enter a number between 1 and 1000. Then it will try to guess the correct number with the guaranteed minimal average number of guesses. Which search algorithm should you use? 2) Expected Output: Enter a number between 1 and 1000? 784 Computer guesses: 500 Type 'l' if...

  • Please Write in Java 15 salesmen have performed sales in 10 different cities and the information...

    Please Write in Java 15 salesmen have performed sales in 10 different cities and the information is stored in a two-dimensional array. For example: City 0 City 1 City 2 City … City 9 salesman 0 3.4 4.0 2.6 …… 3.5 salesman 1 3.7 4.44 1.90 …… 5.9 salesman … 1.5 5.9 7.4 …… 0.0 salesman 14 44.4 5.6 7.8 ….. 0.9 Write a class called Employee that contains the following methods A static method called totalSales() (String name, double...

  • Using the following create the nessecary static methods to pass random arrays with the TempartureWithArrays and...

    Using the following create the nessecary static methods to pass random arrays with the TempartureWithArrays and Temperature classes -------------------------------------------------------------------------------------- public class TemperatureWithArrays {    public static final int ARRAY_SIZE = 5;    public static void main(String[] args)    {        int x;        Temperature temp1 = new Temperature(120.0, 'C');        Temperature temp2 = new Temperature(100, 'C');        Temperature temp3 = new Temperature(50.0, 'C');        Temperature temp4 = new Temperature(232.0, 'K');        Temperature tempAve =...

  • python 2..fundamentals of python 1.Package Newton’s method for approximating square roots (Case Study 3.6) in a...

    python 2..fundamentals of python 1.Package Newton’s method for approximating square roots (Case Study 3.6) in a function named newton. This function expects the input number as an argument and returns the estimate of its square root. The script should also include a main function that allows the user to compute square roots of inputs until she presses the enter/return key. 2.Convert Newton’s method for approximating square roots in Project 1 to a recursive function named newton. (Hint: The estimate of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT