Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Thu Apr 26 10:32:50 2018
- @author: Thomas
- """
- from scipy import *
- from pylab import *
- import sys
- import numpy as np
- import math
- import matplotlib.pyplot as plt
- class Interval:
- def __init__(self,a,*args):
- if args:
- self.a = float(a)
- self.b = float(args[0]) #If statement to make an interval from one real number
- #print(float(args[0]))
- else:
- self.a = float(a)
- self.b = float(a)
- def __repr__(self):
- return '[%g,%g]' % (self.a,self.b) #First two functions in this class for Task 1
- def __add__(self,other):
- if isinstance(other, (int,float)): #If statement for task 8
- other = Interval(other,other)
- a1 = self.a + other.a
- a2 = self.b + other.b
- else:
- a1 = self.a + other.a #Basically task 2
- a2 = self.b + other.b
- return Interval(a1, a2)
- def __radd__(self,other):
- if isinstance(other, (int,float)): #Resverse add
- other = Interval(other,other)
- return other + self
- def __sub__(self,other):
- if isinstance(other, (int,float)):
- other = Interval(other,other)
- s1 = self.a - other.b
- s2 = self.b - other.a
- else:
- s1 = self.a - other.b
- s2 = self.b - other.a
- return Interval(s1, s2)
- def __rsub__(self,other):
- if isinstance(other, (int,float)):
- other = Interval(other,other)
- return other - self
- def __mul__(self,other):
- if isinstance(other, (int,float)):
- other = Interval(other,other)
- m1 = min(self.a*other.a,self.a*other.b,self.b*other.a,self.b*other.b)
- m2 = max(self.a*other.a,self.a*other.b,self.b*other.a,self.b*other.b)
- else:
- m1 = min(self.a*other.a,self.a*other.b,self.b*other.a,self.b*other.b)
- m2 = max(self.a*other.a,self.a*other.b,self.b*other.a,self.b*other.b)
- return Interval(m1, m2)
- def __rmul__(self,other):
- if isinstance(other, (int,float)):
- other = Interval(other,other)
- return other * self
- def __pow__(self,other):
- if other % 2 == 0:
- if self.a < 0:
- p1 = 0
- p2 = max(self.b ** other, self.a ** other)
- elif self.b < 0:
- p1 = self.b ** other
- p2 = self.a ** other
- elif self.a > 0:
- p1 = self.a ** other
- p2 = self.b ** other
- else:
- p1 = self.a ** other
- p2 = self.b ** other
- else:
- p1 = self.a ** other
- p2 = self.b ** other
- return Interval(p1,p2)
- def divide(self,other):
- if other.a == 0:
- print("cannot divide by zero")
- p1 = 0
- p2 = 0
- else:
- if other.b == 0:
- print("cannot divide by zero")
- p1 = 0
- p2 = 0
- else:
- if other.a > 1000:
- print("too large value")
- p1 = 0
- p2 = 0
- else:
- if other.b > 1000:
- print("too large value")
- p1 = 0
- p2 = 0
- else:
- p1 = min(self.a/other.a,self.a/other.b,self.b/other.a,self.b/other.b)
- p2 = max(self.a/other.a,self.a/other.b,self.b/other.a,self.b/other.b)
- return p1, p2
- def __contains__(self,realnumber):
- if max(realnumber,self.b) == self.b:
- if min(realnumber,self.a) == self.a:
- print("contains")
- else:
- print("outside")
- else:
- print("outside")
- #q = Interval(1,4)
- #p = Interval(-2,-1)
- #print(q.__add__(p))
- #x = Interval(-2,2)
- #print(x**2)
- #Task 10 Thomas version
- """
- xl = np.linspace(0.,1,1000)
- xu = np.linspace(0.,1,1000) + 0.5
- Ilist = []
- yl = []
- yu = []
- for n in range(0,1000):
- Ilist.append((3*(Interval(xl[n],xu[n]))**3 ) - (2*(Interval(xl[n],xu[n]))**2) - (5*(Interval(xl[n],xu[n]))) - 1)
- yl.append(Ilist[n].a)
- yu.append(Ilist[n].b)
- plt.plot(xl,yl)
- plt.plot(xl,yu)
- """
- #Task 10 Better
- def p(I):
- return 3*I**3 - 2*I**2 - 5*I -1
- xl = np.linspace(0.,1,1000)
- xu = xl+0.5
- y=[p(Interval(xl[i], xu[i])) for i in range (len(xl))]
- yl=[y[i].a for i in range(len(y))]
- yu=[y[i].b for i in range(len(y))]
- plt.plot(xl,yl)
- plt.plot(xl,yu)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement