Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m having difficulties creating a couple of python functions for scientific co

ID: 3604416 • Letter: I

Question

I'm having difficulties creating a couple of python functions for scientific computing if anyone could help me out. I will post the code with descriptions of what the functions should do below.

import numpy as np
import matplotlib.pyplot as plt

def mysin(x, tol=1e-8):
"""
compute sin(x) by summing taylor expansion at 0,

sin(x) = x - x^3/3! + x^5/5! +.... + (-1)^k*x^(2k+1)/(2k+1)! + ...

up to accuracy of tol, i.e. break out of loop when the
|new term to be added| <= tol
"""
#
#note, for numerical efficiency, the new term to be added at the k-th step
#need not be computed as (-1)**k* x**(2*k+1)/(2*k+1)!
#instead, it can be computed by a simple update of term (-1)**(k-1)* x**(2*(k-1)+1)/(2*(k-1)+1)!
#already computed at the previous step: you only need to multiply it by -x**2/2k/(2k+1)
#(try to see if you can understand why. if you cannot do this, you may add term-by-term using
#the straightforward (-1)^k*x^(2k+1)/(2k+1)!, which is less efficient)
#
  


  
  


def mycos(x, tol=1e-8):
"""
compute cos(x) by summing taylor expansion at 0,

cos(x) = 1 - x^2/2! + x^4/4! +.... + (-1)^k*x^(2k)/(2k)! + ...

up to accuracy of tol, i.e. break out of loop when the
|new term to be added| <= tol
"""
#if you can, apply the more efficient 'updating' method instead of direct sum term-by-term

  


  

def mysin_mod(x, tol=1e-8):
"""
compute sin(x) by summing taylor expansion at x0=j*pi, (for some integer j such that
j*pi is closest to x), if j is even, then
sin(x) = (x-x0) - (x-x0)^3/3! + (x-x0)^5/5! +.... + (-1)^k*(x-x0)^(2k+1)/(2k+1)! + ...
if n is odd, multiply the above by -1.
"""
#if you can, apply the more efficient 'updating' method instead of direct sum term-by-term


  
def mycos_mod(x, tol=1e-8):
"""
compute cos(x) by summing taylor expansion at x0=j*pi, (for some integer j such that
j*pi is closest to x), if j is even, then
cos(x) = 1 + (x-x0)^4/4! + (x-x0)^8/8! + (x-x0)^12/12! + (x-x0)^16/16! + ...
- (x-x0)^2/2! + (x-x0)^6/6! + (x-x0)^10/10! + ... )
if j is odd, multiply the above by -1.
"""
#if you can, apply the more efficient 'updating' method instead of direct sum term-by-term


  
  

If anyone could help me with at least a couple of these thatd be great. Will thumbs up.

Explanation / Answer

import math

def mysin(x,tol = 1e-8):
   sum = x

   k = 1
   next = pow(-1,k) * pow(x,2*k+1)/6
   while abs(next) > tol:
        prod = 1
        j = 2
        while j <= 2*k +1:
           prod = prod * j
           j = j+1
        next = pow(-1,k) * pow(x,2*k+1)/prod
        sum = sum + next
        k = k +1
   return sum

def mycos(x,tol = 1e-8):
   sum = 1

   k = 1
   next = pow(-1,k) * pow(x,2*k)/2
   while abs(next) > tol:
        prod = 1
        j = 2
        while j <= 2*k:
           prod = prod * j
           j = j+1
        next = pow(-1,k) * pow(x,2*k)/prod
        sum = sum + next
        k = k +1
   return sum


print(mysin(20 * (3.14/180)) ,math.sin(20 * (3.14/180)))
print(mycos(20 * (3.14/180)) ,math.cos(20 * (3.14/180)))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote