Error reducing algo - AndrewMZ6/python_cheat_sheet GitHub Wiki

phi = 5  
s = 4  
a = 0.4  

amp function models oamplifier behavior when nonlinearity starts. If k = 1 means that the aplifier works in linear zone.
But when it falls lower it means that non linearity involved and we have to change the input to continue desired output

def amp(A, k):
	return A*k	

def train(s, phi, a, n):
	# also while can be used
	# while e > 0.001:
	for i in range(n):
		A = s + phi
		print('A = ', A)
		P = amp(A, 0.9)
		print('P = ', P)
		error = s - P
		print('error = ', error)
		phi = phi + a*error

	return phi

print(train(s, phi, a, 100))

The logic behind this algo is that when error between input value s and output value P is lesser than some very little number we know that the right phi is found phi is a number which should be added to compensate non linearity

output:

A =  9
P =  8.1
error =  -4.1
A =  7.36
P =  6.6240000000000006
error =  -2.6240000000000006
A =  6.3104
P =  5.67936
error =  -1.67936
A =  5.638656
P =  5.0747904
error =  -1.0747904000000004
A =  5.20873984
P =  4.687865856
error =  -0.6878658560000002
A =  4.9335934976
P =  4.440234147840001
error =  -0.4402341478400009
A =  4.757499838464
P =  4.2817498546176
error =  -0.28174985461760027
A =  4.644799896616959
P =  4.180319906955264
error =  -0.18031990695526368
A =  4.5726719338348545
P =  4.115404740451369
error =  -0.11540474045136939
A =  4.526510037654306
P =  4.073859033888876
error =  -0.07385903388887627
A =  4.496966424098756
P =  4.04726978168888
error =  -0.04726978168887985
A =  4.478058511423204
P =  4.030252660280884
error =  -0.030252660280884136
... and so on ...