Advertisement
TheDoskz

eps

Jun 1st, 2021
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. from math import log
  2.  
  3.  
  4. def tailor(x):
  5.     a_n=float(-1.0*pow(x,2))/4.0
  6.     n=1
  7.     sum=a_n
  8.     eps = pow(10,-6)
  9.     '''
  10.    an+1=an*qn
  11.    '''
  12.     while True:
  13.         q_n = (-1.0*x*x*n)/((n+1.0) * (2.0*n+1.0) * (2.0*n+2.0))
  14.         a_n1=a_n*q_n
  15.         r=abs(a_n1-a_n)
  16.         sum+=a_n1
  17.         a_n=a_n1
  18.         n+=1
  19.         if r<eps:
  20.             break
  21.     return sum
  22.            
  23.  
  24. def fun(x):
  25.     return 0.577215+log(x)+tailor(x)
  26.            
  27. def lagran(x, fi, xi):
  28.     sum=0
  29.     for i in range(len(xi)):
  30.         p=1
  31.         for j in range(len(xi)):
  32.             if i != j:
  33.                 p*=(x-xi[j])/(xi[i]-xi[j])
  34.         sum+=fi[i]*p
  35.     return sum
  36.  
  37.  
  38. import numpy as np
  39.  
  40.            
  41. print("Tailor")
  42. a=0.4
  43. b=4.0
  44.  
  45.  
  46.  
  47. print("max esp")
  48.  
  49. for l in np.arange(25,300,10):
  50.   n=l
  51.   h=b/n
  52.   i=a
  53.   f=[]
  54.   x=[]
  55.   # print("x\tf(x)")
  56.   while i<=b+h:
  57.       x.append(i)
  58.       f.append(fun(i))
  59.       i+=h
  60.   # for i,j in zip(f,x):
  61.       # print(f"{j:.1f}\t{i}")
  62.  
  63.   # print("\nLagrang")
  64.   # print("x\tL(x)\t\t\tf(x)\t\t\tesp")
  65.   e_max=0
  66.   j=a+0.1
  67.   while j<=b:
  68.       L=lagran(j,f,x)
  69.       fu=fun(j)
  70.       e=abs(fu-L)
  71.       if e>e_max:
  72.           e_max=e
  73.       # print(f"{j:.1f}\t{L}\t{fu}\t{e}")
  74.       j+=h
  75.   print(f"{n}\t{e_max}")
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement