-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.py
More file actions
27 lines (21 loc) · 898 Bytes
/
graph.py
File metadata and controls
27 lines (21 loc) · 898 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import sys
import json
from sympy import symbols, lambdify, E, pi
from sympy.parsing.sympy_parser import parse_expr
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
payload = json.loads(sys.argv[1])
t = np.linspace(0, float(payload['timeMax']), 400)
y_symbol, t_symbol = symbols('y t')
for equation in payload['equations']:
# Usually the mathematical constant e has to be written as E, but use allow_dict to recognize e as well.
clean_expr = parse_expr(equation['value'], local_dict={'e': E, 'π': pi}, transformations='all')
func = lambdify((y_symbol, t_symbol), clean_expr, 'numpy')
initial_condition = float(equation['initialCondition'])
solution = odeint(func, initial_condition, t)
plt.plot(t, solution, label='dy/dt = ' + equation['value'])
plt.xlabel('Time')
plt.ylabel('y(t)')
plt.legend()
plt.savefig(payload['destination'], dpi=300)