Solve nonlinear curve-fitting (data-fitting) problems

2 views (last 30 days)
Hi all, I have a problem with Solve nonlinear curve-fitting (data-fitting) lsqcurvefit. In fact an error appears to me :
if true
Error using lsqcurvefit (line 248)
Function value and YDATA sizes are not equal.
Error in glob_lv (line 305)
end
The code is :
if true
function yy = func(m,xdata)
yy= [(m(1,1) * xdata(1,:) + m(1,2)* xdata(2,:) + m(1,3) * xdata(3,:) + m(1,4)) / (m(3,1) * xdata(1,:) + m(3,2)* xdata(2,:) + m(3,3) * xdata(3,:) + m(3,4)) ;
(m(2,1) * xdata(1,:)+ m(2,2)* xdata(2,:) + m(2,3) * xdata(3,:) + m(2,4)) / (m(3,1) * xdata(1,:) + m(3,2)* xdata(2,:) + m(3,3) * xdata(3,:) + m(3,4))] ;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xda= [ x1 , x2 , x3 , x4 , x5 , x6 , x7 , x8 , x9 , x10 , x11 , x12]
yda= [ y1 , y2 , y3 , y4 , y5 , y6 , y7 , y8 , y9 , y10 , y11 , y12]
zda= [ z1 , z2 , z3 , z4 , z5 , z6 , z7 , z8 , z9 , z10 , z11 , z12]
xdata= [ xda ; yda ;zda]
ydata = [ u1 ,u2 , u3 ,u4 , u5 ,u6 , u7 ,u8 , u9 ,u10 , u11 ,u12,
v1 ,v2 , v3 ,v4 , v5 ,v6 , v7 ,v8 , v9 ,v10 , v11 ,v12 ]
m0=m_svd;
options = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt');
lb = [];
ub = [];
m = lsqcurvefit(@func,m0,xdata,ydata,lb,ub,options);
end
Any help ?

Answers (1)

Star Strider
Star Strider on 27 Jan 2016
That error usually means that your data are in rows (or columns) and the output of your objective function are in columns (or rows), so they don’t match in dimension. Your objective function otherwise appears correct, so consider transposing the output matrix in it.
For example:
function yy = func(m,xdata)
yy= [(m(1,1) * xdata(1,:) + m(1,2)* xdata(2,:) + m(1,3) * xdata(3,:) + m(1,4)) / (m(3,1) * xdata(1,:) + m(3,2)* xdata(2,:) + m(3,3) * xdata(3,:) + m(3,4)) ;
(m(2,1) * xdata(1,:)+ m(2,2)* xdata(2,:) + m(2,3) * xdata(3,:) + m(2,4)) / (m(3,1) * xdata(1,:) + m(3,2)* xdata(2,:) + m(3,3) * xdata(3,:) + m(3,4))].' ;
end
Note the addition of the (.') operator at the end:
... * xdata(3,:) + m(3,4))].' ;
so it takes the simple transpose. (The (') operator takes the complex-conjugate transpose, although the two are equivalent for real arrays.)
I cannot run your code to check it, so you will have to see if that solves the problem.

Community Treasure Hunt

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

Start Hunting!