'I can't seem to model my data to a cubic function

I've recently been trying to model my data according to separate equations. This function should take some data (xdata and ydata) and fit it as best it can to a cubic equation, but it seems to be giving the wrong answers, and I'm not sure where I've gone wrong. For example when given xdata from 1 to 4 and the ydata of the first 5 4 cubes, it returns:

(array([[ 1.00000000e+00, -2.17381668e-12, -2.20122021e-12, -2.16174470e-12]]))

Here's my code:

import numpy as np
import scipy as sp

from scipy import optimize

xdata_string = input("Enter your values for the xdata")
ydata_string = input("Enter your values for the ydata")

xdata = xdata_string.split()
ydata = ydata_string.split()

#convert input to numbers
    for i in range(len(xdata)):
          xdata[i] = float(xdata[i])

    for i in range(len(ydata)):
          ydata[i] = float(ydata[i])


def curve_fn(x,a,b,c,d):
    return (a*(x**3))+(b*(x**2))+(c*x)+d


def cubefit(xdata,ydata):
    
    answer_string = sp.optimize.curve_fit(curve_fn,xdata,ydata)
    
    #debug
    answer_string = answer_string[:1]
    print(answer_string)
    
    return np.array(answer_string, dtype=float)

cubefit(xdata,ydata)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source