0

I have a set of data that I'd like to fit using the following 3D-polynomial:

y = c1 + c2 * u + c3 * v + c4 * u² + c5 * (u*v) + c6 * v² + c7 * u³ + c8 *  (v * u²) + c9 * (u * v²) + c10 * v³

I tried with curve_fit but it only works with given dataset. Any points between the given data would give a wrong ZZ. Is there a better solution to this?

Any help would be appreciated!

The code:

import numpy as np
#import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import scipy

def fnc(u, v, params):
    c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 = params
    return c1 + c2 * u + c3 * v + c4 * u**2 + c5 * (u*v) + c6 * v**2 + c7 * u**3 + c8 * ( v * u**2 ) + c9 * (u * v**2) + c10 * v**3

def fn(params, f, u, v):
    c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 = params
    return np.ravel(c1 + c2*u + c3*v + c4*u**2+c5*(u*v)+c6*v**2+c7*u**3+c8*(v*u**2)+c9*(u*v**2)+c10*v**3-f)

x = np.transpose(np.array([-25.0,-20.0,-15.0,-10.0,-5.0,0.0,5.0,10.0]))
y = np.transpose(np.array([40.0,50.0,60.0]))
XX,YY = np.meshgrid(x,y)
XX = np.transpose(XX)
YY = np.transpose(YY)

ZZ = np.transpose(np.array([[3878.00,4636.00,5489.00,6483.00,7616.00,8844.00,10245.00,11865.00],[3712.00,4298.00,5123.00,6004.00,7030.00,8161.00,9503.00,11004.00],[3548.00,4064.00,4707.00,5529.00,6434.00,7506.00,8716.00,10070.00]]))

p0 = [0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5]

print 'XX', XX
print ''
print 'YY', YY
print ''
print 'ZZ', ZZ
print ''

#fitParams, fitCovariances = curve_fit(fn,x0=p0,args=(ZZ,XX,YY),full_output=1)
pfit, pcov, infodict, errmsg, success = scipy.optimize.leastsq(fn,x0=p0,args=(ZZ,XX,YY),full_output=1)

for i in range(len(pfit)):
    print 'c'+str(i+1), '%0.3e' % pfit[i], '%0.3e' % np.absolute(pcov[i][i])**0.5
print ''

#print pcov
#print errmsg
#print success

u = -8.
v = 53.
print 'u', u
print 'v', v
amp = fnc(u, v, pfit)
print 'Value with fitted parameters:', amp
12
  • 2
    Could you please explain more what you mean by "it only works with given dataset"?
    – galath
    Jul 24, 2015 at 9:38
  • a wrong ZZ, sorry ...
    – Muho903
    Jul 24, 2015 at 9:51
  • it only works with data in x and y. For ex. :x = -25; y = 40.
    – Muho903
    Jul 24, 2015 at 9:53
  • I tried with u = -8 and v = 53 (see last part of the code), it gave a completely wrong value with fitted parameters...
    – Muho903
    Jul 24, 2015 at 9:57
  • What would be the "correct" result for u=-8, v=53?
    – galath
    Jul 24, 2015 at 10:36

1 Answer 1

0

Let's plot the sample with a fixed YY=40 value:

import matplotlib.pyplot as plt

i = 0
plt.plot(XX[i], ZZ[i],'bo')
plt.xlabel('XX[0]')
plt.ylabel('ZZ[0]')
plt.show()

sample at YY=40

the fitted function with a fixed y value:

x = np.arange(start=-25, stop=10, step=0.1)
for y in [40, 50, 53]:
    z = fnc(x, y, pfit)
    plt.plot(x, z, label='y=' + str(y))
plt.xlabel('x')
plt.ylabel('z')
plt.legend()
plt.show()

enter image description here

It looks weird the y=53 curve is so low.

Although it becomes clear on the on the other axis: look at the plot of the fitted function with a fixed x value:

y = np.arange(start=40, stop=60, step=0.1)
for x in [-10, -8, -5]:
    z = fnc(x, y, pfit)
    plt.plot(y, z, label='x=' + str(x))
plt.xlabel('y')
plt.ylabel('z')
plt.legend()
plt.show()

enter image description here

On that axis, it's clearly overfitting. It's as if in 1D, we fitted a polynomial of degree 3 passing through only 3 points. Perhaps you could increase the number of data points on that axis?

by the way

x = np.transpose(np.array([-25.0,-20.0,-15.0,-10.0,-5.0,0.0,5.0,10.0]))

is unnecessary, replace it with

x = np.array([-25.0,-20.0,-15.0,-10.0,-5.0,0.0,5.0,10.0])
2
  • Thanks alot for ur tip! That was a specific dataset of temperatures and capacities, I'm looking for another one that has more values of temperatures at the y-achsis and will let you know right away...
    – Muho903
    Jul 24, 2015 at 13:45
  • @Muho903 perhaps you could also reduce the complexity of your function on the y axis, too. Degree 3 for the x, degree 2 for the y.
    – galath
    Jul 24, 2015 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.