简体   繁体   中英

Plot a lagrange polynomial from a set of points PYTHON

I need to draw a curve passing through the points (0,0) , (20,10) , (0,50). As there two points with the same x coordinate, I decided to parametrize x and y with a parameter t and since I need the equation of this polynomial (of the curve) I decided tot use Lagrange. Well, the plot is really weird, it prints the points, but there is no curve: 怪异的情节

x_parabola = np.array([0, 20, 0]) # array for x
y_parabola = np.array([0, 10, 50]) # array for y
plt.figure()
u = plt.plot(x_parabola,y_parabola,'ro') # plot the points
t = np.linspace(0, 1, len(x_parabola)) # parameter t to parametrize x and y
pxLagrange = scipy.interpolate.lagrange(t, x) # X(T)
pyLagrange = scipy.interpolate.lagrange(t, y) # Y(T)
n = 100
ts = np.linspace(t[0],t[-1],n)
xLagrange = pxLagrange(ts) # lagrange x coordinates
yLagrange = pyLagrange(ts) # lagrange y coordinates
plt.plot(xLagrange, yLagrange,'b-',label = "Polynomial")

Please, could you show me what is wrong with the code? Thanks in advance!

You have to redefine variables.This works

import numpy as np
import scipy.interpolate
import matplotlib.pyplot as plt

x_parabola = np.array([0, 20, 0]) # array for x
y_parabola = np.array([0, 10, 50]) # array for y
plt.figure()
u = plt.plot(x_parabola,y_parabola,'ro') # plot the points
t = np.linspace(0, 1, len(x_parabola)) # parameter t to parametrize x and y
pxLagrange = scipy.interpolate.lagrange(t, x_parabola) # X(T)
pyLagrange = scipy.interpolate.lagrange(t, y_parabola) # Y(T)
n = 100
ts = np.linspace(t[0],t[-1],n)
xLagrange = pxLagrange(ts) # lagrange x coordinates
yLagrange = pyLagrange(ts) # lagrange y coordinates
plt.plot(xLagrange, yLagrange,'b-',label = "Polynomial")
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM