Advertisement
Guest User

Untitled

a guest
May 20th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Thu Apr 26 10:32:50 2018
  4.  
  5. @author: Thomas
  6. """
  7.  
  8. from scipy import *
  9. from pylab import *
  10. import sys
  11. import numpy as np
  12. import math
  13. import matplotlib.pyplot as plt
  14.  
  15. class Interval:
  16. def __init__(self,a,*args):
  17. if args:
  18. self.a = float(a)
  19. self.b = float(args[0]) #If statement to make an interval from one real number
  20. #print(float(args[0]))
  21. else:
  22. self.a = float(a)
  23. self.b = float(a)
  24.  
  25. def __repr__(self):
  26. return '[%g,%g]' % (self.a,self.b) #First two functions in this class for Task 1
  27.  
  28. def __add__(self,other):
  29. if isinstance(other, (int,float)): #If statement for task 8
  30. other = Interval(other,other)
  31. a1 = self.a + other.a
  32. a2 = self.b + other.b
  33. else:
  34. a1 = self.a + other.a #Basically task 2
  35. a2 = self.b + other.b
  36. return Interval(a1, a2)
  37.  
  38. def __radd__(self,other):
  39. if isinstance(other, (int,float)): #Resverse add
  40. other = Interval(other,other)
  41. return other + self
  42.  
  43. def __sub__(self,other):
  44. if isinstance(other, (int,float)):
  45. other = Interval(other,other)
  46. s1 = self.a - other.b
  47. s2 = self.b - other.a
  48. else:
  49. s1 = self.a - other.b
  50. s2 = self.b - other.a
  51. return Interval(s1, s2)
  52.  
  53. def __rsub__(self,other):
  54. if isinstance(other, (int,float)):
  55. other = Interval(other,other)
  56. return other - self
  57.  
  58. def __mul__(self,other):
  59. if isinstance(other, (int,float)):
  60. other = Interval(other,other)
  61. m1 = min(self.a*other.a,self.a*other.b,self.b*other.a,self.b*other.b)
  62. m2 = max(self.a*other.a,self.a*other.b,self.b*other.a,self.b*other.b)
  63. else:
  64. m1 = min(self.a*other.a,self.a*other.b,self.b*other.a,self.b*other.b)
  65. m2 = max(self.a*other.a,self.a*other.b,self.b*other.a,self.b*other.b)
  66. return Interval(m1, m2)
  67.  
  68. def __rmul__(self,other):
  69. if isinstance(other, (int,float)):
  70. other = Interval(other,other)
  71. return other * self
  72.  
  73. def __pow__(self,other):
  74. if other % 2 == 0:
  75. if self.a < 0:
  76. p1 = 0
  77. p2 = max(self.b ** other, self.a ** other)
  78. elif self.b < 0:
  79. p1 = self.b ** other
  80. p2 = self.a ** other
  81. elif self.a > 0:
  82. p1 = self.a ** other
  83. p2 = self.b ** other
  84. else:
  85. p1 = self.a ** other
  86. p2 = self.b ** other
  87. else:
  88. p1 = self.a ** other
  89. p2 = self.b ** other
  90. return Interval(p1,p2)
  91.  
  92. def divide(self,other):
  93. if other.a == 0:
  94. print("cannot divide by zero")
  95. p1 = 0
  96. p2 = 0
  97. else:
  98. if other.b == 0:
  99. print("cannot divide by zero")
  100. p1 = 0
  101. p2 = 0
  102. else:
  103. if other.a > 1000:
  104. print("too large value")
  105. p1 = 0
  106. p2 = 0
  107. else:
  108. if other.b > 1000:
  109. print("too large value")
  110. p1 = 0
  111. p2 = 0
  112. else:
  113. p1 = min(self.a/other.a,self.a/other.b,self.b/other.a,self.b/other.b)
  114. p2 = max(self.a/other.a,self.a/other.b,self.b/other.a,self.b/other.b)
  115. return p1, p2
  116.  
  117. def __contains__(self,realnumber):
  118. if max(realnumber,self.b) == self.b:
  119. if min(realnumber,self.a) == self.a:
  120. print("contains")
  121. else:
  122. print("outside")
  123. else:
  124. print("outside")
  125.  
  126. #q = Interval(1,4)
  127. #p = Interval(-2,-1)
  128. #print(q.__add__(p))
  129. #x = Interval(-2,2)
  130. #print(x**2)
  131.  
  132.  
  133. #Task 10 Thomas version
  134. """
  135. xl = np.linspace(0.,1,1000)
  136. xu = np.linspace(0.,1,1000) + 0.5
  137.  
  138. Ilist = []
  139. yl = []
  140. yu = []
  141.  
  142. for n in range(0,1000):
  143. Ilist.append((3*(Interval(xl[n],xu[n]))**3 ) - (2*(Interval(xl[n],xu[n]))**2) - (5*(Interval(xl[n],xu[n]))) - 1)
  144. yl.append(Ilist[n].a)
  145. yu.append(Ilist[n].b)
  146.  
  147. plt.plot(xl,yl)
  148. plt.plot(xl,yu)
  149. """
  150.  
  151. #Task 10 Better
  152. def p(I):
  153. return 3*I**3 - 2*I**2 - 5*I -1
  154.  
  155. xl = np.linspace(0.,1,1000)
  156. xu = xl+0.5
  157. y=[p(Interval(xl[i], xu[i])) for i in range (len(xl))]
  158. yl=[y[i].a for i in range(len(y))]
  159. yu=[y[i].b for i in range(len(y))]
  160. plt.plot(xl,yl)
  161. plt.plot(xl,yu)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement