Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 1 Encapsulate the following Python code from Section 7.5 in a function namedmy_sqrtthat takes a as a parameter, chooses a starting value forx, and

Part 1

Encapsulate the following Python code from Section 7.5 in a function namedmy_sqrtthat takes a as a parameter, chooses a starting value forx, and returns an estimate of the square root ofa.

while True:

y = (x + a/x) / 2.0

if y == x:

break

x = y

Part 2

Write a function namedtest_sqrtthat prints a table like the following using awhileloop, where "diff" is the absolute value of the difference betweenmy_sqrt(a)andmath.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 foravalues 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.

Your submission will be assessed using the following Aspects.

  1. Does the submission include amy_sqrtfunction that takes a single argument and includes thewhileloop from the instructions?
  2. Does themy_sqrtfunction initializexand return its final value?
  3. Does thetest_sqrtfunction printavaluesfrom 1 to 25?
  4. Does thetest_sqrtfunction print the values returned bymy_sqrtfor each value ofa?
  5. Does thetest_sqrtfunction print correct values frommath.sqrtfor each value ofa?
  6. Does thetest_sqrtfunction print the absolute value of the differences betweenmy_sqrtandmath.sqrtfor each value ofa?
  7. Does themy_sqrtfunction compute values that are almost identical tomath.sqrt("diff" less than 1e-14)?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions