 CoCalc Public Filesassignments / Assignment 03: Python Loops Drill / Python Loops Drill.ipynb
Author: Enrique Guzman
Views : 117

# Python Loops Drill

Instructions: In Python use one or more loops to solve the following exercises. Do not rely on a fancy formula that you find on the intenet -- that would defeat the purpose of the drill. The answers to some of the problems are given below so you can check them against the output of your loop.

Click Kernel above and make sure your Kernel is Python 3(Anaconda 5). You can also use the Kernel menu to restart the kernel if the program jams up. This sometimes occurs if you ask it to execute an incomplete command that never finishes, or an infinite loop.

#### 1.

Compute $\sum_{n=1}^{100} \frac{1}{n^2+1}$

(answer $\approx 1.06672420915241$)

In :
sum = 0
for n in range(1, 101):
sum = sum + (1/(n**2 + 1))
sum

1.0667242091524087

#### 2.

You deposit $65 on the first business day of each month into a bank account earning 0.24% interest per month. Interest is paid in the on the last business day of each month. Your first deposit is made on Wednesday, Jan. 2, 2019, you make no other deposits or withdrawals except for the deposits mentioned above, and the interest rate never changes. How much will there be in your account (a) after the interest is paid on Thursday, Dec. 31, 2026? (answer =$7024.80)

In :
acc = 0
for year in range(1,9):
for months in range(1,13):
acc = acc + 65
acc = acc + (acc*0.0024)

acc


7024.7951617909985

(b) after the interest is paid on Wednesday, July 31, 2030?

(answer = $10,735.18) In : acc = 0 for year in range(1,12): for months in range(1,13): acc = acc + 65 acc = acc + acc*0.0024 for months in range(1,8): acc = acc + 65 acc = acc + (acc*0.0024) acc  10735.17861550018 #### 3. On Wed. Jan. 2, 2019 you buy a used car. You agree to pay$9000. You pay a $2000 down payment and finance the rest. Your monthly monthly payments are$150, due on the first business day of each month. Your first monthly payment is due Friday, Feb. 1, 2019. Your last monthly payment will be the amount that you still owe on the car if that amount is less than $150. The finance company charges at 9% annual percentage rate (APR). Assume this means on the last day of each month they charge 1/12 of 9% interest, and the first interest charge occurs on Jan. 30, 2019. (a) How much will you still owe on the car on Friday, Dec. 31, 2021, after the interest is charged? (answer$3137.61)

In :
acc = 7000
for months in range(1,36):
acc = acc + (acc*0.0075)
acc = acc - 150

acc = acc + (acc*0.0075)
acc

3137.6101780850618

(b) From the day you buy the car how many months will it take to pay off the car?

In :
acc = 7000
for months in range(1,59):
acc = acc + (acc*0.0075)
acc = acc - 150

acc = acc + (acc*0.0075)
acc

-52.33583150406438
In :
print("you pay it off in 58 months")

you pay it off in 58 months

(c) If you add up your down payment and all your monthly payments how much will you have paid for the car?

In :
58*150+2000-52.34

10647.66
In :
print("you paid $10,647.66")  you paid$10,647.66

#### 4.

A credit union Visa card charges 11.99% annual percentage rate for customers who are in good standing. Assume this means they charge 1/12 of that rate each month. To be in good standing customers who owe money on their cards are required to pay at least a minimum payment each month. According to the credit union the required minimum payment is

• 3% of your total balance but not less than $25 • except if your total balance is less than$25 then you don't pay $25, you just pay off the total balance. Suppose a customer owes$2500 on his Visa card on Jan. 2, 2019. On the first business day of each month he pays the the minimum payment, and makes no other payments or charges. Interest is charged on the last business day of each month.

(a) How much will the customer owe on his card on Dec, 31, 2021, after the interest for that month is charged?

(answer = $1194.43) In : owed = 2500 month = 0 paid = 0 min_p = 0 while owed>0: month = month + 1 if owed>25: min_p = owed*0.03 if min_p<25: min_p = 25 else: min_p = owed owed= owed-min_p interest = owed*0.1199/12 owed = owed + interest if month ==36: print (owed)  1194.4360415249637 (b) If he keeps making the minimum payment until the balance on the card is paid off, when will he make his last payment? (hint: first figure out how many months he makes minimum payments that are over$25. Then figure out how many more months it takes to pay off the card. Be sure to include the last payment.)

In :
owed = 2500
month = 0
paid = 0
min_p = 0
while owed>0:
month = month+1
if owed>25:
min_p = owed*0.03
if min_p<25:
min_p = 25
else:
min_p = owed
owed= owed-min_p
interest = owed*0.1199/12
owed = owed + interest
print("Total months taken to repay: ", month)
years = (int)(month/12)


Total months taken to repay: 94

(c) After he pays off this debt he adds up all the payments that he has made to find out how much it cost to pay off this debt. How much will it have cost to pay off this $2500 debt making minimum payments? (hint: nearly$3500.)

In :
owed = 2500
month = 0
paid = 0
min_p = 0
while owed>0:
month = month+1
if owed>25:
min_p = owed*0.03
if min_p<25:
min_p = 25
else:
min_p = owed
owed= owed-min_p
interest = owed*0.1199/12
owed = owed + interest
years = (int)(month/12)

m = month%12

print("Sum off all payments", month*min_p)

Sum off all payments 1852.5194176436678

#### 5.

(For help with this problem see the section on "Approximate Integration" in your Calculus book -- it's section 6.5 in Stewart's Essential Calculus, Second edition.)

Approximate the integral $\int_1^7 \sqrt{x^3+2x+1} \; dx$ by subdividing the interval into $n=26$ subintervals using

(a) The trapezoid rule:, (a pretty-good approximation formula). If $a=x_0 < x_1 subdivides $[a,b]$ into $n$ congruent subintervals of equal length

$\Delta x = x_{i}-x_{i-1}, \quad i=1,\ldots, n$

then

In :
def Trapezoidal(f, a, b, n):
dx = (b-a)/float(n)

x = 0.5*(f(a) + f(b))

for i in range(1,n,1):
x = x + f(a + i*dx)
return dx*x

import math as m
def f(x):
return m.sqrt(x**3 + 2*x + 1)

a = 1;  b = 7
n = 26
I = Trapezoidal(f, a, b, n)
print("The asnswer is, I = ")
print (round(I, 3))

The asnswer is, I = 55.023

(b) Simpson's rule: (generally this is more accurate than the trapezoid rule). If $n$ is even and $a=x_0 < x_1 subdivides $[a,b]$ into $n$ congruent subintervals of equal length

$\Delta x = x_{i}-x_{i-1}, \quad i=0,\ldots,n$

then

In :
import math

def func( x ):
return math.sqrt(x**3 + 2*x + 1)

def simpsons_( ll, ul, n ):

h = ( ul - ll )/n

x = list()
fx = list()

i = 0
while i<= n:
x.append(ll + i * h)
fx.append(func(x[i]))
i += 1

res = 0
i = 0
while i<= n:
if i == 0 or i == n:
res+= fx[i]
elif i % 2 != 0:
res+= 4 * fx[i]
else:
res+= 2 * fx[i]
i+= 1
res = res * (h / 3)
return res

lower_limit = 1  # Lower limit
upper_limit = 7 # Upper limit
n = 6 # Number of interval
print("%.6f"% simpsons_(lower_limit, upper_limit, n))

55.011934
In [ ]: