b_lab2

1558 days ago by derevjaginti

f(x)=sqrt(14)*exp((sin(sqrt(14)*x))) 
       
plot(f, -1.5, 1) 
       
def error(f, inter_f): print("RelError = ",abs(f-inter_f)/f) print("AbsError = ",abs(f-inter_f)) 
       

----------------LAGRANGE----------------

def lagrange(x0, i1, i2): result = 0 for i in range(i1, i2+1): tmp = 1 for j in range(i1, i2+1): if i != j : tmp *= (x0-N(j/10))/(N(i/10)-N(j/10)) result += N(f(N(i/10)))*tmp return result 
       
def where_x(x0): for i in range(-15,11): if x0<i/10: return N(i-1) 
       
def chose_bounds(x0, i1, i2, state): if i1 < -14 and i2 < 10 or state%3 == 0 and i2 < 10: return i1, i2+1.0 elif i1 > -15 and i2 > 9 or state%3 == 1: return i1-1, i2 else: return i1-1, i2+1 
       
def min_lagrange(x0): min = 100000000 i1 = where_x(x0) i2 = i1 for i in range(10000): i1, i2 = chose_bounds(x0, i1, i2, i) if i1<-15 or i2>10: return "Can't find" l = lagrange(x0, i1, i2) print(i1, i2, l, i) if (abs(l-min)<EPS): return l min = l return l 
       

-------------------SPLINE---------------------

def omega (x, j): result = 1 i = where_x(x) if i-j == 1: result *= (x-N((j+1)/10))/(N(j/10)-N((j+1)/10)) result *= (x-N((j+2)/10))/(N(j/10)-N((j+2)/10)) elif i-j == 0: result *= (x-N((j+1)/10))/(N(j/10)-N((j+1)/10)) result *= (x-N((j-1)/10))/(N(j/10)-N((j-1)/10)) else: result *= (x-N((j-1)/10))/(N(j/10)-N((j-1)/10)) result *= (x-N((j-2)/10))/(N(j/10)-N((j-2)/10)) return result 
       
def spline (x): i = where_x(x) result = N(f(N((i-1)/10)))*omega(x,i-1) result += N(f(N((i+1)/10)))*omega(x,i+1) result += N(f(N(i/10)))*omega(x,i)