-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
50 lines (42 loc) · 1.3 KB
/
main.py
File metadata and controls
50 lines (42 loc) · 1.3 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from deriv import *
import matplotlib.pyplot as plt
import matplotlib.animation as ani
import sympy as sp
import numpy as np
import sys
x = sp.symbols('x')
while True:
try:
raw_eq = input("Enter equation: ")
eq = sp.sympify(raw_eq)
break
except (sp.SymplifyError, SyntaxError) as e:
print("Invalid Input")
except Exception as e:
print(f"An exception occured during execution: {e}")
try:
fig, ax = plt.subplots()
line, = ax.plot([], [], lw=2)
tlist = newton(eq)
#configure graph
ax.set_ylim(-60, 60)
ax.set_xlim(-20, 20)
ax.axhline(0, color='black', linewidth=0.5)
ax.axvline(0, color='black', linewidth=0.5)
ax.grid(True)
x_vals = np.linspace(-20, 20, 400)
y_vals_eq = [eq.subs(sp.symbols('x'), x) for x in x_vals]
ax.plot(x_vals, y_vals_eq, 'r-', lw=1)
# Configure animation
def init():
line.set_data([], [])
return line,
def update(frame):
x_vals = np.linspace(-20, 20, 400)
y_vals = [tlist[frame](x) for x in x_vals]
line.set_data(x_vals, y_vals)
return line,
anim = ani.FuncAnimation(fig, update, frames=len(tlist), init_func=init, interval=100, blit=True)
plt.show()
except Exception as e:
print(f"An exception occured during execution: {e}")