We want to use our computers to solve scientific problems!
<img src="images/NGVS_nomask.jpg",width=500,height=250> Figure: Optical astronomical image
<img src="images/NGVS_mask.jpg",width=500,height=250> Figure: Optical image with automatically generated masks
A Programming language is a language that you need to actively speak to master it!
In [ ]:
import sys
"""
program to estimate the square root of a positive real number
"""
def my_sqrt(x, epsilon):
"""
input:
- x: postive float from which to estimate the square root
- eps: expetced accuracy of the estimated square root.
output:
- The estimated square root if the iteration converges to the
expected accuracy eps
- 'None' if the function does not converge or if an ivalid input
was given.
"""
if x < 0 or epsilon < 0:
# the following print foes to STDERR:
print("Both arguments to my_sqrt must be positive!", file=sys.stderr)
return None
a = 0. # start of interval
b = x # end of interval
sqrt_estimate = (a + b) / 2.
n_iteration = 0
while (b - a) > epsilon and n_iteration < 1000:
if (sqrt_estimate**2) < x:
a = (a + b) / 2.
else:
b = (a + b) / 2.
sqrt_estimate = (a + b) / 2.
n_iteration = n_iteration + 1
if n_iteration == 1000:
print("function my_sqrt did not converge after 1000 iterations",
file=sys.stderr)
return None
return sqrt_estimate
# estimate the square root of 2:
print(my_sqrt(2.0, 1.0e-06))
In [ ]: