Trouble formulating minimization problem

I have the following minimization problem that I am trying solve in Matlab, but just not coming up with the right way to formulate it.
Given an image matrix V, I need to minimize the error function (not in Matlab format)
error = ∑ ( V(x,y) - f(x,y) )^2, summed over 1600 points (x,y)
where f(x,y) = P1 * x^2 + P2 * y^2 + P3 *x*y + P4*x + P5*y + P6
The goal is to determine the P1-P6 values. Any help would be greatly appreciated.
Keith

 Accepted Answer

I assume V is of double type.
[m,n] = size(V);
[x,y] = ndgrid(1:m,1:n);
x = x(:); y = y(:);
P = [x.^2,y.^2,x.*y,x,y,ones(m*n,1)]\V(:);
The elements of P will be your desired least square coefficients.

4 Comments

Roger, That worked perfectly. Now if I can just understand what the heck you did, I'll be in good shape ( I had been going down the road of fminsearch with no luck). Thanks again.
Read the documentation for 'mldivide' (backslash) in the section for over-determined linear equations: "If A is an m-by-n matrix with m ~= n and B is a column vector with m components, or a matrix with several such columns, then X = A\B is the solution in the least squares sense to the under- or overdetermined system of equations AX = B." In your case m is the number of points, (over 1600,) and n is six, so it is definitely over-determined.
http://www.mathworks.com/help/matlab/ref/mldivide.html
It is not necessary to use functions like 'fminsearch' because your expression is linear in the six unknown parameters.
Thanks - that clears it up for me. Now if I can just solve the problem of having too many senior moments, ...
When you solve that problem, please let me know. At age 87 I am having senior moments too.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 21 Jun 2013
Edited: Matt J on 21 Jun 2013
There is also this FEX file
which takes care to use QR factorizations, similar to polyfit.

1 Comment

Matt - I'll definitely check this out as well. Thanks.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!