Appendix F — Homework: Functions

Submit this homework via Dropbox. For the deadline please consult the schedule posted on Blackboard.

  • Python-scripts are submitted in one file called: homework5.py

F.1 Exercise 1

The formula for converting Fahrenheit degrees to Celsius reads: \(C = \frac{5}{9}(F - 32)\)

Write a function called myfuncCtoF that implements this formula and converts the following temperatures from F to C: \({60, 70, 75, 80, 85, 90, 95, 100, 105}\).

Use the def keyword to define your function myfuncCtoF. Then call the function to make the conversion. You have at least two options here:

  1. Either define the function so you hand in a number and it spits a number back and then call the function from a loop.
  2. Or, you define the function so that it accepts a vector as input. Then you write a loop inside the function that converts all the numbers from the vector and spits back a results vector.

Then pretty print the results i.e. something like this:

60  Fahrenheit equal .. Celsius
70  Fahrenheit equal .. Celsius
...
105 Fahrenheit equal .. Celsius

F.2 Exercise 2

The factorial of \(n\), written as \(n!\), is defined as \(n! = n \times (n - 1)(n - 2) ... \times 1\) with the special cases \(1! = 1\), \(0! = 1\).

For example, \(4! = 4 \times 3 \times 2 \times 1 = 24\), and \(2! = 2 \times 1 = 2\). Write a function myfact(n) that returns \(n!\). Return 1 immediately if x is 1 or 0, otherwise use a loop (inside the function definition) to compute \(n!\).

You are not allowed to use the built in np.fact() function - but you can use this built in function to check the correctness of your "hand written" function.

import math as m
import numpy as np
print(m.factorial(4))
print(np.math.factorial(4))
24
24

F.3 Exercise 3

The (Euclidean) length of a vector \(v = (a_0, a_1,\dots, a_k)\) is the square root of the sum of squares of its coordinates \(\sqrt{a_0^2 + a_1^2,\dots, a_k^2}\).

Write a function myDistance that returns the length of a vector calculated according to the formula above. This requires a loop inside your function definition.

Then test your function and calculate the length of vector \(a = [3, 5, 23, 45, 12]\) and vector \(b = [6, -5, 20]\).

F.4 Exercise 4

This should look familiar. Plot the following composite function. First define a function f(x) as below and call it myFunction1 and then evaluate myFunction1 on a grid of x values from \([-3, 5]\). Store the function values in a vector yv. Then plot this function.

\[\begin{aligned} f(x) = \begin{cases} x^2 & x < -1 \\ |x| & -1 \leq x < 0 \\ -1 & 0 \leq x < 0.5 \\ x^2 & 0.5 \leq x < 2 \\ \sqrt{x} & 2 \leq x \end{cases} \end{aligned}\]

F.5 Exercise 5 (more difficult)

Some object is moving along a path in the X-Y plane. Maybe think of a chessboard, where the X values are the rows and the Y values are the columns.

At \(n\) points of time we have recorded the corresponding (x, y) positions of the object that moves on our "chessboard". Here are the coordinates for the n different positions, indexed as \(0,1, ..., n-1\): \((x_0 , y_0), (x_1, y_2), ..., (x_{n-1}, y_{n-1})\).

The total length \(L\) of the path from \((x_0, y_0)\) to \((x_{n-1}, y_{n-1})\) is the sum of all the individual line segments \((x_{i-1}, y_{i-1})\) to \((x_i, y_i)\), for \(i = 1,..., n-1\):

\(L = \sum_{i=1}^{n-1} \sqrt{(x_i - x_{i-1})^2 + (y_i - y_{i-1})^2}\)

Make a function with the name pathlength(x,y) for computing this length \(L\) according to the formula above.

The arguments \(x\) and \(y\) hold all the \(x_0,...,x_{n-1}\) and \(y_0,...,y_{n-1}\) coordinates, respectively. Test the function on a triangular path with the four points (1,1), (2,1), (1,2), and (1,1).

Hint: As the object moves from (1,1) to (2,1) calculate the length of that path and store it in an "accumulator variable". Then calculate the length of the path from (2,1) to (1,2) and add that to the stored value, etc. Put this into a loop so that if more points are added the function can calculate the total length traveled.