Question

Encapsulate the following Python code from Section 7.5 in a function named my_sqrt that takes a...

Encapsulate the following Python code from Section 7.5 in a function named my_sqrt that takes a as a parameter, chooses a starting value for x, and returns an estimate of the square root of a.

while True:
y = (x + a/x) / 2.0
if y == x:
break
x = y


Part 2

Write a function named test_sqrt that prints a table like the following using a while loop, where "diff" is the absolute value of the difference between my_sqrt(a) and math.sqrt(a).

a = 1 | my_sqrt(a) = 1 | math.sqrt(a) = 1.0 | diff = 0.0
a = 2 | my_sqrt(a) = 1.41421356237 | math.sqrt(a) = 1.41421356237 | diff = 2.22044604925e-16
a = 3 | my_sqrt(a) = 1.73205080757 | math.sqrt(a) = 1.73205080757 | diff = 0.0
a = 4 | my_sqrt(a) = 2.0 | math.sqrt(a) = 2.0 | diff = 0.0
a = 5 | my_sqrt(a) = 2.2360679775 | math.sqrt(a) = 2.2360679775 | diff = 0.0
a = 6 | my_sqrt(a) = 2.44948974278 | math.sqrt(a) = 2.44948974278 | diff = 0.0
a = 7 | my_sqrt(a) = 2.64575131106 | math.sqrt(a) = 2.64575131106 | diff = 0.0
a = 8 | my_sqrt(a) = 2.82842712475 | math.sqrt(a) = 2.82842712475 | diff = 4.4408920985e-16
a = 9 | my_sqrt(a) = 3.0 | math.sqrt(a) = 3.0 | diff = 0.0

Modify your program so that it outputs lines for a values from 1 to 25 instead of just 1 to 9.

You should submit a script file and a plain text output file (.txt) that contains the test output. Multiple file uploads are permitted.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python code:

import math

def my_sqrt(a): # Same as given in question
x = a
while True:
y = (x + a / x) / 2.0
if y == x:
break
x = y
return x

def test_sqrt():
for a in range(1, 26): # Loop over the values of a
my, lib = my_sqrt(a), math.sqrt(a) # Calculate and store to avoid repetition
print(f"a = {a} | my_sqrt(a) = {my} | math.sqrt(a) = {lib} | diff = {abs(my - lib)}") # Print result line


test_sqrt()

Output:

a = 1 | my_sqrt(a) = 1 | math.sqrt(a) = 1.0 | diff = 0.0
a = 2 | my_sqrt(a) = 1.414213562373095 | math.sqrt(a) = 1.4142135623730951 | diff = 2.220446049250313e-16
a = 3 | my_sqrt(a) = 1.7320508075688772 | math.sqrt(a) = 1.7320508075688772 | diff = 0.0
a = 4 | my_sqrt(a) = 2.0 | math.sqrt(a) = 2.0 | diff = 0.0
a = 5 | my_sqrt(a) = 2.23606797749979 | math.sqrt(a) = 2.23606797749979 | diff = 0.0
a = 6 | my_sqrt(a) = 2.449489742783178 | math.sqrt(a) = 2.449489742783178 | diff = 0.0
a = 7 | my_sqrt(a) = 2.6457513110645907 | math.sqrt(a) = 2.6457513110645907 | diff = 0.0
a = 8 | my_sqrt(a) = 2.82842712474619 | math.sqrt(a) = 2.8284271247461903 | diff = 4.440892098500626e-16
a = 9 | my_sqrt(a) = 3.0 | math.sqrt(a) = 3.0 | diff = 0.0
a = 10 | my_sqrt(a) = 3.162277660168379 | math.sqrt(a) = 3.1622776601683795 | diff = 4.440892098500626e-16
a = 11 | my_sqrt(a) = 3.3166247903554 | math.sqrt(a) = 3.3166247903554 | diff = 0.0
a = 12 | my_sqrt(a) = 3.4641016151377544 | math.sqrt(a) = 3.4641016151377544 | diff = 0.0
a = 13 | my_sqrt(a) = 3.6055512754639896 | math.sqrt(a) = 3.605551275463989 | diff = 4.440892098500626e-16
a = 14 | my_sqrt(a) = 3.7416573867739413 | math.sqrt(a) = 3.7416573867739413 | diff = 0.0
a = 15 | my_sqrt(a) = 3.872983346207417 | math.sqrt(a) = 3.872983346207417 | diff = 0.0
a = 16 | my_sqrt(a) = 4.0 | math.sqrt(a) = 4.0 | diff = 0.0
a = 17 | my_sqrt(a) = 4.123105625617661 | math.sqrt(a) = 4.123105625617661 | diff = 0.0
a = 18 | my_sqrt(a) = 4.242640687119286 | math.sqrt(a) = 4.242640687119285 | diff = 8.881784197001252e-16
a = 19 | my_sqrt(a) = 4.358898943540673 | math.sqrt(a) = 4.358898943540674 | diff = 8.881784197001252e-16
a = 20 | my_sqrt(a) = 4.47213595499958 | math.sqrt(a) = 4.47213595499958 | diff = 0.0
a = 21 | my_sqrt(a) = 4.58257569495584 | math.sqrt(a) = 4.58257569495584 | diff = 0.0
a = 22 | my_sqrt(a) = 4.69041575982343 | math.sqrt(a) = 4.69041575982343 | diff = 0.0
a = 23 | my_sqrt(a) = 4.795831523312719 | math.sqrt(a) = 4.795831523312719 | diff = 0.0
a = 24 | my_sqrt(a) = 4.898979485566356 | math.sqrt(a) = 4.898979485566356 | diff = 0.0
a = 25 | my_sqrt(a) = 5.0 | math.sqrt(a) = 5.0 | diff = 0.0

