Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- BISECTION
- import math
- import random
- def f(x):
- return math.exp(-x) - x
- while(1):
- a = random.random()
- b = random.random()
- if f(a)*f(b) < 0:
- break
- x = []
- y = []
- i = 0
- n = 20
- c = (a+b)/2
- while(i < n):
- if f(c) == 0:
- break
- if f(c)*f(a) > 0:
- a = c
- if f(c)*f(b) > 0:
- b = c
- temp = c
- c = (a+b)/2
- #Difference of current c and previous c
- #Error approximation
- y.append(abs((c-temp)/c)*100)
- i+=1
- print c, f(c)
- import matplotlib.pyplot as plt
- plt.plot(range(1,n+1),y,marker = 'o')
- plt.show()
- POWER
- # -*- coding: wtf-8 -*-
- """
- Created on Tue Sep 4 14:06:16 2018
- @author: CSE CARNIVAL 2017
- """
- import math
- def ePower(x,tol):
- val=1
- i=0
- while True:
- temp=val
- i+=1;
- val+=(x**i)/math.factorial(i)
- if (val-temp)/val<=tol:
- break
- return val,i
- print ePower(5,0.01)
- print math.exp(0.5)
- EXPONENTIAL
- # -*- coding: utf-8 -*-
- """
- Created on Sun Sep 9 14:24:32 2018
- @author: CSE CARNIVAL 2017
- """
- """
- e^x - exponential
- """
- #Loop until a fixed tolerance of error
- import numpy as np
- import math
- def myexp(x, tol):
- s = 1.
- i = 1
- while(1):
- s_p = s
- s += (x**i)/math.factorial(i)
- err = (s-s_p)/s*100
- if(err < tol):
- break
- i+=1
- return s,i
- #Loop until a fixed number of iterations
- def myexp2(x, n):
- s = 1.
- i = 1
- while(1):
- s_p = s
- s += (x**i)/math.factorial(i)
- err = (s-s_p)/s*100
- if(i >= n):
- break
- i+=1
- return s
- print myexp(0.5,0.0000001)
- print myexp2(0.5,10)
- print math.exp(0.5)
- true_val = math.exp(0.5)
- y = []
- x = range(10)
- y= []
- for i in x:
- approx_val = myexp2(0.5,i)
- true_err = (true_val - approx_val)/true_val*100
- y.append(true_err)
- #Same shit using list comprehension
- #y = [(true_val - myexp2(0.5,i))/true_val*100 for i in x]
- import matplotlib.pyplot as plt
- plt.plot(x,y,marker='o')
- plt.show()
- FALSE POSITION
- # -*- coding: utf-8 -*-
- """
- Created on Tue Sep 11 14:12:56 2018
- @author: CSE CARNIVAL 2017
- """
- import math
- import random
- def f(x):
- return math.exp(-x)-x
- while(True):
- x_l=random.random()
- x_u=random.random()
- if f(x_l)*f(x_u)<0:
- break
- #x_l=0
- #x_u =1
- i=0
- x_r=x_u-(f(x_u)*(x_l-x_u))/(f(x_l)-f(x_u))
- err=[]
- while(i<20):
- if f(x_r)==0:
- break
- if f(x_r)*f(x_l)>0:
- x_l=x_r
- if f(x_r)*f(x_u)>0:
- x_u=x_r
- old=x_r
- x_r=x_u-(f(x_u)*(x_l-x_u))/(f(x_l)-f(x_u))
- i+=1
- #sign is not important for error
- #error is high for 1st iteration
- err.append(abs((x_r-old)/x_r)*100)
- print(x_r,i)
- import matplotlib.pyplot as plt
- plt.plot(range(i),err,marker='o')
- plt.show()
- GIVEN
- # -*- coding: utf-8 -*-
- """
- Created on Tue Sep 4 14:36:49 2018
- @author: CSE CARNIVAL 2017
- """
- import math
- def ePower(x,tol):
- val=1
- i=0
- while True:
- temp=val
- i+=1;
- val+=(x**i)/math.factorial(i)
- if (val-temp)/val<=tol:
- break
- return val,i
- def ePower2(x,n):
- val=1
- i=0
- while True:
- i+=1;
- val+=(x**i)/math.factorial(i)
- if i==n:
- break
- return val,i
- print ePower(0.5,5)
- print ePower2(0.5,5)
- print math.exp(0.5)
- x,_=ePower(0.5,0.01)
- y=math.exp(0.5)
- print ((y-x)/y*100)
- MIXED
- # -*- coding: utf-8 -*-
- """
- Created on Tue Aug 14 14:40:39 2018
- @author: CSE CARNIVAL 2017
- """
- import matplotlib.pyplot as plt
- import math
- import numpy as np
- x=np.linspace(0,3*math.pi,100)
- y=[]
- #for v in x:
- #y.append(math.sin(v))
- y=[math.cos(v)*1000 for v in x]
- y1=[math.sin(v)*1000 for v in x]
- y2=[math.exp(v) for v in x]
- plt.plot(x,y,color="blue")
- plt.plot(x,y1,color="red")
- plt.plot(x,y2,color="black")
- plt.show()
- NEWTON RAFSON
- # -*- coding: utf-8 -*-
- """
- Created on Tue Sep 11 15:08:41 2018
- @author: CSE CARNIVAL 2017
- """
- import math
- import random
- def f(x):
- return math.exp(-x)-x
- def ff(x):
- return -math.exp(-x)-1
- x_i=random.random()
- print x_i
- i=0
- x_r=x_i-f(x_i)/ff(x_i)
- err=[]
- while(True):
- x_r=x_i-f(x_i)/ff(x_i)
- err.append(abs((x_r-x_i)/x_r)*100)
- i+=1
- if f(x_r)==0:
- break
- x_i=x_r
- print(x_r,i)
- import matplotlib.pyplot as plt
- plt.plot(range(i),err,marker='o')
- plt.show()
- SIN
- # -*- coding: utf-8 -*-
- """
- Created on Tue Aug 14 14:18:47 2018
- @author: CSE CARNIVAL 2017
- """
- import matplotlib.pyplot as plt
- import math
- import numpy as np
- x=np.linspace(0,2*math.pi,100)
- y=[]
- #for v in x:
- #y.append(math.sin(v))
- y=[math.sin(v) for v in x]
- plt.plot(x,y,marker='.')
- plt.show()
- UNTITLED
- # -*- coding: utf-8 -*-
- """
- Created on Tue Sep 11 15:37:29 2018
- @author: CSE CARNIVAL 2017
- """
- # -*- coding: utf-8 -*-
- """
- Created on Tue Sep 11 15:08:41 2018
- @author: CSE CARNIVAL 2017
- """
- import math
- import random
- def f(x):
- return math.exp(-x)-x
- def ff(x):
- return -math.exp(-x)-1
- while(True):
- x_l=random.random()
- x_u=random.random()
- if f(x_l)*f(x_u)<0:
- break
- #x_l=0
- #x_u =1
- i=0
- x_r=x_u-(f(x_u)*(x_l-x_u))/(f(x_l)-f(x_u))
- y=[]
- while(i<20):
- if f(x_r)==0:
- break
- if f(x_r)*f(x_l)>0:
- x_l=x_r
- if f(x_r)*f(x_u)>0:
- x_u=x_r
- old=x_r
- x_r=x_u-(f(x_u)*(x_l-x_u))/(f(x_l)-f(x_u))
- i+=1
- y.append(abs((x_r-old)/x_r)*100)
- print(x_r,i)
- import matplotlib.pyplot as plt
- plt.plot(range(i),y,marker='*',label='FP')
- x_i=random.random()
- print x_i
- i=0
- x_r=x_i-f(x_i)/ff(x_i)
- err=[]
- while(True):
- x_r=x_i-f(x_i)/ff(x_i)
- err.append(abs((x_r-x_i)/x_r)*100)
- i+=1
- if f(x_r)==0:
- break
- x_i=x_r
- print(x_r,i)
- plt.plot(range(i),err,marker='o',label='NR')
- plt.legend()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement