Python Forum
Is there similar function to lsqnonlin (in matlab) in python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there similar function to lsqnonlin (in matlab) in python?
#1
I have done the curve fitting for a specific equation in matlab. I have got the coefficients of the equation. And when I plotted the implicit equation in matlab, it shows a difference between plotted equation and data points. Could anyone explain why?

And the code I used in matlab is

function res = FUN(x,xdata,ydata,zdata)

n = 8
% A = ydata
% B = -xdata
% C = xdata. - ydata
% H = zdata

a1 = x(1)*ydata - x(3)*(xdata - ydata)
b1 = x(2)*(-xdata) - x(1)*(ydata)
c1 = x(3)*(xdata - ydata) - x(2)*(-xdata)
h1 = x(4)*(zdata)

I3 = ((a1.* b1.*c1)/(54))-((b1.*(h1.^2))/(6))
I2 = ((h1.^2)/(3))+((a1.^2 + b1.^2 + c1.^2)/(54))

th = acos(I3/(I2.^(3/2)))
v1 = ((2*th)+pi)/6

an1 = (abs(2*cos(v1)))^n
an2 = (abs(2*cos((2*th+3*pi)/6)))^n
an3 = (abs(2*cos((2*th+5*pi)/6)))^n

res = ((3*I2).^(n/2)) * (an1 + an2 + an3) - (2*(189.32)^8);
%res = ((3*I2).^(n/2)) * (an1 + an2 + an3) - (1);
end

x_10 = [212.60,0,36.03,186.45,231.366,198.50,0,-36.25,-185.20,-210.71,-230.14,-197.98];
y_10 =[0,-211.50,-186.48,-37.51,199.48,231.98,213.38,187.72,37.30,0,-198.96,-230.79];
zdata = [0,0,0,0,0,0,0,0,0,0,0,0];
x0 = [1 1 1 0]; % initial values for a, b and c
[x] = lsqnonlin(@(x)FUN(x,x_10,y_10,zdata),x0)

I got the coefficients of the equation as 0.8198 0.8253 0.8760 0


Then I want to try the same thing in python. I used the following code in python
[xdata =  [2.12536531e+02 -5.25762033e-37  3.76934845e+01 1.86245922e+02 2.32034637e+02  1.99421349e+02 -5.29372192e-37 -3.78800319e+01 -1.84965199e+02 -2.10596796e+02 -2.30791199e+02 -1.98935694e+02]
ydata = [-2.29764844e-36 -2.11076049e+02 -1.85513163e+02 -3.74651905e+01 2.00243718e+02  2.32547563e+02  2.12989518e+02 1.86788046e+02 3.72506794e+01 -2.37575344e-36 -1.99735494e+02 2.31310086e+02]
zdata = [0,0,0,0,0,0,0,0,0,0,0,0]

def barlat(x,p,q,r,s):

    n  = 8
#     % A  = ydata
#     % B  = -xdata
#     % C  = xdata. - ydata
#     % H  = zdata

    a1 = p*ydata - r*(xdata - ydata)
    b1 = q*(-xdata) - p*(ydata)
    c1 = r*(xdata - ydata) - q*(-xdata)

    h1 = s*zdata

#     I3 = ((a1.* b1.*c1)/(54))-((b1.*(h1.^2))/(6))
#     I2 = ((h1.^2)/(3))+((a1.^2 + b1.^2 + c1.^2)/(54))

    a2 = np.multiply(a1,b1)
    a3 = np.multiply(a2,c1)
    h2 = np.multiply(h1,h1)
    a4 = np.multiply(a1,a1)
    b4 = np.multiply(b1,b1)
    c4 = np.multiply(c1,c1)


    I3 = ((a3)/(54))
    I2 = ((a4 + b4 + c4)/(54))  

    th = np.arccos(np.divide(I3,I2**(3/2)))
    v1 = ((2*th)+ np.pi)/6

    an1 = (np.abs(2*np.cos(v1)))**n
    an2 = (np.abs(2*np.cos((2*th+3*np.pi)/6)))**n
    an3 = (np.abs(2*np.cos((2*th+5*np.pi)/6)))**n

    res = ((3*I2)**(n/2)) * (an1 + an2 + an3) - (2*(189.32)**8);
#     %res = ((3*I2)^(n/2)) * (an1 + an2 + an3) - (1);

    return res

popt, pcov = curve_fit(barlat, xdata, ydata,p0= (1,1,1,0))
popt
Error:
[py:26: RuntimeWarning: invalid value encountered in arccos py:794: OptimizeWarning: Covariance of the parameters could not be estimated category=OptimizeWarning) ]

(May-04-2019, 11:51 AM)Jay_Nerella Wrote: I have done the curve fitting for a specific equation in matlab. I have got the coefficients of the equation. And when I plotted the implicit equation in matlab, it shows a difference between plotted equation and data points. Could anyone explain why?

And the code I used in matlab is

function res = FUN(x,xdata,ydata,zdata)

n = 8
% A = ydata
% B = -xdata
% C = xdata. - ydata
% H = zdata

a1 = x(1)*ydata - x(3)*(xdata - ydata)
b1 = x(2)*(-xdata) - x(1)*(ydata)
c1 = x(3)*(xdata - ydata) - x(2)*(-xdata)
h1 = x(4)*(zdata)

I3 = ((a1.* b1.*c1)/(54))-((b1.*(h1.^2))/(6))
I2 = ((h1.^2)/(3))+((a1.^2 + b1.^2 + c1.^2)/(54))

th = acos(I3/(I2.^(3/2)))
v1 = ((2*th)+pi)/6

an1 = (abs(2*cos(v1)))^n
an2 = (abs(2*cos((2*th+3*pi)/6)))^n
an3 = (abs(2*cos((2*th+5*pi)/6)))^n

res = ((3*I2).^(n/2)) * (an1 + an2 + an3) - (2*(189.32)^8);
%res = ((3*I2).^(n/2)) * (an1 + an2 + an3) - (1);
end

x_10 = [212.60,0,36.03,186.45,231.366,198.50,0,-36.25,-185.20,-210.71,-230.14,-197.98];
y_10 =[0,-211.50,-186.48,-37.51,199.48,231.98,213.38,187.72,37.30,0,-198.96,-230.79];
zdata = [0,0,0,0,0,0,0,0,0,0,0,0];
x0 = [1 1 1 0]; % initial values for a, b and c
[x] = lsqnonlin(@(x)FUN(x,x_10,y_10,zdata),x0)

I got the coefficients of the equation as 0.8198 0.8253 0.8760 0


Then I want to try the same thing in python. I used the following code in python
[xdata =  [2.12536531e+02 -5.25762033e-37  3.76934845e+01 1.86245922e+02 2.32034637e+02  1.99421349e+02 -5.29372192e-37 -3.78800319e+01 -1.84965199e+02 -2.10596796e+02 -2.30791199e+02 -1.98935694e+02]
ydata = [-2.29764844e-36 -2.11076049e+02 -1.85513163e+02 -3.74651905e+01 2.00243718e+02  2.32547563e+02  2.12989518e+02 1.86788046e+02 3.72506794e+01 -2.37575344e-36 -1.99735494e+02 2.31310086e+02]
zdata = [0,0,0,0,0,0,0,0,0,0,0,0]

def barlat(x,p,q,r,s):

    n  = 8
#     % A  = ydata
#     % B  = -xdata
#     % C  = xdata. - ydata
#     % H  = zdata

    a1 = p*y - r*(x - y)
    b1 = q*(-x) - p*(y)
    c1 = r*(x - y) - q*(-x)

    h1 = s*zdata

#     I3 = ((a1.* b1.*c1)/(54))-((b1.*(h1.^2))/(6))
#     I2 = ((h1.^2)/(3))+((a1.^2 + b1.^2 + c1.^2)/(54))

    a2 = np.multiply(a1,b1)
    a3 = np.multiply(a2,c1)
    h2 = np.multiply(h1,h1)
    a4 = np.multiply(a1,a1)
    b4 = np.multiply(b1,b1)
    c4 = np.multiply(c1,c1)


    I3 = ((a3)/(54))
    I2 = ((a4 + b4 + c4)/(54))  

    th = np.arccos(np.divide(I3,I2**(3/2)))
    v1 = ((2*th)+ np.pi)/6

    an1 = (np.abs(2*np.cos(v1)))**n
    an2 = (np.abs(2*np.cos((2*th+3*np.pi)/6)))**n
    an3 = (np.abs(2*np.cos((2*th+5*np.pi)/6)))**n

    res = ((3*I2)**(n/2)) * (an1 + an2 + an3) - (2*(189.32)**8);
#     %res = ((3*I2)^(n/2)) * (an1 + an2 + an3) - (1);

    return res

popt, pcov = curve_fit(barlat, xdata, ydata,p0= (1,1,1,0))
popt
Error:
[py:26: RuntimeWarning: invalid value encountered in arccos py:794: OptimizeWarning: Covariance of the parameters could not be estimated category=OptimizeWarning) ]
Reply
#2
Hello Jay_Nerella,

presently I also use the curve_fit function of python so I thought I may possibly help you. First, I detected some syntax faults in your python code:

"[xdata" in the beginning should be only "xdata".

You should use np.arrays instead of lists for the x-, y-, zdata:
xdata =  np.array([2.12536531e+02,-5.25762033e-37, 3.76934845e+01,1.86245922e+02,2.32034637e+02, 1.99421349e+02,-5.29372192e-37,-3.78800319e+01,-1.84965199e+02,-2.10596796e+02,-2.30791199e+02,-1.98935694e+02])
(At least with my python2.7 interpreter I had change these lines.)

Then I realised that your function barlat(x,p,q,r,s) does not use the variable x within its definition, which it definitely should. For me is also unclear what data the output of the function should correspond to. Normally this is ydata. But you use ydata within your definition of barlat(x,p,q,r,s), so it cannot also be the data corresponding to the output. Again you also use xdata within the definition of barlat(x,p,q,r,s), normally you use xdata only when performing the curve_fit function.

So think about the procedure of curve fitting again. First you have a function f(x,p,q,r,s), where x can be multidimensional and p, q, r, s are parameters. Then you have data xdata (input) and ydata (output) which you want to describe by the function. So in other words you want to find parameters p, q, r, s such that f(xdata,p,q,r,s) = ydata "holds as best as it can be".

Hope this is useful.

feli_x
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem of converting Matlab code to python DongyanZ 2 1,376 Feb-03-2023, 01:04 PM
Last Post: jefsummers
  Convolution "same" in Python doesn't work as Matlab claw91 4 3,688 Oct-01-2020, 08:59 AM
Last Post: claw91
  scipy.signal.find_peak VS Matlab's findpeaks function claw91 2 5,408 Sep-15-2020, 12:27 PM
Last Post: claw91
  Python equivalent of Matlab code kwokmaster 5 6,999 Apr-03-2020, 12:02 AM
Last Post: scidam
  Matlab to Python Ariane 6 6,999 Jun-14-2018, 12:08 PM
Last Post: Ariane
  How to use .m matlab file in python ? sameer 5 15,291 May-10-2018, 09:39 AM
Last Post: wavic
  Importing matlab cell array (.mat) into a python list scanato 0 8,604 Nov-15-2017, 11:04 AM
Last Post: scanato

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020