Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import math as mt
- ######TASK 1
- #nodes = int(input("Enter the number of nodes: "))
- ### FUNCTION TO BE INTERPOLATED
- def myFunction(x):
- return x**10 + x**4 + 3*x**2 - 2*x + 1
- ### TASK 1 & 2
- ### set chebyshev to true to use chebyshev nodes
- def interpolation(nodes, a, b, point, chebyshev=False):
- step = (b-a)/(nodes-1)
- Matrix = np.zeros([3,nodes])
- if chebyshev == False:
- for i in range(0, nodes):
- Matrix[0][i] = a + i*step
- Matrix[1][i] = myFunction(Matrix[0][i])
- else:
- for i in range(0,nodes):
- xi = mt.cos(mt.pi * (2*i + 1) / (2*nodes + 2))
- zi = 0.5*((b-a)*xi + a + b)
- Matrix[0][i] = zi
- Matrix[1][i] = myFunction(Matrix[0][i])
- for i in range(0,nodes):
- nominator = 1
- denominator = 1
- for n, k in enumerate(Matrix[0]):
- if n != i:
- val = (point-k)
- nominator *= val
- val = (Matrix[0][i]-k)
- denominator *= val
- Matrix[2][i] = nominator/denominator * Matrix[1][i]
- return sum(Matrix[2])
- ### TASK 3
- def maxError(a, b, step=0.01):
- maxim = abs(interpolation(31, a, b, a, True) - myFunction(a))
- maxIndex = a
- for i in np.arange(a+step, b, step):
- val = abs(interpolation(31, a, b, i, True) - myFunction(i))
- if val > maxim:
- maxim = val
- maxIndex = i
- return maxIndex, maxim
- ### TESTS
- #print(interpolation(5,0,6.28,mt.pi/6))
- #print(interpolation(31, -5, 5, 7, True))
- #print(interpolation(31, -5, 5, 7, False))
- #print(myFunction(7))
- print(myFunction(7))
- print(interpolation(31, 0, 8, 7, True))
- ind, valueMax = maxError(-30.0, 30.0)
- print(ind, valueMax)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement