CoCalc Shared Files6_7_3.sagews
Author: Georgy Lazarev
Views : 7
def func(x):
return 1/(1+2*abs(x))
a=-2
b=2
plot(func(x),(x,-2,2))

x_l=[]
x_l2=[]
y_l=[]
y_l2=[]
xn1=0
xn2=0
n1=17
for k in range(17):
xn1=-2+(0.25)*k
x_l.append((xn1).n())
y_l.append(func(xn1.n()))
x_l
len(x_l)
n2=n1*3
for k in range(n2):
xn2=-2+(0.08)*k
x_l2.append(xn2.n())
y_l2.append(func(xn2).n())
x_l2
def lin(x1,x2,y1,y2,x):
return ((x-x1)/(x2-x1))*(y2-y1)+y1
def for_lin(x):
for j in range(len(x_l)):
if x>x_l[j]:
return lin(x_l[j],x_l[j+1],y_l[j],y_l[j+1],x)
s=0
def inter(xl,yl,n,t):
f=matrix(RR,n+1)
for i in range(n+1):
f[i,0]=yl[i]
for k in range(1,n+1):
for i in range(n+1-k):
f[i,k]=(f[i+1,k-1]-f[i,k-1])/(xl[i+k]-xl[i])
s=yl[0]
for k in range(1,n+1):
r=1
for i in range(k):
r=r*(t-xl[i])
s+=f[0,k]*r
return s

interlist = []
for i in range(len(x_l2)):
t=-2+0.08*i
t.n()
interlist.append(inter(x_l,y_l,n1-1,t))

[-2.00000000000000, -1.75000000000000, -1.50000000000000, -1.25000000000000, -1.00000000000000, -0.750000000000000, -0.500000000000000, -0.250000000000000, 0.000000000000000, 0.250000000000000, 0.500000000000000, 0.750000000000000, 1.00000000000000, 1.25000000000000, 1.50000000000000, 1.75000000000000, 2.00000000000000] 17 [-2.00000000000000, -1.92000000000000, -1.84000000000000, -1.76000000000000, -1.68000000000000, -1.60000000000000, -1.52000000000000, -1.44000000000000, -1.36000000000000, -1.28000000000000, -1.20000000000000, -1.12000000000000, -1.04000000000000, -0.960000000000000, -0.880000000000000, -0.800000000000000, -0.720000000000000, -0.640000000000000, -0.560000000000000, -0.480000000000000, -0.400000000000000, -0.320000000000000, -0.240000000000000, -0.160000000000000, -0.0800000000000001, 0.000000000000000, 0.0800000000000001, 0.160000000000000, 0.240000000000000, 0.320000000000000, 0.400000000000000, 0.480000000000000, 0.560000000000000, 0.640000000000000, 0.720000000000000, 0.800000000000000, 0.880000000000000, 0.960000000000000, 1.04000000000000, 1.12000000000000, 1.20000000000000, 1.28000000000000, 1.36000000000000, 1.44000000000000, 1.52000000000000, 1.60000000000000, 1.68000000000000, 1.76000000000000, 1.84000000000000, 1.92000000000000, 2.00000000000000] -2.00000000000000 -1.92000000000000 -1.84000000000000 -1.76000000000000 -1.68000000000000 -1.60000000000000 -1.52000000000000 -1.44000000000000 -1.36000000000000 -1.28000000000000 -1.20000000000000 -1.12000000000000 -1.04000000000000 -0.960000000000000 -0.880000000000000 -0.800000000000000 -0.720000000000000 -0.640000000000000 -0.560000000000000 -0.480000000000000 -0.400000000000000 -0.320000000000000 -0.240000000000000 -0.160000000000000 -0.0800000000000001 0.000000000000000 0.0800000000000001 0.160000000000000 0.240000000000000 0.320000000000000 0.400000000000000 0.480000000000000 0.560000000000000 0.640000000000000 0.720000000000000 0.800000000000000 0.880000000000000 0.960000000000000 1.04000000000000 1.12000000000000 1.20000000000000 1.28000000000000 1.36000000000000 1.44000000000000 1.52000000000000 1.60000000000000 1.68000000000000 1.76000000000000 1.84000000000000 1.92000000000000 2.00000000000000


y_l
y_l2

[0.200000000000000, 0.222222222222222, 0.250000000000000, 0.285714285714286, 0.333333333333333, 0.400000000000000, 0.500000000000000, 0.666666666666667, 1.00000000000000, 0.666666666666667, 0.500000000000000, 0.400000000000000, 0.333333333333333, 0.285714285714286, 0.250000000000000, 0.222222222222222, 0.200000000000000] [0.200000000000000, 0.206611570247934, 0.213675213675214, 0.221238938053097, 0.229357798165138, 0.238095238095238, 0.247524752475248, 0.257731958762887, 0.268817204301075, 0.280898876404494, 0.294117647058824, 0.308641975308642, 0.324675324675325, 0.342465753424658, 0.362318840579710, 0.384615384615385, 0.409836065573771, 0.438596491228070, 0.471698113207547, 0.510204081632653, 0.555555555555556, 0.609756097560976, 0.675675675675676, 0.757575757575758, 0.862068965517241, 1.00000000000000, 0.862068965517241, 0.757575757575757, 0.675675675675675, 0.609756097560976, 0.555555555555556, 0.510204081632653, 0.471698113207547, 0.438596491228070, 0.409836065573770, 0.384615384615385, 0.362318840579710, 0.342465753424658, 0.324675324675325, 0.308641975308642, 0.294117647058823, 0.280898876404494, 0.268817204301075, 0.257731958762887, 0.247524752475248, 0.238095238095238, 0.229357798165138, 0.221238938053097, 0.213675213675214, 0.206611570247934, 0.200000000000000]
n2

for j in range(len(x_l2)):
t=-2+j*(0.08)
t
inter(x_l2,y_l2,n2-1,t)

51 -2.00000000000000 0.200000000000000 -1.92000000000000 0.206611570247934 -1.84000000000000 0.213675213675214 -1.76000000000000 0.221238938053097 -1.68000000000000 0.229357798165138 -1.60000000000000 0.238095238095238 -1.52000000000000 0.247524752475247 -1.44000000000000 0.257731958762887 -1.36000000000000 0.268817204301075 -1.28000000000000 0.280898876404494 -1.20000000000000 0.294117647058823 -1.12000000000000 0.308641975308642 -1.04000000000000 0.324675324675325 -0.960000000000000 0.342465753424657 -0.880000000000000 0.362318840579710 -0.800000000000000 0.384615384615385 -0.720000000000000 0.409836065573771 -0.640000000000000 0.438596491228070 -0.560000000000000 0.471698113207547 -0.480000000000000 0.510204081632653 -0.400000000000000 0.555555555555556 -0.320000000000000 0.609756097560976 -0.240000000000000 0.675675675675676 -0.160000000000000 0.757575757575758 -0.0800000000000001 0.862068965517242 0.000000000000000 1.00000000000000 0.0800000000000001 0.862068965517241 0.160000000000000 0.757575757575760 0.240000000000000 0.675675675675720 0.320000000000000 0.609756097560876 0.400000000000000 0.555555555554747 0.480000000000000 0.510204081656411 0.560000000000000 0.471698113920866 0.640000000000000 0.438596493797377 0.720000000000000 0.409836057573557 0.800000000000000 0.384615486487746 0.880000000000000 0.362316429615021 0.960000000000000 0.342473506927490 1.04000000000000 0.324748218059540 1.12000000000000 0.309021711349487 1.20000000000000 0.295476436614990 1.28000000000000 0.292613983154297 1.36000000000000 0.236015319824219 1.44000000000000 0.493263244628906 1.52000000000000 1.72489929199219 1.60000000000000 4.40856933593750 1.68000000000000 64.4486083984375 1.76000000000000 14.9992675781250 1.84000000000000 484.848144531250 1.92000000000000 2567.78222656250 2.00000000000000 2773.57812500000

# interlist: points
interlistpoints = []
l2points = []

for i in range(len(interlist)):
interlistpoints.append((x_l2[i], interlist[i]))
l2points.append((x_l2[i], y_l2[i]))
for k in range(len(interlistpoints)):
if interlistpoints[k][1] < -5:
interlistpoints[k] = (0, 0)
l2points[k] = (0, 0)

show(points(interlistpoints, rgbcolor=(0.2, 0.1, 0.1), pointsize=15) + points(l2points, rgbcolor=(0.7, 0.1, 0.1), pointsize=15) + plot(func(x),(x,-2,2)), aspect_ratio='automatic')