Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 366
Kernel: Python 3 (Anaconda 5)

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 n=11001n2+1 \sum_{n=1}^{100} \frac{1}{n^2+1}

(answer 1.06672420915241\approx 1.06672420915241)

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)

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)

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)

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?

acc = 7000 for months in range(1,59): acc = acc + (acc*0.0075) acc = acc - 150 acc = acc + (acc*0.0075) acc
-52.33583150406438
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?

58*150+2000-52.34
10647.66
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)

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.)

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.)

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 17x3+2x+1  dx\int_1^7 \sqrt{x^3+2x+1} \; dx by subdividing the interval into n=26n=26 subintervals using

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

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

then abf(x)  dxΔx2(f(x0)+2f(x1)+2f(x2)+2f(x3)++2f(xn1)+f(xn))=Δx2(f(x0)+2[i=1n1f(xi)]+f(xn))\begin{align*} \int_a^b f(x)\;dx &\approx \frac{\Delta x}{2} \left( f(x_0)+2f(x_1)+2f(x_2)+2f(x_3)+\cdots + 2f(x_{n-1})+f(x_n)\right) \\ &= \frac{\Delta x}{2}\left(f(x_0)+2 \Big[ \sum_{i=1}^{n-1} f(x_i) \Big] + f(x_n)\right) \end{align*}

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 <x_2 < \cdots < x_n=b ) subdivides [a,b][a,b] into nn congruent subintervals of equal length

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

then abf(x)  dxΔx3(f(x0)+4f(x1)+2f(x2)+4f(x3)+2f(x4)++2f(xn4)+4f(xn3)+2f(xn2)+4f(xn1)+f(xn))\begin{align*} \int_a^b f(x)\; dx \approx \frac{\Delta x}{3} & \Big( f(x_0)+4f(x_1)+2f(x_2)+4f(x_3)+2f(x_4)+\cdots \\ &\cdots+2f(x_{n-4})+4f(x_{n-3})+ 2f(x_{n-2})+4f(x_{n-1})+f(x_n)\Big) \end{align*}

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