You are on page 1of 15

Question 1

[13]

The multiple choice answers are as follows:

1.1 D
1.2 B
1.3 C
1.4 B

Please see the LibreOffice document for the detailed memo for each multiple choice question.

Bladsy 1 van 15 / Page 1 of 15

Question 2

[10]

#!/usr/bin/env python
# -*- coding: utf-8 -*"""
Created on Thu Apr 25 21:18:45 2013
@author: Nico Wilke
"""
from matplotlib import pyplot as plt
import numpy as np

# ---------- version 1 ---------# generate a range of x values for each function


x1 = np.linspace(0, 10, 11)
x2 = np.arange(0, 10.2, 0.2)
# generate the range of function values for the x values
fx = x1 + x1**2 + x1**3
gx = np.cos(x2) + np.sin(x2)**2

[2]

[1]

plt.figure(1)
# used subplots for each graph
plt.subplot(1, 2, 1)
# plotted x and f(x)
# added graph markers s and *
plt.plot(x1, fx, ks-)
# added graph title, xlabel and ylabel
plt.title(rGraph of $x + x^2 + x^3$, fontsize=16)
plt.xlabel(r$x$, fontsize=16)
plt.ylabel(r$f(x)$, fontsize=16)

Bladsy 2 van 15 / Page 2 of 15

[1]
[1]
[1]
[2]

plt.subplot(1, 2, 2)
# plotted x and g(x)
plt.plot(x2, gx, k*-)
plt.title(rGraph of $cos(x) + sin(x)^2$, fontsize=16)
plt.xlabel(r$x$, fontsize=16)
plt.ylabel(r$g(x)$, fontsize=16)
plt.show()

The different versions of the question answers shown above are not the only solutions available, many others exist.

Bladsy 3 van 15 / Page 3 of 15

[1]

Question 3

[12]

#!/usr/bin/env python
# -*- coding: utf-8 -*"""
Created on Fri Mar 15 09:22:17 2013
@author: Logan Page
"""
# ---------- version 1 ---------# function definition with inputs
def trajectory_v1(mass, delta_t, vel0_x, vel0_y):
"""
Function to calculate the trajectory of a cannon ball.

[3]

Inputs:
mass: mass of the cannon ball
delta_t: time step for updating the cannon balls trajectory
vel0_x: initial cannon ball x velocity
vel0_y: initial cannon ball y velocity
Returns:
store_x: list of x coordinates along the cannon ball trajectory
store_y: list of y coordinates along the cannon ball trajectory
"""
# initialize starting values
x_val = 0
y_val = 0
vel_x = vel0_x
vel_y = vel0_y
# initialize coordinate lists
store_x = [x_val]
store_y = [y_val]

Bladsy 4 van 15 / Page 4 of 15

[1]

[1]

# use of a loop
# correct condition in the while loop
while y_val >= 0:
# update the next x and y coordinate in the sequence
x_val = x_val + delta_t * vel_x
y_val = y_val + delta_t * vel_y

[1]
[1]
[2]

vel_x = vel_x - delta_t * (2 * vel_x / mass)


vel_y = vel_y - delta_t * (9.81 + (2 * vel_y / mass))
# append the updated x and y coordinates to their lists
store_x = store_x + [x_val]
store_y = store_y + [y_val]
# correctly return both lists
return store_x, store_y

# ---------- version 2 ---------# function definition with inputs


def trajectory_v2(mass, delta_t, vel_x, vel_y):
"""
Function to calculate the trajectory of a cannon ball.

[2]

[1]

[3]

Inputs:
mass: mass of the cannon ball
delta_t: time step for updating the cannon balls trajectory
vel0_x: initial cannon ball x velocity
vel0_y: initial cannon ball y velocity
Returns:
store_x:
store_y:
"""
# initialize
# initialize
x_vals = [0]
y_vals = [0]

list of x coordinates along the cannon ball trajectory


list of y coordinates along the cannon ball trajectory
starting values
coordinate lists

Bladsy 5 van 15 / Page 5 of 15

[1]
[1]

# use of a loop
# correct condition in the while loop
while y_vals[-1] >= 0:
# update the next x and y coordinate in the sequence
# append the updated x and y coordinates to their lists
x_vals.append(x_vals[-1] + delta_t * vel_x)
y_vals.append(y_vals[-1] + delta_t * vel_y)

[1]
[1]
[2]
[2]

vel_x = vel_x - delta_t * (2 * vel_x / mass)


vel_y = vel_y - delta_t * (9.81 + (2 * vel_y / mass))
# correctly return both lists
return x_vals, y_vals

# ---------- version 3 ---------# function definition with inputs


def trajectory_v3(mass, delta_t, vel_x, vel_y):
"""
Function to calculate the trajectory of a cannon ball.

