From: M.E.L. on
I've written the following code to a least squares fit of a function. I want to find the values of k1 and k2 for which the the function is minimized.

% set ifit to 0 and don't continue on to the fit until
% the user sets it to 1
ifit=0;
while ifit==0
disp(' Enter an initial guess for the function ')
kplot=input('parameters [k1,k2] in vector form [...]- ')

% plot the data and the initial function guess
Fplot=funcfit(kplot,rplot);
figure, plot(r1,F2(:,y),'b-',rplot,Fplot,'g-')
figure, plot(r1,F2(:,y),'b-',rplot(125:325),.5*Fplot(125:325)-.38,'g-')
ifit=input(' Enter 0 to guess again, 1 to try to fit with this guess - ')
end

option=optimset('TolX',1e-10);
k=fminsearch(@(k) leastsq(k,r,A),[kx,ky],option)

The while loops works fine and my plots look good. The problem arises when I try to fit using one of my guesses.

The function leastsq is defined as:
function s=leastsq(k,r,A)
s=sum((A-(k(1).*r+k(2).*r)).^2);

and both A and r are defined as 480x480 arrays of numbers.

The error I get is:

??? Subscripted assignment dimension mismatch.

Error in ==> fminsearch at 205
fv(:,1) = funfcn(x,varargin{:});

Error in ==> FFT_DataFit2 at 97
k=fminsearch(@(k) leastsq(k,r,A),[kx,ky],option)

Am I running into problems because A and r are arrays? Help!
From: Matt J on
"M.E.L. " <schrodingers.lyon(a)gmail.com> wrote in message <hsf1i9$oio$1(a)fred.mathworks.com>...

>
> option=optimset('TolX',1e-10);
> k=fminsearch(@(k) leastsq(k,r,A),[kx,ky],option)
>
> The while loops works fine and my plots look good. The problem arises when I try to fit using one of my guesses.
>
> The function leastsq is defined as:
> function s=leastsq(k,r,A)
> s=sum((A-(k(1).*r+k(2).*r)).^2);
==============

Some pecularities obvious from the beginning:

(1) Your objective depends on k(1) and k(2) only through its sum, i.e., the above objective simplifies to

s=sum( (A - sum(k).*r).^2 )

This means you have a continuum of solutions, making the problem highly ill-conditioned.


(2) Your objective function above does not produce a scalar if A and r are 480x480.
Because sum() adds up individual columns by default, the result of the above sum will be a 1x480 vector.


(3) To address both (1) and (2), it's possible that you want instead the objective function

function s=leastsq(ksum,r,A)
s=sum( (A(:) - ksum*r(:) )^2)

This however has the closed form solution

ksum=A(:)\r(:)

In general, it is strange that you would try to solve a low dimensional unconstrained quadratic minimization using an iterative method like fminsearch when these problems have analytical solutions.

====================
>
> The error I get is:
>
> ??? Subscripted assignment dimension mismatch.
>
> Error in ==> fminsearch at 205
> fv(:,1) = funfcn(x,varargin{:});
==========

The error is in funfcn which you've told us nothing about. Regardless, the problem is that it is returning an array whose size is not the same as size(fv(:,1)).
From: Matt J on
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <hsf2v9$r42$1(a)fred.mathworks.com>...

> function s=leastsq(ksum,r,A)
> s=sum( (A(:) - ksum*r(:) )^2)
>
> This however has the closed form solution
>
> ksum=A(:)\r(:)
============

Make that ksum=r(:)\A(:)
From: M.E.L. on
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <hsf2v9$r42$1(a)fred.mathworks.com>...
> "M.E.L. " <schrodingers.lyon(a)gmail.com> wrote in message <hsf1i9$oio$1(a)fred.mathworks.com>...
>
> >
> > option=optimset('TolX',1e-10);
> > k=fminsearch(@(k) leastsq(k,r,A),[kx,ky],option)
> >
> > The while loops works fine and my plots look good. The problem arises when I try to fit using one of my guesses.
> >
> > The function leastsq is defined as:
> > function s=leastsq(k,r,A)
> > s=sum((A-(k(1).*r+k(2).*r)).^2);
> ==============
>
> Some pecularities obvious from the beginning:
>
> (1) Your objective depends on k(1) and k(2) only through its sum, i.e., the above objective simplifies to
>
> s=sum( (A - sum(k).*r).^2 )
>
> This means you have a continuum of solutions, making the problem highly ill-conditioned.

======================

Sloppy error, that should be rx and ry. So it's actually:

s=sum((A-((k(:,1).*rx)+(k(:,2).*ry)+(Y.*k(:,3)))).^2);

I've also included an additional term, which is a phase offset. I need to minimize k1, k2, and k3.

=======================
>
> (2) Your objective function above does not produce a scalar if A and r are 480x480.
> Because sum() adds up individual columns by default, the result of the above sum will be a 1x480 vector.


> (3) To address both (1) and (2), it's possible that you want instead the objective function
>
> function s=leastsq(ksum,r,A)
> s=sum( (A(:) - ksum*r(:) )^2)
>
> This however has the closed form solution
>
> ksum=A(:)\r(:)
>
> In general, it is strange that you would try to solve a low dimensional unconstrained quadratic minimization using an iterative method like fminsearch when these problems have analytical solutions.
>
====================
> >
> > The error I get is:
> >
> > ??? Subscripted assignment dimension mismatch.
> >
> > Error in ==> fminsearch at 205
> > fv(:,1) = funfcn(x,varargin{:});

> ==========
>
> The error is in funfcn which you've told us nothing about. Regardless, the problem is that it is returning an array whose size is not the same as size(fv(:,1)).

===============

Here's the kicker: I don't have a function called funfcn. I have no idea where it's coming from. I've just barely started using Matlab, so the whole thing is still rather mysterious to me. I'm sure my code must look ridiculous, but I very much appreciate your help!
From: Matt J on
"M.E.L. " <schrodingers.lyon(a)gmail.com> wrote in message <hsfbkq$ksb$1(a)fred.mathworks.com>...

> Sloppy error, that should be rx and ry. So it's actually:
>
> s=sum((A-((k(:,1).*rx)+(k(:,2).*ry)+(Y.*k(:,3)))).^2);
>
> I've also included an additional term, which is a phase offset. I need to minimize k1, k2, and k3.
=================

The only way this expression won't give an error is if k(:,i) are scalars and
rx, ry, rz are 480x480 . It follows that k is a vector and
you can just index this as k(i) instead of k(:,i)

In any case, my remarks from before still apply. There is no way s will work out to be a scalar if A is 480x480

Also, if this is a quadratic minimization, there is still no reason you should be using fminsearch. The minimum of any quadratic function of k1, k2, k3 has a closed-form solution obtainable much more reliably/efficiently from the backslash operator '\'.



>
> Here's the kicker: I don't have a function called funfcn. I have no idea where it's coming from. I've just barely started using Matlab, so the whole thing is still rather mysterious to me. I'm sure my code must look ridiculous, but I very much appreciate your help!
========================

It's probably called internally by fminsearch and the error is probably because, as mentioned above, your leastsq() objective function does not return a scalar.

The problem will be moot once you stop using fminsearch, as I've recommended.
 |  Next  |  Last
Pages: 1 2
Prev: Mean gray scale of video
Next: create an executable