Add a comment
Know the answer?
Add Answer to:
Encapsulate the following Python code from Section 7.5 in a function named my_sqrt that takes a...
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
  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

  • Question 1: Creating a user-defined function Write a user-defined MATLAB function named PBTask4pl_f.m for the following...

    Question 1: Creating a user-defined function Write a user-defined MATLAB function named PBTask4pl_f.m for the following math function with x the input argument and y the output y(x)=0.8x4-13x2-5x The function should work for x being a scalar or a vector. Write a script file named PBTask4pl.m to a) Use the function to calculate y(3) and y(5) and display the results in command window b) Use the function to make a plot of the function y(x) for -5:5: x 5:5. Label...

  • Using python Question 13 (20 points) Write a function named Linestats that finds the number of...

    Using python Question 13 (20 points) Write a function named Linestats that finds the number of consonant and vowel letters on each line of a file and writes those statistics to a corresponding line of a new file, separated by a space. Definitions: A vowel letter is one of a, e, i, o, u, along with the corresponding uppercase letters..A consonant letter is not a vowel letter. The function linestats takes two parameters: 1. inFile, a string, the name of...

  • the language is python Instructions Forum Tutoring Problem program. Write a function concatenate_files(filename1, filename2, new filename) that concatenates the text from two source files such...

    the language is python Instructions Forum Tutoring Problem program. Write a function concatenate_files(filename1, filename2, new filename) that concatenates the text from two source files such that the text from the file named by argument filename 2 follows the text from filename1. The concatenated text is written to a new file with the name given by new_filename. Your function O must not return anything. We have provided sample input files named part1.txt and part2.txt containing a portion of the text from...

  • Hi it's my code for python I almost finished my project but only one thing left which is most con...

    Hi it's my code for python I almost finished my project but only one thing left which is most confusing part please help me I have to find most occurring ending character from a to z For instance, output should be like this I have to find a to z. Words starting with ‘a’ end mostly with ‘O’ Words starting with ‘b’ end mostly with ‘O’ ...... No words start with ‘O’(If there's no word in the character from a...

  • ____________ % This function is a modified versio of the newtmult function obtained % from % “Ap...

    ____________ % This function is a modified versio of the newtmult function obtained % from % “Applied Numerical Methods with MATLAB, Chapra, % 3rd edition, 2012, McGraw-Hill.” function [x,f,ea,iter]=newtmult(func,x0,es,maxit,varargin) % newtmult: Newton-Raphson root zeroes nonlinear systems % [x,f,ea,iter]=newtmult(f,J,x0,es,maxit,p1,p2,...): % uses the Newton-Raphson method to find the roots of % a system of nonlinear equations % input: % f = the passed function % J = the passed jacobian % x0 = initial guess % es = desired percent relative error...

  • If necessary, create a new project named Intermediate24 Project and save it in the Cpp8\Chap14 folder....

    If necessary, create a new project named Intermediate24 Project and save it in the Cpp8\Chap14 folder. Also create a new source file named Intermediate24.cpp. If you are using Microsoft Visual C++, copy the Intermediate24.txt file from the Cpp8\Chap14 folder to the Cpp8\Chap14\Intermediate24 Project folder. Use a text editor to open the Intermediate24.txt file, which contains payroll codes and salaries. Close the Intermediate24.txt file. Create a program that allows the user to enter a payroll code. The program should search for...

  • mathlab please copy and paste the code and output Q1. (50 points) Consider the following mathematical...

    mathlab please copy and paste the code and output Q1. (50 points) Consider the following mathematical sequence for any mumber fathe sequence, the first number is 1 = 0. The remaining terms are computed via the following method. The second number of the sequence is given by 1 /2 f iseve Ty - 3x + 1 if, is add The third number of the sequence is given by 1 /2 fils even 133ry + 1 ifr, is odd Thus, the...

  • MATLAB, please provide code script Objective: Create a function file that animates projectile motion defined by...

    MATLAB, please provide code script Objective: Create a function file that animates projectile motion defined by the following equations in a subplot. Your function should accept user inputs of launch speed and launch angle. The output of your function will be a top subplot that displays height (y) as a function of x. The bottom subplot should display the vertical velocity while the projectile is in motion. A video of what your animation should look like is posted with this...

  • Create a file named “toneA.m” with the following MATLAB code: clear all Fs=5000; Ts=1/Fs; t=[0:Ts:0.4]; F_A=440;...

    Create a file named “toneA.m” with the following MATLAB code: clear all Fs=5000; Ts=1/Fs; t=[0:Ts:0.4]; F_A=440; %Frequency of note A is 440 Hz A=sin(2*pi*F_A*t); sound(A,Fs); Type toneA at the command line and then answer the following: (a) What is the time duration of A? (b) How many elements are there in A? (c) Modify toneA.m by changing “Fs=5000” to “Fs=10000”. Can you hear any difference? (d) Create a file named “tone.m” with the following MATLAB code: function x = tone(frequency,...

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
ADVERTISEMENT