woz
woz

Reputation: 574

implicit curve fitting using scattered data

I have two arrays - x and y - which corresponds to coordinates (x,y) in a cartesian plane. For example, with the scatter function (plt.scatter(x,y)) from matplotlib (so far, I'm trying to solve my problem using Python), I get the following result:

What I really need is to get an implicit function f(x,y) from this data, or at least coefficients from an approximate function f(x,y) . So far, I tried to use the curve_fitfunction from scipy.optimize as suggested here, but I've got the following error message:

OptimizeWarning: Covariance of the parameters could not be estimated category=OptimizeWarning)

That is my code so far:

import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import scipy as sy
import pylab as plb 

def func(x, a, b, c):
    return a*x**b + c
def main():
    file = open('firstcurve.out')
    lines = file.read().split('\n')
    file.close()
    x = []
    y = []
    for item in lines:
        if len(item) > 0:
            numbers = item.split(",")
            x = x + [float(numbers[0])]
            y = y + [float(numbers[1])]

    p0 = sy.array([1,1,1])
    coeffs, matcov = curve_fit(func, x, y, p0)
    yaj = func(x, coeffs[0], coeffs[1], coeffs[2])
    plt.plot(x,yaj,'r-')
    plt.show()
main()

Any help or suggestions are really appreciated!

PS: I'm trying to do it in Python, but MatLab is also an option in case there is any tool that does what I need to. I tried to use the SLM ToolKit but it didn't work as well.

Upvotes: 2

Views: 1745

Answers (2)

James Phillips
James Phillips

Reputation: 4657

Here is code to fit a surface equation "z = f(x,y)", plot the raw data 3D scatterplot, plot the 3D fitted surface, and graph a contour plot. This should at least give you the graphics you need.

import numpy, scipy, scipy.optimize
import matplotlib
from mpl_toolkits.mplot3d import  Axes3D
from matplotlib import cm # to colormap 3D surfaces from blue to red
import matplotlib.pyplot as plt

graphWidth = 800 # units are pixels
graphHeight = 600 # units are pixels

# 3D contour plot lines
numberOfContourLines = 16


def SurfacePlot(func, data, fittedParameters):
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

    matplotlib.pyplot.grid(True)
    axes = Axes3D(f)

    x_data = data[0]
    y_data = data[1]
    z_data = data[2]

    xModel = numpy.linspace(min(x_data), max(x_data), 20)
    yModel = numpy.linspace(min(y_data), max(y_data), 20)
    X, Y = numpy.meshgrid(xModel, yModel)

    Z = func(numpy.array([X, Y]), *fittedParameters)

    axes.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=1, antialiased=True)

    axes.scatter(x_data, y_data, z_data) # show data along with plotted surface

    axes.set_title('Surface Plot (click-drag with mouse)') # add a title for surface plot
    axes.set_xlabel('X Data') # X axis data label
    axes.set_ylabel('Y Data') # Y axis data label
    axes.set_zlabel('Z Data') # Z axis data label

    plt.show()
    plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def ContourPlot(func, data, fittedParameters):
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
    axes = f.add_subplot(111)

    x_data = data[0]
    y_data = data[1]
    z_data = data[2]

    xModel = numpy.linspace(min(x_data), max(x_data), 20)
    yModel = numpy.linspace(min(y_data), max(y_data), 20)
    X, Y = numpy.meshgrid(xModel, yModel)

    Z = func(numpy.array([X, Y]), *fittedParameters)

    axes.plot(x_data, y_data, 'o')

    axes.set_title('Contour Plot') # add a title for contour plot
    axes.set_xlabel('X Data') # X axis data label
    axes.set_ylabel('Y Data') # Y axis data label

    CS = matplotlib.pyplot.contour(X, Y, Z, numberOfContourLines, colors='k')
    matplotlib.pyplot.clabel(CS, inline=1, fontsize=10) # labels for contours

    plt.show()
    plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def ScatterPlot(data):
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

    matplotlib.pyplot.grid(True)
    axes = Axes3D(f)
    x_data = data[0]
    y_data = data[1]
    z_data = data[2]

    axes.scatter(x_data, y_data, z_data)

    axes.set_title('Scatter Plot (click-drag with mouse)')
    axes.set_xlabel('X Data')
    axes.set_ylabel('Y Data')
    axes.set_zlabel('Z Data')

    plt.show()
    plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def func(data, a, alpha, beta):
    t = data[0]
    p_p = data[1]
    return a * (t**alpha) * (p_p**beta)


if __name__ == "__main__":
    xData = numpy.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])
    yData = numpy.array([11.0, 12.1, 13.0, 14.1, 15.0, 16.1, 17.0, 18.1, 90.0])
    zData = numpy.array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.0, 9.9])

    data = [xData, yData, zData]

    # this example uses curve_fit()'s default initial paramter values
    fittedParameters, pcov = scipy.optimize.curve_fit(func, [xData, yData], zData)

    ScatterPlot(data)
    SurfacePlot(func, data, fittedParameters)
    ContourPlot(func, data, fittedParameters)

    print('fitted prameters', fittedParameters)

Upvotes: 0

Eric Yang
Eric Yang

Reputation: 2750

This is more of a math problem than it is a coding question. You can't use the curve fit function in python because it's looking it's looking for a function i.e. you cannot have two separate Y's for the same X.

One thing that you can try if it's possible is to define a parametric function

x = f(t)

y = g(t)

And use the curve fit function to fit x and y vs. t. If you represent it that way, you can use smoothing splines to do the fit.

https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.UnivariateSpline.html

Upvotes: 1

Related Questions