User defined sine function in python using series expansion Applying a Maclaurin expansion to the sine function gives
$$\sin\left(x\right)=\frac{x}{1!}-\frac{x^{3}}{3!}+\frac{x^{5}}{5!}-\frac{x^{7}}{7!}\;+...+\sum_{n=1}^{\infty}\frac{\left(-1\right)^{n+1}}{\left(2n-1\right)!}x^{2n-1}$$
with the ratio between successive terms being
$$\frac{-x^{2}}{\left(2n-1\right)\left(2n-2\right)}$$
I've written a program to approximate the sine function using a fixed number of terms from the expansion, for example the first 10 terms.
Here is my code:
from numpy import *

def mac(x, nmax):
    sigma = x
    term = x
    n = 2
    while (n <= nmax):
        term *= -x**2 / ((2*n - 1) * (2*n - 2))
        sigma += term
        n += 1
    return sigma

file = open('task13.txt', 'w')
x = 0.0
xmax = 14.0
dx = 0.1
nmax = 10
file.write('nmax = ' + str(nmax) + '\n')
file.write('x' + '\t' + 'series expansion' + '\t' + 'sin(x)' + '\n')
while (x <= xmax):
    file.write(str(x) + '\t' + str(mac(x, nmax)) + '\t' + str(sin(x)) + '\n')
    x += dx

file.close()
exit()

Here is a plot of the data:

Why does the approximation rapidly deviate from the actual value of $\sin(x)$ ? Is it because of an error in the code or is there a better way of calculating $\sin(x)$ using a user defined function?

The MacLaurin expansion is a Taylor series centered around $x = 0$. That means that the more terms you compute the better your approximation will become. However, you cannot expect infinite precision, as you would have to compute an infinite number of terms, which is not possible.
A MacLaurin expansion guarantees precision only around $0$, not over $\mathbb R$.
