Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. def bisection(f,a,b,N):
  2. '''Approximate solution of f(x)=0 on interval [a,b] by the bisection method.
  3.  
  4. Parameters
  5. ----------
  6. f : function
  7. The function for which we are trying to approximate a solution f(x)=0.
  8. a,b : numbers
  9. The interval in which to search for a solution. The function returns
  10. None if f(a)*f(b) >= 0 since a solution is not guaranteed.
  11. N : (positive) integer
  12. The number of iterations to implement.
  13.  
  14. Returns
  15. -------
  16. x_N : number
  17. The midpoint of the Nth interval computed by the bisection method. The
  18. initial interval [a_0,b_0] is given by [a,b]. If f(m_n) == 0 for some
  19. midpoint m_n = (a_n + b_n)/2, then the function returns this solution.
  20. If all signs of values f(a_n), f(b_n) and f(m_n) are the same at any
  21. iteration, the bisection method fails and return None.
  22.  
  23. Examples
  24. --------
  25. >>> f = lambda x: x**2 - x - 1
  26. >>> bisection(f,1,2,25)
  27. 1.618033990263939
  28. >>> f = lambda x: (2*x - 1)*(x - 3)
  29. >>> bisection(f,0,1,10)
  30. 0.5
  31. '''
  32. if f(a)*f(b) >= 0:
  33. print("Bisection method fails.")
  34. return None
  35. a_n = a
  36. b_n = b
  37. for n in range(1,N+1):
  38. m_n = (a_n + b_n)/2
  39. f_m_n = f(m_n)
  40. if f(a_n)*f_m_n < 0:
  41. a_n = a_n
  42. b_n = m_n
  43. elif f(b_n)*f_m_n < 0:
  44. a_n = m_n
  45. b_n = b_n
  46. elif f_m_n == 0:
  47. print("Found exact solution.")
  48. return m_n
  49. else:
  50. print("Bisection method fails.")
  51. return None
  52. return (a_n + b_n)/2
  53.  
  54. import pandas as pd
  55. df = pd.read_excel("directory.file.xlsx")
  56. v= df
  57. v
  58.  
  59. D P h O Q SD LT
  60. 80 27 0.37 50 2 3 1.51
  61.  
  62. from scipy.stats import norm
  63. import numpy as np
  64. f = lambda x: norm.ppf(1- (v['Q']*v['P']*v['h'])/(2*v['D']*x))*
  65. v['SD']*np.sqrt(v['LT'])*v['P']*v['h']+
  66. np.sqrt(2*v['D']*v['O']*v['h']*v['P'])-
  67. v['D']*x*(((-(norm.ppf(1-(v['Q']*v['h']*
  68. v['P'])/(2*x*v['D']))))*(1-norm.cdf((norm.ppf(1-
  69. (v['Q']*v['h']*v['P'])/(2*x*v['D']))),loc=0,scale=1))
  70. +(norm.pdf((norm.ppf(1-
  71. (v['Q']*v['h']*v['P'])/(2*x*v['D']))),loc=0,scale=1)))
  72. *v['SD']*np.sqrt(v['LT'])-v['Q'])/v['Q']*-1
  73. b = 10
  74. a = 1
  75. N = 100
  76. v['f'] = v.apply(bisection(f,a,b,N),axis=0)
  77. v
  78.  
  79. from scipy.stats import norm
  80. import numpy as np
  81. f = lambda x: norm.ppf(1- (2*27*0.37)/(2*80*x))*3*np.sqrt(1.51)*
  82. 27*0.37+np.sqrt(2*80*50*0.37*27)-80*x*
  83. (((-(norm.ppf(1-(2*0.37*27)/(2*x*80))))*
  84. (1-norm.cdf((norm.ppf(1-(2*0.37*27)/(2*x*80))),loc=0,scale=1))+
  85. (norm.pdf((norm.ppf(1-(2*0.37*27)/(2*x*80))),loc=0,scale=1)))*
  86. 3*np.sqrt(1.51)-2)/2*-1
  87. result = bisection(f,1,10,100)
  88. print(result)
  89.  
  90. Found exact solution.
  91. 4.503647631997683
  92.  
  93. from scipy.stats import norm
  94. import numpy as np
  95. f = lambda x: norm.ppf(1- (v['Q']*v['P']*v['h'])/(2*v['D']*x))*
  96. v['SD']*np.sqrt(v['LT'])*v['P']*v['h']+
  97. np.sqrt(2*v['D']*v['O']*v['h']*v['P'])-
  98. v['D']*x*(((-(norm.ppf(1-(v['Q']*v['h']*
  99. v['P'])/(2*x*v['D']))))*(1-norm.cdf((norm.ppf(1-
  100. (v['Q']*v['h']*v['P'])/(2*x*v['D']))),loc=0,scale=1))
  101. +(norm.pdf((norm.ppf(1-
  102. (v['Q']*v['h']*v['P'])/(2*x*v['D']))),loc=0,scale=1)))
  103. *v['SD']*np.sqrt(v['LT'])-v['Q'])/v['Q']*-1
  104. b = 10
  105. a = 1
  106. N = 100
  107. v['f'] = v.apply(bisection(f,a,b,N),axis=0)
  108. v
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement