Goals of the Course

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

What do you need to learn?

  • A (general purpose) programming language, its elements (data structures) and syntax. Most languages are very similar in their building blocks (see below). All general purpose languages can solve the same class of problems.
  • For concrete problems you need specific algorithms (simulations, differential equations, numerical mathematics)
  • You need algorithmic thinking - how can I decompose a given problem to solve it with available elements of my programming language.
  • You need the ability to test the correctness of your program and a very good feeling for potential problems!

A Programming language is a language that you need to actively speak to master it!

Elements of a programming language

  • Input and output (Interaction with the user)
  • Manipulation of memory and variables
  • elementary mathematics
  • conditional program execution
  • loops (repetition of code parts with variable parameters)
  • functions
  • comments

  • (Libraries for your applications)


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 [ ]: