Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New branch #962

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions taylor_mclaurian_expansion/README.md
Original file line number Diff line number Diff line change
@@ -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.
69 changes: 69 additions & 0 deletions taylor_mclaurian_expansion/main.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 2 additions & 0 deletions taylor_mclaurian_expansion/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mpmath==1.3.0
sympy==1.13.3
Loading