Python Exercise 1

Fredrik Eikeland Fossan


Jan 13, 2016


Intro to Python

The first python exercise involves some simple tasks in order to get familiar with Python.

Problem 1: Simple tasks

Perform the following simple tasks.

a) Write a Python program that writes "Hello, World!" to the screen.

b) In this subexercise some simple mathematical operations are to be carried out. Define the vectors \( a \) and \( b \) and the matrices \( A \) and \( B \) as numpy arrays as follows, $$ \begin{align*} a&=\left[ \begin{matrix} 1 \\ 2 \\ 3 \end{matrix}\right] & b&=\left[ \begin{matrix} 4 \\ 5 \\ 6 \end{matrix}\right] & A&=\left[ \begin{matrix} 1 & 1 & 2 \\ 2 & 3 & 3 \\ 4 & 4 & 5 \end{matrix}\right] & B&=\left[ \begin{matrix} 2 & 4 & 6 \\ 8 & 10 & 12 \\ 14 & 16 & 18 \end{matrix}\right] \end{align*} $$ Carry out the following calculations and print the results to the screen:

Hint.

For the latter two calculations some functions in numpy.linalg might be useful. See http://docs.scipy.org/doc/numpy/reference/routines.linalg.html.

c) Implement a function fib(n) that writes the first n elements in the Fibonacci series to the screen. Write the first 20 elements to the screen.

d) Plot \( sin(x) \) from \( x=0 \) to \( x=2\pi \) with 10 and 100 points. Make the figure presentable by naming the axes, set different colors to the lines, including legend etc. Save the figure as a file.

e) Rewrite the function fib(n) such that the sum of the series is stored in a variable (numpy array). Plot the first 30 first values in the variable with logarithmic y-axis. Make the plot presentable.