Curve fitting lsqnonlin Error? Optimization

2 views (last 30 days)
Jay
Jay on 9 Apr 2019
Commented: Jay on 26 Apr 2019
Hello
I have done the curve fitting for the customized equation using the following method:
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);
end
x_100 = [204.31,0,31.59,181.02,219.97,191.86,0,-31.811,-179.81,-202.56,-218.67,-191.13];
y_100 = [0,-203.68,-181.29,-33.38,193.82,222.11,205.42,182.49,33.18,0,-193.06,-220.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_100,y_100,zdata),x0)
I got the result as 0.8535 0.9247 0.9893
Then I plotted the same equation using the obtained coefficients using fimplicit and it looks like:
If I am using lsqnonlin, the implicit equation should pass through the points, but there is a gap between points and implicit equation.
Could anyone answer this, why the implicit equation is not passing through the points though I calculated the coefficients of the function using curve fitting with these points?
  2 Comments
Matt J
Matt J on 26 Apr 2019
The code you've posted fails with
Matrix dimensions must agree.
Error in test>FUN (line 18)
I3 = ((a1.* b1.*c1)/(54))-((b1.*(h1.^2))/(6))
Error in test>@(x)FUN(x,x_100,y_100,zdata)
Error in lsqnonlin (line 206)
initVals.F = feval(funfcn{3},xCurrent,varargin{:});
Error in test (line 6)
[x,r1,r2,exitflag] = lsqnonlin(@(x)FUN(x,x_100,y_100,zdata),x0)
Caused by:
Failure in initial objective function evaluation. LSQNONLIN cannot continue.
Jay
Jay on 26 Apr 2019
Hi Matt
I have edited the matrix. There is an extra 0 in zdata. Now it will be working.
Thanks in advance
Jay

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!