[1]

[3]

Inputs:
mass: mass of the cannon ball
delta_t: time step for updating the cannon balls trajectory
vel0_x: initial cannon ball x velocity
vel0_y: initial cannon ball y velocity
Returns:
store_x:
store_y:
"""
# initialize
# initialize
x_vals = [0]
y_vals = [0]

list of x coordinates along the cannon ball trajectory


list of y coordinates along the cannon ball trajectory
starting values
coordinate lists

Bladsy 6 van 15 / Page 6 of 15

[1]
[1]

cnt = 0
# use of a loop
# correct condition in the while loop
while y_vals[cnt] >= 0:
# update the next x and y coordinate in the sequence
new_x = x_vals[cnt] + delta_t * vel_x
new_y = y_vals[cnt] + delta_t * vel_y

[1]
[1]
[2]

vel_x = vel_x - delta_t * (2 * vel_x / mass)


vel_y = vel_y - delta_t * (9.81 + (2 * vel_y / mass))
# append the updated x and y coordinates to their lists
x_vals = x_vals + [new_x]
y_vals = y_vals + [new_y]

[2]

cnt = cnt + 1
# correctly return both lists
return x_vals, y_vals

The different versions of the question answers shown above are not the only solutions available, many others exist.

Bladsy 7 van 15 / Page 7 of 15

[1]

#!/usr/bin/env python
# -*- coding: utf-8 -*"""
Created on Fri Mar 15 09:22:17 2013
@author: Logan Page
YOU CAN TEST QUESION 3 WITH THE FOLLOWING SCRIPT
ASSUMING OF COURSE THAT THE TRAJECTORY FUNCTION IS SAVED IN "question3.py"
(PLOTTING INCLUDED FOR MORE INFORMATION)
"""
import matplotlib.pyplot as plt
import question3 as q3

x_coords, y_coords = q3.trajectory_v1(10, 0.2, 30, 30)


plt.figure(1)
plt.plot(x_coords, y_coords, g-)
plt.title("Cannon ball trajectory", fontsize=16)
plt.xlabel("Distance [m]", fontsize=12)
plt.ylabel("Height [m]", fontsize=12)
plt.show()

Bladsy 8 van 15 / Page 8 of 15

Question 4

[15]

#!/usr/bin/env python
# -*- coding: utf-8 -*"""
Created on Fri Mar 15 09:25:24 2013
@author: Logan Page
"""
import numpy as np

# ---------- version 1 ---------# function definition with inputs


[2]
def matrix_type_v1(mat_h):
"""
function to determine the matrix type (balanced / unbalanced) based on the
sum of the absolute values of the terms above and below the diagonal of
the matix
Input:
mat_h: numpy matrix
Return:
1 if the matrix is balance
0 if the matrix is unbalanced
"""
# get the number of rows and cols of the matrix
rows, cols = np.shape(mat_h)
# initialize the summing names
sum_upper = 0
sum_lower = 0

Bladsy 9 van 15 / Page 9 of 15

[2]

[1]

# loop through the rows of the matrix


for i in range(rows):
# loop through the cols of the matrix
for j in range(cols):
# sum the terms above the diagonal
if i > j:
sum_upper = sum_upper + abs(mat_h[i][j])
# sum the terms below the diagonal
if i < j:
sum_lower = sum_lower + abs(mat_h[i][j])
# test if the matrix is balanced or unbalanced
if (sum_upper % 2) == (sum_lower % 2):
# return the correct value
return 1

[1]
[1]
[2]

[2]

[4]
[1]

if (sum_upper % 2) != (sum_lower % 2):


return 0

# ---------- version 2 ---------# function definition with inputs


[2]
def matrix_type_v2(mat_h):
"""
function to determine the matrix type (balanced / unbalanced) based on the
sum of the absolute values of the terms above and below the diagonal of
the matix
Input:
mat_h: numpy matrix
Return:
1 if the matrix is balance
0 if the matrix is unbalanced
"""

Bladsy 10 van 15 / Page 10 of 15

# get the number of rows and cols of the matrix


rows, cols = np.shape(mat_h)

[2]

# initialize the summing names


sum_upper = 0
sum_lower = 0

[1]

# loop through the rows of the matrix


for i in range(rows):
# loop through the cols of the matrix
for j in range(i+1, cols):
# sum the terms above the diagonal
sum_upper = sum_upper + abs(mat_h[i][j])

[1]

for i in range(rows):
for j in range(i):
# sum the terms below the diagonal
sum_lower = sum_lower + abs(mat_h[i][j])
# test if the matrix is balanced or unbalanced
if (sum_upper % 2 == 1) and (sum_lower % 2 == 1):
bal = 1

[1]
[2]

[2]

[4]

if (sum_upper % 2 == 0) and (sum_lower % 2 == 0):


bal = 1
if (sum_upper % 2 == 1) and (sum_lower % 2 == 0):
bal = 0
if (sum_upper % 2 == 0) and (sum_lower % 2 == 1):
bal = 0
# return the correct value
return bal

Bladsy 11 van 15 / Page 11 of 15

[1]

# ---------- version 3 ---------# function definition with inputs


[2]
def matrix_type_v3(mat_h):
"""
function to determine the matrix type (balanced / unbalanced) based on the
sum of the absolute values of the terms above and below the diagonal of
the matix
Input:
mat_h: numpy matrix
Return:
1 if the matrix is balance
0 if the matrix is unbalanced
"""
# get the number of rows and cols of the matrix
rows, cols = np.shape(mat_h)

[2]

# initialize the summing names


sum_upper = 0
sum_lower = 0

[1]

# loop through the rows of the matrix


for i in range(cols):
# loop through the cols of the matrix
for j in range(i+1, rows):
# sum the terms above the diagonal
sum_upper = sum_upper + abs(mat_h[j][i])

[1]

for i in range(cols):
for j in range(i):
# sum the terms below the diagonal
sum_lower = sum_lower + abs(mat_h[j][i])

Bladsy 12 van 15 / Page 12 of 15

[1]
[2]

[2]

# test if the matrix is balanced or unbalanced


if (sum_upper % 2 == 1) and (sum_lower % 2 == 1):
bal = 1

[4]

if (sum_upper % 2 == 0) and (sum_lower % 2 == 0):


bal = 1
if (sum_upper % 2 == 1) and (sum_lower % 2 == 0):
bal = 0
if (sum_upper % 2 == 0) and (sum_lower % 2 == 1):
bal = 0
# return the correct value
return bal

[1]

# ---------- Why tril and triu were excluded ---------def matrix_type_bi(mat_h):


"""
function to determine the matrix type (balanced / unbalanced) based on the
sum of the absolute values of the terms above and below the diagonal of
the matix
Input:
mat_h: numpy matrix
Return:
1 if the matrix is balance
0 if the matrix is unbalanced
"""
upper_mat = np.triu(mat_h)
lower_mat = np.tril(mat_h)
sum_upper = np.sum(upper_mat)
sum_lower = np.sum(lower_mat)

Bladsy 13 van 15 / Page 13 of 15

if (sum_upper % 2) == (sum_lower % 2):


return 1
if (sum_upper % 2) != (sum_lower % 2):
return 0

The different versions of the question answers shown above are not the only solutions available, many others exist.

Bladsy 14 van 15 / Page 14 of 15

#!/usr/bin/env python
# -*- coding: utf-8 -*"""
Created on Fri Mar 15 09:22:17 2013
@author: Logan Page
YOU CAN TEST QUESION 4 WITH THE FOLLOWING SCRIPT
ASSUMING OF COURSE THAT THE MATRIX_TYPE FUNCTION IS SAVED IN "question4.py"
"""
import numpy as np
import question4 as q4

my_mat = np.array([[2,
[1,
[1,
[1,
print
print
print
print

1,
2,
1,
1,

1,
1,
2,
1,

1],
1],
1],
2]], dtype=int)

q4.matrix_type_v1(my_mat)
q4.matrix_type_v2(my_mat)
q4.matrix_type_v3(my_mat)
q4.matrix_type_bi(my_mat)

Bladsy 15 van 15 / Page 15 of 15

You might also like