Appendix B — Homework: First Steps
Submit this homework via Dropbox. For due date please check the schedule posted on Blackboard.
- Python-scripts are submitted in one file called:
homework1.py
- This file already exists in your shared Dropbox folder. So simply open this file with a text editor (or Spyder) and start adding to this script file.
- Make sure your Dropbox is turned on -- you should see a Dropbox icon in your statusbar so that the file 'syncs-up' to the cloud so that I can see the changes.
- I will base your homework grade on my grading of 2 randomly selected homework exercises. So please make sure that you complete every homework 100% as you might otherwise end up with zero points.
B.1 Exercise 1:
Make two lists having the following numbers as content:
- \((1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1)\)
- \((1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5)\)
Define the two lists as listA
and listB
.
Print the 3. element of
listA
using list indexing.Print elements 5-8 of
listB
with a one line command using list slicing.Add the 3. element of
listA
to the 8. element oflistB
and print the sum of these two numbers. Use list indexing to "extract" the numbers from the list.
B.2 Exercise 2:
Compute the growth of money in a bank. Let \(r\) be a bank's interest rate in percent per year. An initial amount \(A\) will then grow to:
\(A(1+\frac{r}{100})^n\),
after \(n\) years.
Now, make a program (or script) for computing how much money you have if you take 1,000 dollars and invest it for three years according to the formula above. Assume that your interest rate is \(5\) percent.
B.3 Exercise 3:
The following program calculates the
area of a parallelogram
\[Area = base \times height\]
area of a square
\[Area = base^2\]
area of a circle
\[Area = \pi \times radius^2\]
volume of a cone
\[Volume = \frac{(\pi \times r^2 \times h)}{3}\]
Type the following program in your Spyder editor and execute it. It won't run. Debug it and make sure it all runs through correctly. Try to update it to the newest Python syntax (e.g., .format()
function, ...):
import numpy as np
h = 5.0 # height
b = 2.0 # base
r = 1.5 # radius
#
area_parallelogram = h*b
print "The area of the parallelogram is %.3f" % area_parallelogram
#
area_square = b**2
print "The area of the square is %g" % area_square
#
area_circle = pi*r**2
print "The area of the circle is %.3f" % area_circle
#
volume_cone = 1.0/3*npp.pi*r**2*h
print 'The volume of the cone is %.3f' % volume_cone
B.4 Exercise 4:
Type these short programs in your editor and execute them. When they do not work, identify and correct the erroneous statements.
- The following code checks whether \(sin(x)^2 + cos(x)^2 = 1\). Make sure it runs through and make sure the code verifies that the left hand side value equal the right hand side value of the equation above.
Code:
from math import sin, cos
x = pi/4
1_val = sin(x)**2 + cos2(x)**2
print 1_VAL
- Second, fix the following script that implements the formulas below. Hint: Some variables are used before they are defined. Make up some values for undefined variables
m
,s
etc. and add them to your script and make sure it all runs through without any more errors. The respective formulas are:
\[\begin{aligned} \begin{eqnarray*} v_0 = 3 \left(\frac{m}{s}\right), \\ t = 1 \times s, \\ a = 2 \left(\frac{m}{s}\right)^2, \\ k = v_0\times t + \left(\frac{1}{2}\right)a\times t^2. \\ \end{eqnarray*} \end{aligned}\]
Code:
v0 = 3 m/s
t = 1 s
a = 2 m/s**2
k = v0*t + 1/2 a*t**2
print k
- The next code chunk verifies these equations:
\[\begin{aligned} \begin{eqnarray*} (a + b)^2 &=& a^2 + 2ab + b^2 \\ (a - b)^2 &=& a^2 - 2ab + b^2 \end{eqnarray*} \end{aligned}\]
Code:
a = 3,3 b = 5,3
a2 = a**2
b2 = b**2
eq1_sum = a2 + 2ab + b2
eq2_sum = a2 - 2ab + b2
eq1_pow = (a + b)**2
eq2_pow = (a - b)**2
print 'First equation: %h = %' % (eq2_sum, eq2_sum)
print 'Second equation: %h = %' % (eq2_pow, eq2_pow)
B.5 Exercise 5:
Find errors in the coding of a formula. Given a quadratic equation, \(ax^2 + bx + c = 0\), the two roots (i.e., the x values that would make the left hand side equal to zero) are: \(x_1 = \frac{-b + \sqrt{b^2 - 4ac}}{2a}, x_2 = \frac{-b - \sqrt{b^2 - 4ac}}{2a}\)
What are the problems with the following program:
a = 2; b = 10; c = 2
from math import sqrt
q = sqrt(b*b - 4*a*c)
x1 = (-b + q)/2*a
x2 = (-b - q)/2*a
print x1, x2
Hint: Compute all terms in the math expression above with the aid of a calculator (you can use the command line in Spyder to do that, no need to find a pocket calculator), and compare with the corresponding intermediate results computed in the program. You need to add some print
statements to see the result of q
, -b+q
, and 2*a
, etc.