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.
Compute $\sum_{n=1}^{100} \frac{1}{n^2+1}$
(answer $\approx 1.06672420915241$)
sum = 0 for n in range(1, 101): sum = sum + (1/(n**2 + 1)) sum
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
(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
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
(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
print("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
print("you paid $10,647.66")
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)
(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)
(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)
(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 <x_2 < \cdots < x_n=b$ subdivides $[a,b]$ into $n$ congruent subintervals of equal length
$\Delta x = x_{i}-x_{i-1}, \quad i=1,\ldots, n$
then
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))
(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]$ into $n$ congruent subintervals of equal length
$\Delta x = x_{i}-x_{i-1}, \quad i=0,\ldots,n$
then
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))