import numpy as np
import pandas as pd
import numpy.linalg as LA
df = pd.read_csv('data/data1.csv')
df
no. | f | x | y | z | |
---|---|---|---|---|---|
0 | 1 | 0.720482 | 0.737451 | 4.10 | 0.8695 |
1 | 2 | 0.812391 | 0.884248 | 3.65 | 0.5750 |
2 | 3 | 0.813530 | 0.863871 | 3.55 | 0.5125 |
3 | 4 | 0.532572 | 0.872902 | 3.85 | 0.6520 |
4 | 5 | 0.697305 | 0.891665 | 4.70 | 0.5400 |
5 | 6 | 0.733407 | 0.880975 | 0.70 | 0.7745 |
6 | 7 | 0.654187 | 0.733362 | 2.25 | 0.7130 |
7 | 8 | 0.709219 | 0.839979 | -1.10 | 0.9320 |
8 | 9 | 0.750032 | 0.917038 | -1.25 | 0.7335 |
9 | 10 | 0.713197 | 0.915913 | 0.90 | 0.7345 |
10 | 11 | 0.521182 | 0.885547 | -0.15 | 0.7685 |
11 | 12 | 0.824820 | 0.884369 | 0.60 | 0.7090 |
12 | 13 | 0.770785 | 0.873444 | 1.25 | 0.4890 |
13 | 14 | 0.772823 | 0.726831 | 3.40 | 0.5375 |
14 | 15 | 0.600803 | 0.638667 | 1.80 | 0.8985 |
15 | 16 | 0.792857 | 0.753639 | 1.10 | 0.5705 |
16 | 17 | 0.800259 | 0.819985 | 0.80 | 0.4965 |
17 | 18 | 0.537606 | 0.847556 | -0.25 | 0.8340 |
18 | 19 | 0.575679 | 0.884614 | 2.85 | 0.7870 |
19 | 20 | 0.821320 | 0.863913 | 0.75 | 0.6245 |
20 | 21 | 0.325209 | 0.757005 | -0.20 | 0.7630 |
21 | 22 | 0.790763 | 0.899136 | 5.30 | 0.4470 |
22 | 23 | 0.803798 | 0.897501 | 3.45 | 0.3915 |
23 | 24 | 0.785717 | 0.895834 | 4.40 | 0.4190 |
24 | 25 | 0.754932 | 0.808529 | 3.80 | 0.5265 |
25 | 26 | 0.793982 | 0.892413 | 5.10 | 0.4350 |
26 | 27 | 0.789723 | 0.881186 | 3.40 | 0.4320 |
27 | 28 | 0.797951 | 0.936163 | 3.20 | 0.5280 |
28 | 29 | 0.796906 | 0.934264 | 2.80 | 0.4445 |
29 | 30 | 0.800149 | 0.941757 | 3.00 | 0.4490 |
Normal equation are
∑f=na+b∑x+c∑y+d∑z
∑xf=a∑x+b∑x2+c∑xy+d∑xz
∑yf=a∑y+b∑xy+c∑y2+d∑yz
∑zf=a∑z+b∑xz+c∑zy+d∑z2
n=30
a11=n
a12=np.sum(df['x']) #sum of x
a13=np.sum(df['y']) #sum of y
a14=np.sum(df['z']) #sum of z
a21=np.sum(df['x']) #sum of x
a22=np.sum(df['x']**2) #sum of x2
a23=np.sum(np.dot(df['x'],df['y'])) #sum of xy
a24=np.sum(np.dot(df['x'],df['z'])) #sum of xz
a31=np.sum(df['y']) #sum of y
a32=np.sum(np.dot(df['y'],df['x'])) #sum of xy
a33=np.sum(df['y']**2) #sum of y2
a34=np.sum(np.dot(df['y'],df['z'])) #sum of yz
a41=np.sum(df['z']) #sum of z
a42=np.sum(np.dot(df['z'],df['x'])) #sum of xz
a43=np.sum(np.dot(df['z'],df['y'])) #sum of yz
a44=np.sum(df['z']**2) #sum of z2
b1=np.sum(df['f']) #sum of f
b2=np.sum(np.dot(df['f'],df['x'])) #sum of fx
b3=np.sum(np.dot(df['f'],df['y'])) #sum of fy
b4=np.sum(np.dot(df['f'],df['z'])) #sum of fz
A = np.array([[a11,a12,a13,a14],[a21,a22,a23,a24],[a31,a32,a33,a34],[a41,a42,a43,a44]])
b = np.array([b1,b2,b3,b4])
x = LA.solve(A,b)
x
array([ 0.85507492, 0.14860913, -0.00131447, -0.41792768])
print('\n a=',"{:10.3}".format(x[0]))
print('\n b=',"{:.3}".format(x[1]))
print('\n c=',"{:.3}".format(x[2]))
print('\n d=',"{:.3}".format(x[3]))
a= 0.855 b= 0.149 c= -0.00131 d= -0.418
f_exp=[] # expected value of f
for i in range(n):
f_exp.append(x[0]+x[1]*df.iloc[i,2]+x[2]*df.iloc[i,3]+x[3]*df.iloc[i,4])
f_exp
[0.5958894003599622, 0.7413760360958024, 0.7645997139268501, 0.7072466193451008, 0.7557255547611699, 0.6613906839806531, 0.6631192534951209, 0.5918408249814666, 0.68644830702124, 0.6830369792734646, 0.6656950521790324, 0.6894008781179288, 0.7788669964212885, 0.7339832421031306, 0.5721125629994265, 0.7271988454495809, 0.7683794858588099, 0.6328064581873758, 0.6538812940618572, 0.7214786253986405, 0.6489567737789436, 0.7949143877809594, 0.8202981096648657, 0.8073086989485949, 0.7501958066178496, 0.7991933185714644, 0.8010132155323733, 0.7693252220065355, 0.8044656816412954, 0.8034356104392159]
RMSE=np.sqrt(np.sum((df['f']-f_exp)**2)/n)
R_square=1-(np.sum(np.sum(np.sum((df['f']-f_exp)**2))/np.sum((df['f']-df['f'].mean())**2)))
print('\n RMSE=',"{:.2}".format(RMSE))
print('\n R square=',"{:.2}".format(R_square))
RMSE= 0.093 R square= 0.36
Calculate I1,I2,I3
Using Kirchhoff's laws in junction a,loop abdea,aegha
I3=I1+I2
25.1I1+24+40.5I2=0
−18+40.5I2+78.25I3=0
A = np.array([[1,1,-1],[25.1,40.5,0],[0,40.5,78.25]]) #arranging I1,I2,I3
b = np.array([0,-24,18])
x = LA.solve(A,b)
x
array([ 18.9867374, -12.3596817, 6.6270557])
print('\n I1=',"{:.3}".format(x[0]))
print('\n I2=',"{:.3}".format(x[1]))
print('\n I3=',"{:.3}".format(x[2]))
I1= 19.0 I2= -12.4 I3= 6.63