In: Computer Science
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 lt and rt and
compare the mid square with x. Depending on this comparison, you
would need to set either lt or
rt to the mid. The loop executes as long as fabs(rt-lt) > ERR .
You can set the const double ERR =
1-10. The function needs to return the mid.
In the main function use a loop to read a double, then print is
square root in 2 ways: one using
cmath’s sqrt function and the other using mysqrt. Of course, the
code is correct if the 2 values
from the functions are very close to eachother. The loop terminates
when the user enters q.
Sample output:
Enter a non-negative real number to compute the square root:
0
sqrt( 0 ) = 0
mysqrt( 0 ) = 0
Enter a non-negative real number to compute the square root:
1
sqrt( 1 ) = 1
mysqrt( 1 ) = 1
Enter a non-negative real number to compute the square root:
2
sqrt( 2 ) = 1.41421356237309514547462185874
mysqrt( 2 ) = 1.41421356232604011893272399902
Enter a non-negative real number to compute the square root:
3
sqrt( 3 ) = 1.73205080756887719317660412344
mysqrt( 3 ) = 1.73205080762272700667381286621
Enter a non-negative real number to compute the square root:
16
sqrt( 16 ) = 4
mysqrt( 16 ) = 4.00000000001091393642127513885
Enter a non-negative real number to compute the square root:
0.26
sqrt( 0.260000000000000008881784197001 ) =
0.509901951359278515774064999277
mysqrt( 0.260000000000000008881784197001 ) =
0.509901951344217785333512438228
Enter a non-negative real number to compute the square root:
0.25
sqrt( 0.25 ) = 0.5
mysqrt( 0.25 ) = 0.500000000029103830456733703613
Enter a non-negative real number to compute the square root:
0.36
sqrt( 0.359999999999999986677323704498 ) =
0.599999999999999977795539507497
mysqrt( 0.359999999999999986677323704498 ) =
0.599999999999999977795539507497
Enter a non-negative real number to compute the square root: q
C++,will leave a like if correct and will appreciate if able to
copy and paste.
he bisection method is an algorithm, and we will explain it in terms of its steps.
Description: Given a closed interval [a,b] on which f changes sign, we divide the interval in half and note that f must change sign on either the right or the left half (or be zero at the midpoint of [a,b].) We then replace [a,b] by the half-interval on which f changes sign. This process is repeated until the interval has total length less than . In the end we have a closed interval of length less than on which f changes sign. The IVT guarantees that there is a zero of f in this interval. The endpoints of this interval, which are known, must be within of this zero.
Initialization: The bisection method is initialized by specifying the function f(x), the interval [a,b], and the tolerance > 0.
We also check whether f(a) = 0 or f(b) = 0, and if so return the value of a or b and exit.
Loop: Let m = (a + b)/2 be the midpoint of the interval [a,b]. Compute the signs of f(a), f(m), and f(b).
If any are zero, return the corresponding point and exit.
Assuming none are zero, if f(a) and f(m) have opposite sides, replace b by m, else replace a by m.
If the length of the [a,b] is less than , return the value of a and exit.
Analysis: When we enter the loop f(a) and f(b) have opposite sign. It follows that either f(m) and f(a) have opposite sign or f(m) and f(b) have oppposite sign. Thus the initial conditions are still satisfied each time we enter the loop.
The length of the initial interval is (b - a). After one time through the loop the length is (b - a)/2, after two times it is (b - a)/4, and after n passes through the loop, the length of the remaining interval is (b - a)/2n. No matter how small , eventually (b - a)/2n < . In fact we can solve this inequality for n:
(b - a)/2n | < | |
2n | > | (b - a)/ |
n ln 2 | > | ln(b - a) - ln() |
n | > | [ln(b - a) - ln()]/ln 2. |
Thus the algorithm terminates after at most M passes through the loop where M is the first integer larger than [ln(b - a) - ln()]/ln 2.
Examples
Example 1. Starting with the interval [1,2], find srqt(2) to within two decimal places (to within an error of .01).
The function involved is f(x) = x2 -2. The following table steps through the iteration until the size of the interval, given in the last column, is less than .01. The final result is the approximation 1.41406 for the sqrt(2). This is guaranteed by the algorithm to be within .01 (actually, to within 1/128) of sqrt(2). In reality it agrees with sqrt(2) to three decimal places, not just two.
a | b | m = (a + b)/2 | f(a) | f(b) | f(m) | b-a | ||
---|---|---|---|---|---|---|---|---|
1 | 2 | 1.5 | -1 | 2 | .25 | 1 | ||
1 | 1.5 | 1.25 | -1 | .25 | -.4375 | .5 | ||
1.25 | 1.5 | 1.375 | -.4375 | .25 | -0.109375 | .25 | ||
1.375 | 1.5 | 1.4375 | -0.109375 | .25 | .0664062 | .125 | ||
1.375 | 1.4375 | 1.40625 | -0.109375 | .0664062 | -.0224609 | .0625 | ||
1.40625 | 1.4375 | 1.42187 | -.0224609 | .0664062 | .0217285 | .03125 | ||
1.40625 | 1.42187 | 1.41406 | -.0224609 | .0217285 | -.0004343 | .015625 | ||
1.41406 | 1.42187 | -.0004343 | .0217285 | .0078125 |
Equipment Check: The following form allows you to compute values of a function g(x). Your task is to find a zero of g(x) on the interval [0,3] to within an accuracy of .5. Take a out a piece of paper and a pencil and step through the algorithm. The "check answer" button will display the answer you should get and the number of times you should have done the loop. The "explain" button will show you a table similar to the one above. Don't look at the table unless you are really stuck or have worked through the entire problem.
x = g(x) = he bisection method is an algorithm, and we will explain it in terms of its steps.
Description: Given a closed interval [a,b] on which f changes sign, we divide the interval in half and note that f must change sign on either the right or the left half (or be zero at the midpoint of [a,b].) We then replace [a,b] by the half-interval on which f changes sign. This process is repeated until the interval has total length less than . In the end we have a closed interval of length less than on which f changes sign. The IVT guarantees that there is a zero of f in this interval. The endpoints of this interval, which are known, must be within of this zero.
Initialization: The bisection method is initialized by specifying the function f(x), the interval [a,b], and the tolerance > 0.
We also check whether f(a) = 0 or f(b) = 0, and if so return the value of a or b and exit.
Loop: Let m = (a + b)/2 be the midpoint of the interval [a,b]. Compute the signs of f(a), f(m), and f(b).
If any are zero, return the corresponding point and exit.
Assuming none are zero, if f(a) and f(m) have opposite sides, replace b by m, else replace a by m.
If the length of the [a,b] is less than , return the value of a and exit.
Analysis: When we enter the loop f(a) and f(b) have opposite sign. It follows that either f(m) and f(a) have opposite sign or f(m) and f(b) have oppposite sign. Thus the initial conditions are still satisfied each time we enter the loop.
The length of the initial interval is (b - a). After one time through the loop the length is (b - a)/2, after two times it is (b - a)/4, and after n passes through the loop, the length of the remaining interval is (b - a)/2n. No matter how small , eventually (b - a)/2n < . In fact we can solve this inequality for n:
(b - a)/2n | < | |
2n | > | (b - a)/ |
n ln 2 | > | ln(b - a) - ln() |
n | > | [ln(b - a) - ln()]/ln 2. |
Thus the algorithm terminates after at most M passes through the loop where M is the first integer larger than [ln(b - a) - ln()]/ln 2.
Examples
Example 1. Starting with the interval [1,2], find srqt(2) to within two decimal places (to within an error of .01).
The function involved is f(x) = x2 -2. The following table steps through the iteration until the size of the interval, given in the last column, is less than .01. The final result is the approximation 1.41406 for the sqrt(2). This is guaranteed by the algorithm to be within .01 (actually, to within 1/128) of sqrt(2). In reality it agrees with sqrt(2) to three decimal places, not just two.
a | b | m = (a + b)/2 | f(a) | f(b) | f(m) | b-a | ||
---|---|---|---|---|---|---|---|---|
1 | 2 | 1.5 | -1 | 2 | .25 | 1 | ||
1 | 1.5 | 1.25 | -1 | .25 | -.4375 | .5 | ||
1.25 | 1.5 | 1.375 | -.4375 | .25 | -0.109375 | .25 | ||
1.375 | 1.5 | 1.4375 | -0.109375 | .25 | .0664062 | .125 | ||
1.375 | 1.4375 | 1.40625 | -0.109375 | .0664062 | -.0224609 | .0625 | ||
1.40625 | 1.4375 | 1.42187 | -.0224609 | .0664062 | .0217285 | .03125 | ||
1.40625 | 1.42187 | 1.41406 | -.0224609 | .0217285 | -.0004343 | .015625 | ||
1.41406 | 1.42187 | -.0004343 | .0217285 | .0078125 |
Equipment Check: The following form allows you to compute values of a function g(x). Your task is to find a zero of g(x) on the interval [0,3] to within an accuracy of .5. Take a out a piece of paper and a pencil and step through the algorithm. The "check answer" button will display the answer you should get and the number of times you should have done the loop. The "explain" button will show you a table similar to the one above. Don't look at the table unless you are really stuck or have worked through the entire problem.
x = g(x) = he bisection method is an algorithm, and we will explain it in terms of its steps.
Description: Given a closed interval [a,b] on which f changes sign, we divide the interval in half and note that f must change sign on either the right or the left half (or be zero at the midpoint of [a,b].) We then replace [a,b] by the half-interval on which f changes sign. This process is repeated until the interval has total length less than . In the end we have a closed interval of length less than on which f changes sign. The IVT guarantees that there is a zero of f in this interval. The endpoints of this interval, which are known, must be within of this zero.
Initialization: The bisection method is initialized by specifying the function f(x), the interval [a,b], and the tolerance > 0.
We also check whether f(a) = 0 or f(b) = 0, and if so return the value of a or b and exit.
Loop: Let m = (a + b)/2 be the midpoint of the interval [a,b]. Compute the signs of f(a), f(m), and f(b).
If any are zero, return the corresponding point and exit.
Assuming none are zero, if f(a) and f(m) have opposite sides, replace b by m, else replace a by m.
If the length of the [a,b] is less than , return the value of a and exit.
Analysis: When we enter the loop f(a) and f(b) have opposite sign. It follows that either f(m) and f(a) have opposite sign or f(m) and f(b) have oppposite sign. Thus the initial conditions are still satisfied each time we enter the loop.
The length of the initial interval is (b - a). After one time through the loop the length is (b - a)/2, after two times it is (b - a)/4, and after n passes through the loop, the length of the remaining interval is (b - a)/2n. No matter how small , eventually (b - a)/2n < . In fact we can solve this inequality for n:
(b - a)/2n | < | |
2n | > | (b - a)/ |
n ln 2 | > | ln(b - a) - ln() |
n | > | [ln(b - a) - ln()]/ln 2. |
Thus the algorithm terminates after at most M passes through the loop where M is the first integer larger than [ln(b - a) - ln()]/ln 2.
Examples
Example 1. Starting with the interval [1,2], find srqt(2) to within two decimal places (to within an error of .01).
The function involved is f(x) = x2 -2. The following table steps through the iteration until the size of the interval, given in the last column, is less than .01. The final result is the approximation 1.41406 for the sqrt(2). This is guaranteed by the algorithm to be within .01 (actually, to within 1/128) of sqrt(2). In reality it agrees with sqrt(2) to three decimal places, not just two.
a | b | m = (a + b)/2 | f(a) | f(b) | f(m) | b-a | ||
---|---|---|---|---|---|---|---|---|
1 | 2 | 1.5 | -1 | 2 | .25 | 1 | ||
1 | 1.5 | 1.25 | -1 | .25 | -.4375 | .5 | ||
1.25 | 1.5 | 1.375 | -.4375 | .25 | -0.109375 | .25 | ||
1.375 | 1.5 | 1.4375 | -0.109375 | .25 | .0664062 | .125 | ||
1.375 | 1.4375 | 1.40625 | -0.109375 | .0664062 | -.0224609 | .0625 | ||
1.40625 | 1.4375 | 1.42187 | -.0224609 | .0664062 | .0217285 | .03125 | ||
1.40625 | 1.42187 | 1.41406 | -.0224609 | .0217285 | -.0004343 | .015625 | ||
1.41406 | 1.42187 | -.0004343 | .0217285 | .0078125 |
Equipment Check: The following form allows you to compute values of a function g(x). Your task is to find a zero of g(x) on the interval [0,3] to within an accuracy of .5. Take a out a piece of paper and a pencil and step through the algorithm. The "check answer" button will display the answer you should get and the number of times you should have done the loop. The "explain" button will show you a table similar to the one above. Don't look at the table unless you are really stuck or have worked through the entire problem.
x = g(x) =