[13]
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.
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
[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)
[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.
[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]
[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]
[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]
[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]
[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]
[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]
[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.
[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
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
[2]
[1]
[1]
[1]
[2]
[2]
[4]
[1]
[2]
[1]
[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]
[1]
[2]
[1]
[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])
[1]
[2]
[2]
[4]
[1]
The different versions of the question answers shown above are not the only solutions available, many others exist.
#!/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)