# Homework 1¶

Student's Name: PLEASE INSERT YOUR NAME HERE (DOUBLE CLICK THIS BOX TO EDIT.)

Directions: Add work to this notebook to solve the problems below.

Problem Sources:

• LL: Programming for Computations - Python by Svein Linge and Hans Petter Langtangen, 2nd edition.
• L: A Primer on Scientific Programming with Python by Hans Petter Langtangen, 2nd edition.

These are standard imports:

In [ ]:
import numpy as np
import math as m
import matplotlib.pyplot as plt

1: Volume of a cube

Set the variable V to be the volume (in $cm^3$) of a cube whose side length is L=4 cm. (This is a modification of Exercises 1.2 from LL § 1.9.)

In [ ]:
L = 4 # Side length is 4 cm
In [ ]:

2: Circumference and area of a circle

Complete the following functions which compute the circumference and area of a circle given its radius. Replace the ??? with the correct expressions. Test your code to make sure it is working by running the code below. (This is a modification of Exercises 1.3 from LL § 1.9.)

In [ ]:
return ???

return ???
In [ ]:
circumference(1)
In [ ]:
circumference(0.5)
In [ ]:
area(1)
In [ ]:
area(2)

### 3: Volumes of three cubes¶

This is problem 1.4 of LL § 1.9:

In [ ]:

### 4: Printing¶

Read section 1.6.2 of of LL about printing in Python, and use what you have learned to complete problem 1.6 of LL:

In [ ]:

### 5: Compute the growth of money in a bank¶

Let $p$ be a bank’s interest rate in percent per year. An initial amount $A$ has then grown to $$A(1+\frac{p}{100})^n$$ after $n$ years. Write a function compute_new_amount that takes as input three variables A, p, and n as above and outputs the result of the formula above. Use it to compute how much money 1000 euros have grown to after three years with 5% interest rate.

(This is a modification of Exercise 1.5 of L.)

In [ ]:
def compute_new_amount(A, p, n):
return ???
In [ ]:
compute_new_amount(1000, 5, 3)

### 6: Find error(s) in a program.¶

Suppose somebody has written a simple one-line program for computing sin(1):

x=1; print 'sin(%g)=%g' % (x, sin(x))

Fix this program so that it runs correctly.

(This is a modification of Exercise 1.6 of L.)

In [ ]:
x=1; print('sin(%g)=%g' % (x, sin(x)))

### 7: Type in programs and debug them.¶

Attempt to execute these short programs. When they do not work, identify and correct the erroneous statements.

(This is exercise 1.8 from L.)

Does $sin^2(x) + cos^2(x) = 1$?

In [ ]:
from math import sin, cos
x = pi/4
1_val = sin^2(x) + cos^2(x)
print 1_VAL

Work with the expressions for movement with constant acceleration:

In [ ]:
v0 = 3 m/s
t = 1 s
a = 2 m/s**2
s = v0*t + 1/2 a*t**2
print(s)

Verify these equations: $$(a+b)^2 = a^2 + 2ab+b^2,$$ $$(a-b)^2 = a^2-2ab+b^2.$$

In [ ]:
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: %g = %g', % (eq1_sum, eq1_pow)
print 'Second equation: %h = %h', % (eq2_pow, eq2_pow)

### 8: Find errors in the coiding of a formula.¶

(This is a modification of problem 1.9 of L.)

The following is an attempt at an implementation of the quadratic formula, returning the pair of solutions to $a x^2+bx+c = 0$. Recall that the roots have the form $$x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}.$$

In [ ]:
from math import sqrt
def roots(a, b, c):
q = sqrt(b*b - 4*a*c)
root1 = (-b + q)/2*a
root2 = (-b - q)/2*a
return (root1, root2)

Note that $$(2 x-1)(x-2)=2 x^2 - 5x +2.$$ Thus roots(2, -5, 2) should return the pair (2, 0.5) (since the biggest root is returned first if a>0). But it currently doesn't work:

In [ ]:
roots(2, -5, 2)

Correct the roots function above so that it works correctly.