Instructions
Requirements and Specifications
Source Code
QUESTION 1
from sympy import *
from sympy.abc import x
from sympy import Symbol
import numpy as np
from sympy.plotting import plot
import matplotlib.pyplot as plt
def a(n):
return (x ** (3 * n + 1)) / (3 * n + 1)
def PartA():
print("*** PART A ****")
# Calculate the ratio test
n = symbols('n', integer = True, positive = True)
# Print simplified version of a(n+1)/a(n)
simplified = simplify(a(n+1)/a(n))
print("Simplified: ")
print(simplified)
print()
# Now calculate the limit
print("Limit: ")
print(limit(simplified, n, float('inf')))
print()
def PartB():
print("*** PART B ****")
# Calculate the ratio test
n = symbols('n', integer=True, positive=True)
# Print simplified version of a(n+1)/a(n)
simplified = simplify(a(n + 1) / a(n))
# Now calculate the limit
lim = limit(simplified, n, float('inf'))
# Calculate the intervals for which x**3 < 1
interval = reduce_inequalities(lim<1)
print("Interval of convergence:")
print(interval)
print()
""" Each endpoint is 1 value far from x = 0 (-1, 1)"""
def PartC():
print("*** PART C ****")
# Define the function of convergence
eq = 1/3 *log(x+1) - 1/6 *log(x**2-x+1) + 1/sqrt(3) *atan((2*x-1)/sqrt(3)) + pi*sqrt(3)/18
# Interval of convergence is [-1,1)
# Define x values
xvals = np.linspace(-1, 1-0.01, 100)
yvals = lambdify(x,eq)(xvals)
# Now plot
plt.plot(xvals, yvals, label = 'f(x)')
# Compute sq
s1 = a(0) + a(1)
s3 = a(0) + a(1) + a(2) + a(3)
s5 = a(0) + a(1) + a(2) + a(3) + a(4) + a(5)
y_s1 = lambdify(x, s1)(xvals)
y_s3 = lambdify(x, s3)(xvals)
y_s5 = lambdify(x, s5)(xvals)
plt.plot(xvals, y_s1, label = 's1')
plt.plot(xvals, y_s3, label='s3')
plt.plot(xvals, y_s5, label='s5')
plt.grid(True)
plt.legend()
plt.show()
print()
def PartD():
print("*** PART D ****")
# Define t
t = symbols('t')
#Define n
n = symbols('n', integer=True, positive=True)
integral1 = integrate(1/(1+t**3), (t, 0, x))
integral2 = integrate((-1)**n *t**(3*n), (t, 0, x))
print(integral1)
print()
print(integral2)
xvals = np.linspace(-1, 1 - 0.01, 100)
yvals1 = lambdify(x, integral1)(xvals)
yvals2 = lambdify(x, integral2)(xvals)
"""
From Part C), we found that the series converges to the following function:
1/3 *log(x+1) - 1/6 *log(x**2-x+1) + 1/sqrt(3) *atan((2*x-1)/sqrt(3)) + pi*sqrt(3)/18
And we see that the value of the first integral is
log(x + 1)/3 - log(x**2 - x + 1)/6 + sqrt(3)*atan(2*sqrt(3)*x/3 - sqrt(3)/3)/3 + sqrt(3)*pi/18
Which are the same functions.
"""
if __name__ == '__main__':
PartA()
PartB()
PartC()
PartD()
QUESTION 2
from sympy import *
from sympy.abc import x
from sympy import Symbol
import numpy as np
from sympy.plotting import plot
import matplotlib.pyplot as plt
def a(n):
return x**n *factorial(2*n)/(factorial(n)**2)
def PartA():
print("*** Part A ***")
# Calculate the ratio test
n = symbols('n', integer=True, positive=True)
# Print simplified version of a(n+1)/a(n)
simplified = simplify(a(n + 1) / a(n))
print("Simplified: ")
print(simplified)
print()
# Now calculate the limit
print("Limit: ")
print(limit(simplified, n, float('inf')))
print()
def PartB():
print("*** Part B ***")
# Calculate the ratio test
n = symbols('n', integer=True, positive=True)
# Print simplified version of a(n+1)/a(n)
simplified = simplify(a(n + 1) / a(n))
# Now calculate the limit
lim = limit(simplified, n, float('inf'))
# Calculate the intervals for which x**3 < 1
interval = reduce_inequalities(lim < 1)
print("Interval of convergence:")
print(interval)
print()
def PartC():
print("*** Part C ***")
# Define the function of convergence
eq = 1/sqrt(1-4*x)
# Interval of convergence is [-1,1)
# Define x values
xvals = np.linspace(-1/4, 1/4 - 0.01, 100)
yvals = lambdify(x, eq)(xvals)
# Now plot
plt.plot(xvals, yvals, label='f(x)')
# Compute sq
s5 = a(0)
for i in range(1, 6):
s5 += a(i)
s10 = a(0)
for i in range(1, 11):
s10 += a(i)
s15 = a(0)
for i in range(1, 16):
s15 += a(i)
y_s5 = lambdify(x, s5)(xvals)
y_s10 = lambdify(x, s10)(xvals)
y_s15 = lambdify(x, s15)(xvals)
plt.plot(xvals, y_s5, label='s5')
plt.plot(xvals, y_s10, label='s10')
plt.plot(xvals, y_s15, label='s15')
plt.grid(True)
plt.legend()
plt.show()
if __name__ == '__main__':
PartA()
PartB()
PartC()
PROBLEM 3
from sympy import *
from sympy import Symbol
import numpy as np
from sympy.plotting import plot
import matplotlib.pyplot as plt
# Define x as positive
x = symbols('x', positive = True)
def a(n):
return x**(-log(n))
def PartA():
print("*** Part A ***")
# Calculate the ratio test
n = symbols('n', integer=True, positive=True)
# Print simplified version of a(n+1)/a(n)
simplified = simplify(a(n + 1) / a(n))
print("Simplified: ")
print(simplified)
print()
# Now calculate the limit
print("Limit: ")
print(limit(simplified, n, float('inf')))
print()
"""
We see that the result of the limit is 1. This means that the Ratio Test is inconclusive
"""
def PartB():
print("*** Part B ***")
# Calculate the ratio test
n = symbols('n', integer=True, positive=True)
# Define bn
xx = .9
bn = abs(xx**(-log(n)))
# Now, find the limit
print("Limit: ")
print(limit(bn, n, float('inf')))
print()
"""
In this case, the result is that the limit goes to infinity, which means that
the series diverges
"""
def PartC():
print("*** Part C ***")
# Calculate the ratio test
n = symbols('n', integer=True, positive=True)
# Define bn
xx = 1.1
cn = abs(xx ** (-log(n)))
# Now, find the limit
print("Limit: ")
print(limit(cn, n, float('inf')))
print()
"""
In this case, the result is that the limit goes to zero. It means that for x > 1 the
series converges.
"""
def PartD():
print("*** Part D ***")
print("There is no interval of convergence")
def PartE():
print("*** Part E ***")
"""
Using properties of logarithms, we have that, for x = e:
x^(-log(n)) = exp(-log(n)) = 1/exp(log(n)) = 1/n
Then the series is:
(-1)^(n+1) *(1/n) = -(-1)^n *(1/n)
(-1)^n (1/n) is a known series and it is known that its sum is log(2)
"""
xx = exp(1) # x = e
# Calulate the sum
n = symbols('n', integer=True, positive=True)
an = (-1)**(n+1) *xx**(-log(n))
# Define the values of n from 1 to a big number
nvals = np.linspace(1, 1E6, int(1E6))
sum_result = sum(lambdify(n, an)(nvals))
# Print sum
print("The calculated sum of the series is: {:.5f}".format(sum_result))
print("The expected sum of the series is: {:.5f}".format(np.log(2))) # print the value of log(2)
if __name__ == '__main__':
PartA()
PartB()
PartC()
PartD()
PartE()