Advertisement
arnobkumarsaha

Python before 16-9-18

Sep 16th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.97 KB | None | 0 0
  1. BISECTION
  2.  
  3. import math
  4. import random
  5.  
  6. def f(x):
  7.     return math.exp(-x) - x
  8.  
  9. while(1):
  10.     a = random.random()
  11.     b = random.random()
  12.     if f(a)*f(b) < 0:
  13.         break
  14. x = []
  15. y = []
  16. i = 0
  17. n = 20
  18. c = (a+b)/2
  19. while(i < n):
  20.    
  21.     if f(c) == 0:
  22.         break
  23.     if f(c)*f(a) > 0:
  24.         a = c
  25.     if f(c)*f(b) > 0:
  26.         b = c
  27.     temp = c
  28.     c = (a+b)/2
  29.     #Difference of current c and previous c
  30.     #Error approximation
  31.     y.append(abs((c-temp)/c)*100)
  32.     i+=1
  33.  
  34. print c, f(c)
  35.  
  36. import matplotlib.pyplot as plt
  37. plt.plot(range(1,n+1),y,marker = 'o')
  38. plt.show()
  39.  
  40.  
  41.  
  42. POWER
  43.  
  44.  
  45. # -*- coding: wtf-8 -*-
  46. """
  47. Created on Tue Sep  4 14:06:16 2018
  48.  
  49. @author: CSE CARNIVAL 2017
  50. """
  51.  
  52. import math
  53. def ePower(x,tol):
  54.    val=1
  55.    i=0
  56.    while True:
  57.        temp=val
  58.        i+=1;
  59.        val+=(x**i)/math.factorial(i)
  60.        if (val-temp)/val<=tol:
  61.            break
  62.        
  63.    return val,i
  64.    
  65. print ePower(5,0.01)
  66. print math.exp(0.5)
  67.    
  68. EXPONENTIAL
  69.  
  70. # -*- coding: utf-8 -*-
  71. """
  72. Created on Sun Sep  9 14:24:32 2018
  73.  
  74. @author: CSE CARNIVAL 2017
  75. """
  76.  
  77. """
  78. e^x - exponential
  79. """
  80.  
  81. #Loop until a fixed tolerance of error
  82. import numpy as np
  83. import math
  84. def myexp(x, tol):
  85.     s = 1.
  86.     i = 1
  87.     while(1):
  88.         s_p = s
  89.         s += (x**i)/math.factorial(i)
  90.         err = (s-s_p)/s*100
  91.        
  92.         if(err < tol):
  93.             break
  94.         i+=1
  95.     return s,i
  96.  
  97. #Loop until a fixed number of iterations
  98. def myexp2(x, n):
  99.     s = 1.
  100.     i = 1
  101.     while(1):
  102.         s_p = s
  103.         s += (x**i)/math.factorial(i)
  104.         err = (s-s_p)/s*100
  105.        
  106.         if(i >= n):
  107.             break
  108.         i+=1
  109.     return s
  110.  
  111. print myexp(0.5,0.0000001)
  112. print myexp2(0.5,10)
  113. print math.exp(0.5)
  114.  
  115. true_val = math.exp(0.5)
  116. y = []
  117. x = range(10)
  118. y= []
  119. for i in x:
  120.     approx_val = myexp2(0.5,i)
  121.     true_err = (true_val - approx_val)/true_val*100
  122.     y.append(true_err)
  123.  
  124. #Same shit using list comprehension
  125. #y = [(true_val - myexp2(0.5,i))/true_val*100 for i in x]
  126.    
  127. import matplotlib.pyplot as plt
  128. plt.plot(x,y,marker='o')
  129. plt.show()
  130.    
  131.  
  132. FALSE POSITION
  133.  
  134. # -*- coding: utf-8 -*-
  135. """
  136. Created on Tue Sep 11 14:12:56 2018
  137.  
  138. @author: CSE CARNIVAL 2017
  139. """
  140. import math
  141. import random
  142.  
  143. def f(x):
  144.     return math.exp(-x)-x
  145.  
  146. while(True):
  147.     x_l=random.random()
  148.     x_u=random.random()
  149.     if f(x_l)*f(x_u)<0:
  150.         break
  151.  
  152. #x_l=0
  153. #x_u =1
  154. i=0
  155. x_r=x_u-(f(x_u)*(x_l-x_u))/(f(x_l)-f(x_u))
  156. err=[]
  157. while(i<20):
  158.    
  159.  
  160.     if f(x_r)==0:
  161.         break
  162.     if f(x_r)*f(x_l)>0:
  163.         x_l=x_r
  164.     if f(x_r)*f(x_u)>0:
  165.         x_u=x_r
  166.     old=x_r
  167.     x_r=x_u-(f(x_u)*(x_l-x_u))/(f(x_l)-f(x_u))  
  168.     i+=1
  169.     #sign is not important for error
  170.     #error is high for 1st iteration
  171.     err.append(abs((x_r-old)/x_r)*100)
  172. print(x_r,i)
  173.  
  174. import matplotlib.pyplot as plt
  175. plt.plot(range(i),err,marker='o')
  176. plt.show()
  177.  
  178.  
  179. GIVEN
  180.  
  181. # -*- coding: utf-8 -*-
  182. """
  183. Created on Tue Sep  4 14:36:49 2018
  184.  
  185. @author: CSE CARNIVAL 2017
  186. """
  187.  
  188. import math
  189. def ePower(x,tol):
  190.    val=1
  191.    i=0
  192.    while True:
  193.        temp=val
  194.        i+=1;
  195.        val+=(x**i)/math.factorial(i)
  196.        if (val-temp)/val<=tol:
  197.            break
  198.        
  199.    return val,i
  200.  
  201. def ePower2(x,n):
  202.    val=1
  203.    i=0
  204.    while True:
  205.        i+=1;
  206.        val+=(x**i)/math.factorial(i)
  207.        if i==n:
  208.            break
  209.        
  210.    return val,i
  211. print ePower(0.5,5)
  212. print ePower2(0.5,5)
  213. print math.exp(0.5)
  214. x,_=ePower(0.5,0.01)
  215. y=math.exp(0.5)
  216. print ((y-x)/y*100)
  217.  
  218.  
  219.  
  220. MIXED
  221.  
  222. # -*- coding: utf-8 -*-
  223. """
  224. Created on Tue Aug 14 14:40:39 2018
  225.  
  226. @author: CSE CARNIVAL 2017
  227. """
  228.  
  229. import matplotlib.pyplot as plt
  230. import math
  231. import numpy as np
  232. x=np.linspace(0,3*math.pi,100)
  233. y=[]
  234. #for v in x:
  235.     #y.append(math.sin(v))
  236. y=[math.cos(v)*1000 for v in x]
  237. y1=[math.sin(v)*1000 for v in x]
  238. y2=[math.exp(v) for v in x]
  239.  
  240. plt.plot(x,y,color="blue")
  241. plt.plot(x,y1,color="red")
  242. plt.plot(x,y2,color="black")
  243. plt.show()
  244.  
  245.  
  246. NEWTON RAFSON
  247.  
  248. # -*- coding: utf-8 -*-
  249. """
  250. Created on Tue Sep 11 15:08:41 2018
  251.  
  252. @author: CSE CARNIVAL 2017
  253. """
  254.  
  255. import math
  256. import random
  257.  
  258. def f(x):
  259.     return math.exp(-x)-x
  260. def ff(x):
  261.     return -math.exp(-x)-1
  262.  
  263. x_i=random.random()
  264. print x_i
  265. i=0
  266. x_r=x_i-f(x_i)/ff(x_i)
  267. err=[]
  268. while(True):
  269.  
  270.     x_r=x_i-f(x_i)/ff(x_i)
  271.     err.append(abs((x_r-x_i)/x_r)*100)    
  272.     i+=1
  273.     if f(x_r)==0:
  274.         break
  275.     x_i=x_r
  276.    
  277.    
  278. print(x_r,i)
  279. import matplotlib.pyplot as plt
  280. plt.plot(range(i),err,marker='o')
  281. plt.show()
  282.  
  283.  
  284. SIN
  285. # -*- coding: utf-8 -*-
  286. """
  287. Created on Tue Aug 14 14:18:47 2018
  288.  
  289. @author: CSE CARNIVAL 2017
  290. """
  291.  
  292. import matplotlib.pyplot as plt
  293. import math
  294. import numpy as np
  295. x=np.linspace(0,2*math.pi,100)
  296. y=[]
  297. #for v in x:
  298.     #y.append(math.sin(v))
  299. y=[math.sin(v) for v in x]
  300.    
  301. plt.plot(x,y,marker='.')
  302. plt.show()
  303.  
  304.  
  305. UNTITLED
  306.  
  307. # -*- coding: utf-8 -*-
  308. """
  309. Created on Tue Sep 11 15:37:29 2018
  310.  
  311. @author: CSE CARNIVAL 2017
  312. """
  313.  
  314. # -*- coding: utf-8 -*-
  315. """
  316. Created on Tue Sep 11 15:08:41 2018
  317.  
  318. @author: CSE CARNIVAL 2017
  319. """
  320.  
  321. import math
  322. import random
  323.  
  324. def f(x):
  325.     return math.exp(-x)-x
  326. def ff(x):
  327.     return -math.exp(-x)-1
  328.  
  329. while(True):
  330.     x_l=random.random()
  331.     x_u=random.random()
  332.     if f(x_l)*f(x_u)<0:
  333.         break
  334.  
  335. #x_l=0
  336. #x_u =1
  337. i=0
  338. x_r=x_u-(f(x_u)*(x_l-x_u))/(f(x_l)-f(x_u))
  339. y=[]
  340.  
  341. while(i<20):
  342.    
  343.    
  344.     if f(x_r)==0:
  345.         break
  346.     if f(x_r)*f(x_l)>0:
  347.         x_l=x_r
  348.     if f(x_r)*f(x_u)>0:
  349.         x_u=x_r
  350.     old=x_r
  351.     x_r=x_u-(f(x_u)*(x_l-x_u))/(f(x_l)-f(x_u))  
  352.     i+=1
  353.  
  354.    
  355.     y.append(abs((x_r-old)/x_r)*100)
  356. print(x_r,i)
  357.  
  358. import matplotlib.pyplot as plt
  359. plt.plot(range(i),y,marker='*',label='FP')
  360.  
  361.  
  362. x_i=random.random()
  363. print x_i
  364. i=0
  365. x_r=x_i-f(x_i)/ff(x_i)
  366. err=[]
  367. while(True):
  368.  
  369.     x_r=x_i-f(x_i)/ff(x_i)
  370.     err.append(abs((x_r-x_i)/x_r)*100)    
  371.     i+=1
  372.     if f(x_r)==0:
  373.         break
  374.     x_i=x_r
  375.    
  376.    
  377. print(x_r,i)
  378.  
  379. plt.plot(range(i),err,marker='o',label='NR')
  380. plt.legend()
  381. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement