-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot mse.py
69 lines (59 loc) · 1.41 KB
/
plot mse.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import re
import matplotlib.pyplot as plt
import numpy as np
import sys
import json
replacements = {
'sin' : 'np.sin',
'cos' : 'np.cos',
'tan':"np.tan",
'exp': 'np.exp',
'sqrt': 'np.sqrt',
'^': '**',
"pi": 'np.pi',
}
allowed_words = [
'x',
'sin',
'cos',
"tan",
'sqrt',
'exp',
"pi"
]
def string2func(string):
''' evaluates the string and returns a function of x '''
# find all words and check if all are allowed:
for word in re.findall('[a-zA-Z_]+', string):
if word not in allowed_words:
raise ValueError(
'"{}" is forbidden to use in math expression'.format(word)
)
for old, new in replacements.items():
string = string.replace(old, new)
def func(x):
return eval(string)
return func
if __name__ == '__main__':
ys=None
n=None
func_str=None
if len(sys.argv)>1:
inp=sys.argv[1]
ys = json.loads(inp)
# ys=map(float, inp.strip('[]').split(','))
n=len(ys)
# else:
# func_str=input('enter function: f(x) = ')
# func = string2func(func_str)
xPoints = []
yPoints = []
for x in range(1,n+1):
y =ys[x-1]
xPoints.append(x)
yPoints.append(y)
plt.title("mse:")
plt.plot(xPoints, yPoints)
plt.show()
# a = float(input('enter lower limit: '))
# b = float(input('enter upper limit: '))