Advertisement
yloplopy

Secant Method 1.5.7.172

Apr 29th, 2019
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.71 KB | None | 0 0
  1. import math
  2. import scipy.integrate as integrate
  3.  
  4. def PROBLEM1_EQ(x):
  5.    
  6.     i = ((3 * (x**3)) - (5 * (x**2)) - (4 * x) + 4)
  7.    
  8.     return i
  9.  
  10. def PROBLEM1_DERIV_EQ(x):
  11.    
  12.     i = (9 * (x ** 2)) - (10 * x) - 4
  13.    
  14.     return i
  15.  
  16. def PROBLEM5_EQ(x):
  17.    
  18.     i = ((math.exp(x)) - (100 * (x**2)))
  19.    
  20.     return i
  21.  
  22. def PROBLEM5_DERIV_EQ(x):
  23.    
  24.     i = (-200 * x) + (math.exp(x))
  25.    
  26.     return i
  27.  
  28. def PROBLEM7_EQ(x):
  29.    
  30.     i = (x * (math.cosh(100/x))) - x - 15
  31.    
  32.     return i
  33.  
  34. def PROBLEM7_EQ_DER(x):
  35.    
  36.     i = math.cosh(100/x)-((100*math.sinh(100/x))/x)-1
  37.    
  38.     return i
  39.        
  40. def PROBLEM7_Integ():
  41.    
  42.     i = integrate.quad(lambda x: math.sqrt(1+math.pow(math.sinh(x/335.7), 2)), 0, 100)
  43.     print("Length:", i[0] * 2)
  44.  
  45.  
  46. def rate_of_convergence(x):
  47.    
  48.     L = len(x)
  49.  
  50.     y0 = math.log(abs(x[L-1] - x[L-2]))
  51.     x0 = math.log(abs(x[L-2] - x[L-3]))
  52.     y1 = x0
  53.     x1 = math.log(abs(x[L-3] - x[L-4]))
  54.    
  55. #    x0 = round(x0, 1)
  56. #    y0 = round(y0, 0)
  57. #    x1 = round(x1, 0)
  58. #    y1 = x0
  59.    
  60.     slope = ((y1 - y0) / (x1 - x0))
  61.    
  62.     print("\nRate of convergence: ", slope)
  63.  
  64. def bisect(fcn,a,b,acc):
  65.    
  66.     fa = fcn(a)
  67.     #fb = fcn(b)
  68.     count = 1
  69.    
  70.     print(f'{count:<4} : {a:<20}  {b:<25} ')
  71.     while abs(b - a) > acc:
  72.         m = (a + b) / 2
  73.         fm = fcn(m)
  74.         if fa * fm <= 0:
  75.             b = m
  76.             count = count + 1
  77.             print(f'{count:<4} : {a:<20}  {b:<25} ')
  78.         else:
  79.             a = m
  80.             count = count + 1
  81.             print(f'{count:<4} : {a:<20}  {b:<25} ')
  82.    
  83.     count = count + 1
  84.     return (a + b) / 2
  85.  
  86. def secant(fcn, x0, x1, op1, tol):
  87.    
  88.     count = 2
  89.     outputs.append(x0)
  90.     outputs.append(x1)
  91.    
  92.     if op1 == 1:
  93.         for x in range(4):
  94.             x_new = x1 - ((fcn(x1) * (x0 - x1)) / (fcn(x0) - fcn(x1)))
  95.             print("x" + str(count),":",x_new)
  96.             outputs.append(x_new)
  97.             count = count + 1
  98.             x0 = x1
  99.             x1 = x_new
  100.            
  101.     if op1 == 2:
  102.         while(abs(x0 - x1) > tol):
  103.             x_new = x1 - ((fcn(x1) * (x0 - x1)) / (fcn(x0) - fcn(x1)))
  104.             print("x" + str(count),":",x_new)
  105.             outputs.append(x_new)
  106.             count = count + 1
  107.             x0 = x1
  108.             x1 = x_new
  109.  
  110. def newton(fcn, df, x0, tol, option):
  111.     c = 1
  112.    
  113.     if option == 0:
  114.         for x in range(4):
  115.             x1 = x0 - ((fcn(x0)) / (df(x0)))
  116.             outputs_comparison.append(x1)
  117.             print("Iteration", x+1,": ", x1)
  118.             x0 = x1
  119.     elif option == 1:
  120.         while True:
  121.             x1 = x0 - ((fcn(x0)) / (df(x0)))
  122.             outputs_comparison.append(x1)
  123.             print("Iteration", c,": ", x1)
  124.             c += 1
  125.             if abs(x0 - x1) <= tol:
  126.                 break
  127.            
  128.             x0 = x1
  129.            
  130. outputs = []
  131. outputs_comparison = []
  132.  
  133. print("\n---------")
  134. print("PROBLEM 1")
  135. print("---------\n")
  136. print("SECANT")
  137. print("------\n")
  138. print("x0 :", 0.6)
  139. print("x1 :", 0.7)
  140. secant(PROBLEM1_EQ, 0.4, 0.5, 1, 0)
  141. rate_of_convergence(outputs)
  142. print("\nNEWTON")
  143. print("------\n")
  144. newton(PROBLEM1_EQ, PROBLEM1_DERIV_EQ, 0.4, 10e-6, 0)
  145. rate_of_convergence(outputs_comparison)
  146.  
  147. outputs = []
  148. outputs_comparison = []
  149. print("\n---------")
  150. print("PROBLEM 5")
  151. print("---------\n")
  152. print("SECANT")
  153. print("------\n")
  154.  
  155. print("---")  
  156. print("(i)")
  157. print("---")
  158. print("x0 :", 0)
  159. print("x1 :", 0.2)
  160. secant(PROBLEM5_EQ, 0, 0.2, 2, 10e-10)
  161. rate_of_convergence(outputs)
  162.  
  163. outputs = []
  164. outputs_comparison = []
  165. print("----")  
  166. print("(ii)")
  167. print("----")
  168. print("x0 :", -0.2)
  169. print("x1 :", 0)
  170. secant(PROBLEM5_EQ, -0.2, 0, 2, 10e-10)
  171. rate_of_convergence(outputs)
  172.  
  173. outputs = []
  174. outputs_comparison = []
  175. print("-----")  
  176. print("(iii)")
  177. print("-----")
  178. print("x0 :", 8)
  179. print("x1 :", 10)
  180. secant(PROBLEM5_EQ, 8, 10, 2, 10e-10)
  181. rate_of_convergence(outputs)
  182.  
  183. print("\nNEWTON")
  184. print("------\n")
  185.  
  186. print("---")  
  187. print("(i)")
  188. print("---")
  189. newton(PROBLEM5_EQ, PROBLEM5_DERIV_EQ, -0.2, 10e-10, 0)
  190.  
  191. print("----")  
  192. print("(ii)")
  193. print("----")
  194. newton(PROBLEM5_EQ, PROBLEM5_DERIV_EQ, 0.3, 10e-10, 0)
  195.  
  196. print("-----")  
  197. print("(iii)")
  198. print("-----")
  199. newton(PROBLEM5_EQ, PROBLEM5_DERIV_EQ, 8, 10e-10, 0)
  200. outputs = []
  201. outputs_comparison = []
  202. print("\n---------")
  203. print("PROBLEM 7")
  204. print("---------\n")
  205. print("x0 :", 300)
  206. print("x1 :", 400)
  207. secant(PROBLEM7_EQ, 300, 400, 1, 10e-12)
  208. rate_of_convergence(outputs)
  209. newton(PROBLEM7_EQ, PROBLEM7_EQ_DER, 340, 10e-6, 0)
  210. rate_of_convergence(outputs_comparison)
  211. bisect(PROBLEM7_EQ, 300, 400, 10e-2)
  212. outputs = [335.546875, 335.7421875, 335.83984375, 335.791015625]
  213. rate_of_convergence(outputs)
  214. PROBLEM7_Integ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement