use the code above to find the coefficient of the polynomial of degree 25 that passes through the points {k, sin(k)} 21. Plot the given points and the graph of the resulting polynomial. Explain how you answered the problem and implemented your code and include the plot in your documen- tation. Print also the relative error produced by the given points and the polynomial values. item1.py should contain the code to find the coefficients and generate the figure. Sample plot of solution for Item 1 1.0 Given Points Polynomial 0.8 0.6 0.4 0.2 0.0 1.0 1.5 2.0 2.5 3.0 3.5 4.0 . 0.6 0.5 0.4 0.3 0.2 Sample plot solution for Item 2 Given Points Polynomial 0.0 0.2 0.4 0.6 . 0.8 10

icon
Related questions
Question
import numpy as np
 
def ForwardSubRow(L,b):
L = np.array(L,float)
b = np.array(b,float)
x = np.empty_like(b,float)
n = len(b)
for k in range(n):
if L[k,k] == 0:
print("No Solution!")
return b
x[0] = b[0]/L[0,0]
for i in range(1,n):
s = 0
for j in range(i):
s = s + L[i,j]*x[j]
x[i] = (b[i] - s)/L[i,i]
return x
 
def BackwardSubCol(U,b):
U = np.array(U,float)
b = np.array(b,float)
n = len(b)
for k in range(n):
if U[k,k] == 0:
print("No Solution!")
return b
for j in range(n-1,0,-1):
b[j] = b[j]/U[j,j]
for i in range(j):
b[i] = b[i] - U[i,j]*b[j]
b[0] = b[0]/U[0,0]
return b
 
def LUKJI(A):
A = np.array(A,float)
n = len(A)
for k in range(n):
for j in range(k+1,n):
A[j,k] = A[j,k]/A[k,k]
for j in range(k+1,n):
for i in range(k+1,n):
A[i,j] = A[i,j] - A[i,k]*A[k,j]
return A
 
def LUIJK(A):
A = np.array(A,float)
n = len(A)
for j in range(1,n):
A[j,0] = A[j,0]/A[0,0]
for i in range(1,n):
for j in range(i,n):
s = 0
for k in range(i):
s = s + A[i,k]*A[k,j]
A[i,j] = A[i,j] - s
for j in range(i+1,n):
s = 0
for k in range(i):
s = s + A[j,k]*A[k,i]
A[j,i] = (A[j,i]-s)/A[i,i]
return A
 
def GetLU(A,factor=LUKJI):
n = len(A)
A = factor(A)
L = np.zeros((n,n),float)
U = np.zeros((n,n),float)
for i in range(n):
L[i,i] = 1
for j in range(i):
L[i,j] = A[i,j]
for j in range(i,n):
U[i,j] = A[i,j]
return L, U
 
def LUSolve(A,b,factor = LUKJI,forwardsub=ForwardSubRow, backwardsub=BackwardSubCol):
L, U = GetLU(A,factor)
y = forwardsub(L,b)
return backwardsub(U,y)
 
 
if __name__ == '__main__':
A = [[4.5,5.6,6.7,7.8],
[-1,3,5,6],
[5.5,-2,4,5],
[1,2,3,4]]
b = [1,2,3,4]
# print(LUKJI(A))
# L, U = GetLU(A)
# print(f'L = {L}')
# print(f'U = {U}')
x = LUSolve(A,b)
errx = np.dot(A,x) - b
print(x)
print(errx)
 
y = LUSolve(A,b,LUIJK)
erry = np.dot(A,y) - b
print(y)
print(erry)
use the code above to find the coefficient of the polynomial of degree 25 that passes through
the points {k, sin(k)} 21. Plot the given points and the graph of the resulting polynomial. Explain
how you answered the problem and implemented your code and include the plot in your documen-
tation. Print also the relative error produced by the given points and the polynomial values.
item1.py should contain the code to find the coefficients and generate the figure.
Sample plot of solution for Item 1
1.0
Given Points
Polynomial
0.8
0.6
0.4
0.2
0.0
1.0
1.5
2.0
2.5
3.0
3.5
4.0
.
0.6
0.5
0.4
0.3
0.2
Sample plot
solution for Item 2
Given Points
Polynomial
0.0
0.2
0.4
0.6
.
0.8
10
Transcribed Image Text:use the code above to find the coefficient of the polynomial of degree 25 that passes through the points {k, sin(k)} 21. Plot the given points and the graph of the resulting polynomial. Explain how you answered the problem and implemented your code and include the plot in your documen- tation. Print also the relative error produced by the given points and the polynomial values. item1.py should contain the code to find the coefficients and generate the figure. Sample plot of solution for Item 1 1.0 Given Points Polynomial 0.8 0.6 0.4 0.2 0.0 1.0 1.5 2.0 2.5 3.0 3.5 4.0 . 0.6 0.5 0.4 0.3 0.2 Sample plot solution for Item 2 Given Points Polynomial 0.0 0.2 0.4 0.6 . 0.8 10
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer