Skip to content

Commit e02b8e9

Browse files
committed
Taylor/Mclaurian expansion
1 parent 0fa47cd commit e02b8e9

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

taylor_mclaurian_expansion/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# **Taylor and Mclaurian Expansions**
2+
3+
[![python 3](https://img.shields.io/badge/python-3.8-blue)](https://python.org)
4+
5+
This is a small convenient program to use to approximate values of expresstions using taylor expansions or
6+
mclaurian expansions
7+
8+
**Python** has a great library [*sympy*](https://github.com/sympy/sympy) which allows us to do many calculations,
9+
and display them in a manner we are used to.
10+
11+
12+
### Before you import sympy you need to install it:
13+
14+
```python
15+
pip3 install sympy
16+
```
17+
18+
19+
### You can also do the same through the requirements.txt file
20+
21+
```python
22+
pip install requirements.txt
23+
```
24+
25+
Some examples have been included for easier use.

taylor_mclaurian_expansion/main.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import math
2+
from sympy import *
3+
4+
x = symbols('x')
5+
y = symbols('y')
6+
7+
def taylorexpansion(func,a,n,var):
8+
t_y = symbols('t_y')
9+
expansion = func.subs(var,a)
10+
d = func
11+
if(expansion==0):
12+
n+=1
13+
try:
14+
k=1
15+
while (k<n):
16+
d = diff(d,var)
17+
term = (d*((t_y-a)**k))/factorial(k)
18+
term = term.subs(var,a)
19+
if(term==0):
20+
continue
21+
term = term.subs(t_y,var)
22+
expansion=term+expansion
23+
k+=1
24+
if(d==0 and k<n):
25+
print("only ",k-1," terms present")
26+
if n<1:
27+
print("3rd argument denotes number of terms and should be a natural number")
28+
return ''
29+
expansion = expansion.subs(t_y,var)
30+
return expansion
31+
except TypeError:
32+
print("3rd argument denotes number of terms and should be a natural number")
33+
return ''
34+
35+
def taylorvalue(func,a,n,x):
36+
f=taylorexpansion(func,a,n,x)
37+
return f.evalf(subs={x:a})
38+
39+
def mclaurianexpansion(func,steps,variable):
40+
taylorexpansion(func,0,steps,variable)
41+
42+
def mclaurianvalue(func,steps,variable):
43+
taylorvalue(func,0,steps,variable)
44+
45+
def examples():
46+
# e^x expansion at point x=0 with 5 terms differentiating with respect to x
47+
pprint(taylorexpansion(exp(x),0,5,x))
48+
49+
# e^x approximation at point x=1 with 10 terms differentiating with respect to x
50+
print(taylorvalue(exp(x),1,10,x))
51+
52+
# log(1+x) expansion at point x=0 with 5 terms differentiating with respect to x
53+
pprint(taylorexpansion(log(x+1),0,5,x))
54+
55+
# sin(x) expansion at point x=0 with 5 terms differentiating with respect to x
56+
pprint(taylorexpansion(sin(x),0,5,x))
57+
58+
# expansion for expression at point x=0 with 3 terms differentiating with respect to x
59+
pprint(taylorexpansion((5*x**2)+(3*x)+(7),0,3,x))
60+
61+
# e^(xy) expansion at point x=1 with 5 terms differentiating with respect to x
62+
pprint(taylorexpansion(exp(x*y),1,5,x))
63+
64+
examples()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mpmath==1.3.0
2+
sympy==1.13.3

0 commit comments

Comments
 (0)