-
Notifications
You must be signed in to change notification settings - Fork 0
/
fittingCurve1.py
135 lines (123 loc) · 3.79 KB
/
fittingCurve1.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
print(" ...........fitting Curve........ ")
x=[]
y=[]
x2l = []
y2l = []
xyl = []
X=[]
Y=[]
def Curve():
try:
def plot():
ask = str(input("Want to see graph(y/n):"))
if ask=="y":
ask1 = int(input("Enter No. of points you want to take:"))
for i in range(ask1): #straight line
X1 = float(input("Hence at x="))
X.append(X1)
Y1 = a+b*X1
Y.append(Y1)
print(f"x={X}, y={Y}")
plt.scatter(x,y,label="skitscat",color="k")
plt.plot(X,Y)
plt.title("Fitting Curve")
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.show()
if ask=="n":
exit()
ni = int(input("Enter no. of inputs:"))
for i in range(ni):
xi = float(input("Enter elements of x:"))
x.append(xi)
for i in range(ni):
yi = float(input("Enter elements of y:"))
y.append(yi)
print(x)
print(y)
print("====================")
dep = str(input("Dependent on(x/y):"))
n = 0
sum_y = 0
sum_x = 0
sum_x2 = 0
sum_xy = 0
sum_y2 = 0
for i in x:
n = n + (i+1)/(i+1)
for j in y:
sum_y = sum_y + j
sum_y2 = sum_y2 + j**2
y2l.append(j**2)
for z in x:
sum_x = sum_x + z
sum_x2 = sum_x2 + z**2
x2l.append(z**2)
for i in range(int(n)):
sum_xy = sum_xy + x[i]*y[i]
xyl.append(x[i]*y[i])
sum_xyy = float("{:.1f}".format(sum_xy))
if dep=="x":
fr = {
'x':x, #x,y Scatter
'y':y,
'x2':x2l,
'xy':xyl
}
print("n =",n)
tab = pd.DataFrame(fr)
print(tab)
print("---------------------------------")
print("Sum of x:",sum_x)
print("Sum of y:",sum_y)
print("Sum of x2:",sum_x2)
print("Sum of xy:",sum_xyy)
a1 = np.array([[n,sum_x],[sum_x,sum_x2]])
a2 = np.array([[sum_y],[sum_xyy]])
ina1 = np.linalg.inv(a1)
a11 = np.dot(ina1,a2)
A = a11[0,0]
B= a11[1,0]
a = float("{:.3f}".format(A))
b = float("{:.3f}".format(B))
print("---------------")
print(f"|a={a},b={b}|")
print("---------------")
plot()
exit()
elif dep=="y":
fr = {
'x':x,
'y':y,
'y2':y2l,
'xy':xyl
}
print("n =",n)
tab = pd.DataFrame(fr)
print(tab)
print("---------------------------------")
print("Sum of x:",sum_x)
print("Sum of y:",sum_y)
print("Sum of y2:",sum_y2)
print("Sum of xy:",sum_xyy)
a1 = np.array([[n,sum_y],[sum_y,sum_y2]])
a2 = np.array([[sum_x],[sum_xyy]])
ina1 = np.linalg.inv(a1)
a11 = np.dot(ina1,a2)
A = a11[0,0]
B= a11[1,0]
a = float("{:.3f}".format(A))
b = float("{:.3f}".format(B))
print("---------------")
print(f"|a={a},b={b}|")
print("---------------")
plot()
exit()
else:
print("give valid input")
except Exception:
print("Invalid Input")
Curve()