Search code examples
pythonoptimizationpredictionsplinegekko

Using B-spline method of the form z = f(x, y) to fit z = f(x)


As a potential solution to this question, how could one coerce GEKKO's m.bspline method which builds 2D B-splines in the form z = f(x, y) to build 1D B-splines in the form z = f(x)?

More specifically, the 2D method takes in the following arguments:

  • x,y = independent Gekko parameters or variables as predictors for z
  • z = dependent Gekko variable with z = f(x,y)
  • x_data = 1D list or array of x knots, size (nx)
  • y_data = 1D list or array of y knots, size (ny)
  • z_data = 2D list or matrix of c coefficients, size (nx-kx-1)*(ny-ky-1)
  • kx = degree of spline in x-direction, default=3
  • ky = degree of spline in y-direction, default=3

Essentially, I want to trick the method into ignoring the y independent variable completely.


Solution

  • Try using a zero (or some other nominal) value for y in creating the b-spline.

    from gekko import GEKKO
    import numpy as np
    m = GEKKO(remote=False)
    
    xgrid = np.linspace(-1, 1, 50)
    ygrid = np.linspace(-1e-5,1e-5,50)
    xg,yg = np.meshgrid(xgrid,ygrid)
    
    z_data = xg**2
    
    x = m.Var(0.2,lb=-0.8,ub=0.8)
    y = m.Param(0)
    z = m.Var(0.1)
    
    m.bspline(x,y,z,xgrid,ygrid,z_data)
    
    m.Minimize(z)
    
    m.options.SOLVER=3
    m.solve(disp=False)
    
    print(f'x={x.value[0]}')
    print(f'y={y.value[0]}')
    

    There is also an option to inject the knots and coefficients directly if they are already fit with another function. If this is the case, just keep the value of y as a parameter so it is not adjustable.