diff --git a/taylor_mclaurian_expansion/README.md b/taylor_mclaurian_expansion/README.md new file mode 100644 index 000000000..033a27172 --- /dev/null +++ b/taylor_mclaurian_expansion/README.md @@ -0,0 +1,25 @@ +# **Taylor and Mclaurian Expansions** + +[![python 3](https://img.shields.io/badge/python-3.8-blue)](https://python.org) + + This is a small convenient program to use to approximate values of expresstions using taylor expansions or + mclaurian expansions + + **Python** has a great library [*sympy*](https://github.com/sympy/sympy) which allows us to do many calculations, + and display them in a manner we are used to. + + +### Before you import sympy you need to install it: + + ```python + pip3 install sympy + ``` + + +### You can also do the same through the requirements.txt file + +```python +pip install requirements.txt +``` + +Some examples have been included for easier use. \ No newline at end of file diff --git a/taylor_mclaurian_expansion/main.py b/taylor_mclaurian_expansion/main.py new file mode 100644 index 000000000..888a042f6 --- /dev/null +++ b/taylor_mclaurian_expansion/main.py @@ -0,0 +1,69 @@ +from sympy import symbols, factorial, diff, pprint, exp, log, sin + +x = symbols('x') +y = symbols('y') + + +def taylorexpansion(func, a, n, var): + t_y = symbols('t_y') + expansion = func.subs(var, a) + d = func + if (expansion == 0): + n += 1 + try: + k = 1 + while (k < n): + d = diff(d, var) + term = (d * ((t_y - a) ** k)) / factorial(k) + term = term.subs(var, a) + if (term == 0): + continue + term = term.subs(t_y, var) + expansion = term + expansion + k += 1 + if (d == 0 and k < n): + print("only ", k - 1, " terms present") + if (n < 1): + print("3rd argument is for no. of terms, provide a natural number") + return '' + expansion = expansion.subs(t_y, var) + return expansion + except TypeError: + print("3rd argument denotes number of terms, provide a natural number") + return '' + + +def taylorvalue(func, a, n, x): + f = taylorexpansion(func, a, n, x) + return f.evalf(subs={x: a}) + + +def mclaurianexpansion(func, steps, variable): + taylorexpansion(func, 0, steps, variable) + + +def mclaurianvalue(func, steps, variable): + taylorvalue(func, 0, steps, variable) + + +def examples(): + # e^x expansion at x=0 with 5 terms differentiating with respect to x + pprint(taylorexpansion(exp(x), 0, 5, x)) + + # e^x approximation at x=1 with 10 terms differentiating with respect to x + print(taylorvalue(exp(x), 1, 10, x)) + + # log(1+x) expansion at x=0 with 5 terms differentiating with respect to x + pprint(taylorexpansion(log(x + 1), 0, 5, x)) + + # sin(x) expansion at x=0 with 5 terms differentiating with respect to x + pprint(taylorexpansion(sin(x), 0, 5, x)) + + # expansion for expression at x=0 with 3 terms differentiating wrt to x + pprint(taylorexpansion((5 * x ** 2) + (3 * x) + (7), 0, 3, x)) + + # e^(xy) expansion at x=1 with 5 terms differentiating with respect to x + pprint(taylorexpansion(exp(x * y), 1, 5, x)) + + +examples() diff --git a/taylor_mclaurian_expansion/requirements.txt b/taylor_mclaurian_expansion/requirements.txt new file mode 100644 index 000000000..c9ae7aa06 --- /dev/null +++ b/taylor_mclaurian_expansion/requirements.txt @@ -0,0 +1,2 @@ +mpmath==1.3.0 +sympy==1.13.3