Nonlinear least squares problem in Matlab

I am trying to solve an optimization problem in Matlab. It is a nonlinear least squares problem. The goal is to derive the best-fit equations of seven straight lines (and other standard output e.g. residuals etc.).
I’ve posted the problem description, and two images, one that describes the problem setting in detail, the other showing the set of 3D points I plotted for this, all here:

Since I never used any optimizer before, I’d truly appreciate if you can help on:
1.which Matlab function to use for the problem
2.what form of output I should expect
3.I know there is extensive documentation for each Matlab function, yet I’d welcome the formulation of this problem to directly feed to the Matlab function chosen in 1.

Please let me know what you think. Thanking in advance,

Hi Arif,

While hopefully one of our users can help (Chris?), you might want to also post your question to comp…soft-sys.matlab (See This forum mostly focuses on PGI specific issues while comp.soft-sys.matlab focuses on Matlab questions.

  • Mat

I may be wrong but it sounds like you are just trying to do a Total Least Squares fits to 3D data for different sets of data (lines)? This is a little tougher than doing regular 2D TLS, but you can do it in matlab pretty easily. An example using built-in matlab functions is at:

Alternatively, you could so it using linear algebra, by minimizing the ‘true’ distances between the points and the line. Representing your line as [x;y;z] = [a;b;c]*t + [x0;y0;z0], [a;b;c] is your normal vector along the line, and [x0;y0;z0] is a point on the line. You can take x0=mean(x), etc since a TLS fit will pass through the averages of the points. To find your normal vector, you can minimize the distances between the points and the points on the line perpendicular to them. For this setup, your system is a matrix representation of the cross products of your normal vector (n) and the vector from your [x0;y0;z0] point to your data points (lets call it r).

IE: ri = [xi,yi,zi] - [x0,y0,z0]
M = [n x r0;

n x ri
… ]

Alternatively, this minimization is equivalent to maximizing the projection of your vector r onto your normal vector, which is easier to setup.

IE: maximize the norm of A:
A = [x-mean(x), y-mean(y), z-mean(z)]

For either system, you end up taking the SVD of your system (A), and then your normal vector is the singular vector corresponding to either the maximum or minimum singular value, depending on which system you used.

note: I’m not familiar with the function used in the link I posted, but the ‘coeff’ variable they show in the example is the same as the singular vectors of the A matrix above.