Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import sys
  3. import os
  4. import math
  5.  
  6.  
  7. def f1(x):
  8.         return x * .05
  9.  
  10. def f2(x):
  11.         return x + .003
  12.  
  13. def f3(x):
  14.         return x - .0008
  15.  
  16. def f4(x):
  17.         return x * .81
  18.  
  19.  
  20. WANTED_1 = 1.0
  21. WANTED_2 = 0.0
  22. WANTED_3 = 1.0
  23. WANTED_4 = 0.0
  24.  
  25. if __name__ == '__main__':
  26.    
  27.     i = .5
  28.  
  29.     error = 10000.0
  30.  
  31.     lastErrors = []
  32.  
  33.     numIters = 0
  34.  
  35.     while abs(error) > .01:
  36.        
  37.         x1 = f1(i)
  38.         x2 = f2(i)
  39.         x3 = f3(i)
  40.         x4 = f4(i)
  41.  
  42.         error = x1 + x2 + x3 + x4
  43.         error /= 4.0
  44.  
  45.         lastErrors.append(error)
  46.         if len(lastErrors) > 4:
  47.             lastErrors.pop(0)
  48.  
  49.         if error > 0:
  50.             i -= .000001
  51.         else:
  52.             i += .000004
  53.  
  54.         numIters += 1
  55.  
  56.         print ( "Error is: %.7f" %( error, ))
  57.  
  58.     print ( "Found best at i=%.7f after %d iterations" %( i, numIters ) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement