213 - A02.5 - Lagrange Polynomial Application

24 days ago by Professor213

John Travis

Mississippi College

 

For the following, enter data for x values in the list xs and corresponding y-values in the list ys.  Watch as the linear system is created and solved so that the resulting interpolating polynomial is created and graphed.

xs = (-1,0,1,2,4) ys = (4,-1,0,3,-5) n = len(xs) pts = [] for k in range(n): pts.append((xs[k],ys[k])) show(pts) A = zero_matrix(QQ,n,n+1) for k in range(n): u = xs[k] for j in range(n): A[k,j] = u^j A[k,n]=ys[k] D = A.echelon_form() show(A) show(D) G = points(pts,color='red',size=20) f = D[0][5]+D[1][5]*x+D[2][5]*x^2+D[3][5]*x^3+D[4][5]*x^4 pretty_print(html("$ f(x) = %s$"%str(latex(f)))) G += plot(f,(x,min(xs)-0.5,max(xs)+0.5)) show(G) 
       

                                
                            

                                
# as an interactive cell reset() @ interact def _(xs = input_box(default=(-1,0,1,2,4)), ys = input_box(default=(4,-1,0,3,-5))): n = len(xs) pts = [] for k in range(n): pts.append((xs[k],ys[k])) # show(pts) A = zero_matrix(QQ,n,n+1) for k in range(n): u = xs[k] for j in range(n): A[k,j] = u^j A[k,n]=ys[k] D = A.echelon_form() show(A) # show(D) G = points(pts,color='red',size=20) f = 0 for k in range(n): f += D[k][n]*x^k pretty_print(html("$ f(x) = %s$"%str(latex(f)))) G += plot(f,(x,min(xs)-0.5,max(xs)+0.5)) show(G) 
       
xs 
ys 

Click to the left again to hide and once more to show the dynamic interactive window