Advertisement
matbiz01

Untitled

Mar 24th, 2021
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.16 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2.  
  3. from itertools import zip_longest
  4. from math import sin, cos
  5. from math import pi
  6. x0 = 1/3
  7. xn = 3.0
  8. n = 300
  9.  
  10.  
  11. def getOrigValues(x):
  12.     return x * sin(3 * pi / x)
  13.  
  14.  
  15. def calculateBasicPoints(n, czebyszew):
  16.     points = []
  17.     interval = (xn - x0) / (n - 1)
  18.     for x in range(n):
  19.         if czebyszew:
  20.             currX = 1/2 * (xn + x0) + 1/2 * (xn - x0) * cos((2 * (x + 1) - 1) / 2 / n * pi)
  21.         else:
  22.             currX = x0 + x * interval
  23.         points.append([currX, getOrigValues(currX)])
  24.     return points
  25.  
  26. '''
  27. def makePyramid(basePoints):
  28.    xVals = [x for x,y in basePoints]
  29.    pyramid = []
  30.    pyramid.append(basePoints[0])
  31.    pyramid.append(basePoints[1])
  32.    pyramid[1].append((basePoints[1][1] - basePoints[0][1]) / (basePoints[1][0] - basePoints[0][0]))
  33.    for x in range(2, n):
  34.        pyramid.append(basePoints[x])
  35.        for y in range(x):
  36.            pyramid[x].append((pyramid[x][y + 1] - pyramid[x - 1][y + 1]) / (xVals[x] - xVals[x - (y + 1)])) #no idea how, but this works
  37.    coefficients = []
  38.    for x in range(n):
  39.        coefficients.append(pyramid[x][x + 1])
  40.    return coefficients
  41. basePoints = calculateBasicPoints(n, 1)
  42. xVals = [x for x,y in basePoints]
  43. coefficients = makePyramid(basePoints)
  44.  
  45. def getNewtonVal(coefficients, x, xVals):
  46.    points = list(reversed(xVals))
  47.    coeffs = list(reversed(coefficients))
  48.    total = coeffs[0]
  49.    for i in range(1, len(coeffs)):
  50.        total *= (x - points[i])
  51.        total += coeffs[i]
  52.    return total
  53.  
  54.  
  55. print(getNewtonVal(coefficients, 2, xVals))
  56.  
  57.  
  58.  
  59.  
  60. point_count = 500
  61. pointX = []
  62. pointY = []
  63. pointYorig = []
  64. interval = (xn - x0) / point_count
  65. for x in range(point_count):
  66.    currX = x0 + x * interval
  67.    pointX.append(currX)
  68.    pointY.append(getNewtonVal(coefficients, currX, xVals))
  69.    pointYorig.append(getOrigValues(currX))
  70.  
  71.  
  72.  
  73. plt.plot(pointX, pointY, label="wielomian interpolujący")
  74. plt.plot(pointX, pointYorig, label="oryginalna funkcja")
  75. for point in calculateBasicPoints(n, 1):
  76.    plt.scatter(point[0], point[1], s=50)
  77. plt.legend(loc=1)
  78. plt.show()
  79.  
  80. '''
  81.  
  82.  
  83. #larange stuff
  84.  
  85. class LarangeTerm:
  86.     def __init__(self, yCoeff, denominator, xVals):
  87.         self.yCoeff  = yCoeff
  88.         self.denominator = denominator
  89.         self.xVals = xVals
  90.     def getValAt(self, x):
  91.         total = self.yCoeff / self.denominator
  92.         for xVal in self.xVals:
  93.             total *= (x - xVal)
  94.         return total
  95.     def print(self):
  96.         toPrint = ""
  97.         toPrint += str(self.yCoeff)
  98.         for x in self.xVals:
  99.             toPrint += "(x - " + str(x) + ")"
  100.         toPrint += "/" + str(self.denominator)
  101.         print(toPrint)
  102.  
  103. def lagrangeDenominators(z, xVals):
  104.     denominators = []
  105.     for n in range(z):
  106.         baseX = xVals[n]
  107.         total = 1
  108.         for i in range(len(xVals)):
  109.             if n != i:
  110.                 total *= (baseX - xVals[i])
  111.         denominators.append(total)
  112.     return denominators
  113.  
  114. def makeLarTerms(n):
  115.     xVals = [x for x, y in calculateBasicPoints(n, 1)]
  116.     yVals = [y for x, y in calculateBasicPoints(n, 1)]
  117.     LarTerms = []
  118.     lagrDenoms = lagrangeDenominators(n, xVals)
  119.     xToLar = []
  120.     for k in range(n):
  121.         for i in range(n):
  122.             if i != k:
  123.                 xToLar.append(xVals[i])
  124.         LarTerms.append(LarangeTerm(yVals[k], lagrDenoms[k], xToLar))
  125.         xToLar = []
  126.     return LarTerms
  127.  
  128. def lagrValAt(x, larTerms):
  129.     total = 0
  130.     for term in larTerms:
  131.         total += term.getValAt(x)
  132.     return total
  133.  
  134. def draw():
  135.     lart = makeLarTerms(n)
  136.     point_count = 500
  137.     pointX = []
  138.     pointY = []
  139.     pointYorig = []
  140.     interval = (xn - x0) / point_count
  141.     for x in range(point_count):
  142.         currX = x0 + x * interval
  143.         pointX.append(currX)
  144.         pointY.append(lagrValAt(currX, lart))
  145.         pointYorig.append(getOrigValues(currX))
  146.  
  147.  
  148.     plt.plot(pointX, pointY, label="wielomian interpolujący")
  149.     plt.plot(pointX, pointYorig, label="oryginalna funkcja")
  150.     for point in calculateBasicPoints(n, 1):
  151.         plt.scatter(point[0], point[1], s=50)
  152.     plt.legend(loc=1)
  153.     plt.show()
  154.    
  155. draw()
  156.  
  157.  
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement