Thread Subject: Matrix dimensions must agree with fsolve

Subject: Matrix dimensions must agree with fsolve

From: Luca Carlon

Date: 30 Sep, 2009 13:01:02

Message: 1 of 9

Hi! I created a function which solved a nonlinear system of equations using these options:

options = optimset('TolX', 1e-5, 'TolFun', 1e-5,...
    'MaxFunEvals', 1e8, 'MaxIter', 1e8, 'Display', 'on');

This works ok, but I would like to try to have more precise results for the roots, so I tried with these options instead:

options = optimset('TolX', 1e-6, 'TolFun', 1e-6,...
    'MaxFunEvals', 1e8, 'MaxIter', 1e8, 'Display', 'on');

and in this case, after some time, I get:

??? Error using ==> minus
Matrix dimensions must agree.

Error in ==> finitedifferences at 137
                gradf(:,pass_cnt) = (fplus(:) - fCurrent)/deltaX(gcnt);

Error in ==> levenbergMarquardt at 223
            [JAC,unused1,unused2,numEvals] = finitedifferences(XOUT,funfcn{3},[],[],[], ...

Error in ==> fsolve at 385
    [x,FVAL,JACOB,EXITFLAG,OUTPUT,msg] = ...

Error in ==> computeGussPointsWeights2D at 92
    [wxi, fval, exitflag, output] = fsolve(F, x_0, options)

Any idea why? The tolerance doesn't change any dimension in the matrices in my function F which solves the equations of the system...
Thanks!

Subject: Matrix dimensions must agree with fsolve

From: Sebastiaan

Date: 30 Sep, 2009 13:50:04

Message: 2 of 9

It does not make sense indeed. It could be that there is a problem forming the matrices due to precision (singularity) problems. (A function could return 0 in stead of a vector of NaNs, for example.)

I would diagnose this problem as follows:
>> dbstop if error
>> % run fsolve
Now it will terminate with the debugger in the subfunction, so you can diagnose the problem. Do your vectors have the correct size? If not, try to find out why not.

Note that you can use dbup to go up one workspace (e.g. go from finitedifferences to
levenbergMarquardt, where the problem might be).

Sebastiaan



"Luca Carlon" <carlon.lucaREMOVE_THIS@gmail.com> wrote in message <h9vkqe$qc6$1@fred.mathworks.com>...
> Hi! I created a function which solved a nonlinear system of equations using these options:
>
> options = optimset('TolX', 1e-5, 'TolFun', 1e-5,...
> 'MaxFunEvals', 1e8, 'MaxIter', 1e8, 'Display', 'on');
>
> This works ok, but I would like to try to have more precise results for the roots, so I tried with these options instead:
>
> options = optimset('TolX', 1e-6, 'TolFun', 1e-6,...
> 'MaxFunEvals', 1e8, 'MaxIter', 1e8, 'Display', 'on');
>
> and in this case, after some time, I get:
>
> ??? Error using ==> minus
> Matrix dimensions must agree.
>
> Error in ==> finitedifferences at 137
> gradf(:,pass_cnt) = (fplus(:) - fCurrent)/deltaX(gcnt);
>
> Error in ==> levenbergMarquardt at 223
> [JAC,unused1,unused2,numEvals] = finitedifferences(XOUT,funfcn{3},[],[],[], ...
>
> Error in ==> fsolve at 385
> [x,FVAL,JACOB,EXITFLAG,OUTPUT,msg] = ...
>
> Error in ==> computeGussPointsWeights2D at 92
> [wxi, fval, exitflag, output] = fsolve(F, x_0, options)
>
> Any idea why? The tolerance doesn't change any dimension in the matrices in my function F which solves the equations of the system...
> Thanks!

Subject: Matrix dimensions must agree with fsolve

From: Luca Carlon

Date: 30 Sep, 2009 15:00:22

Message: 3 of 9

Hi! Thanks for your answer. This is how I called fsolve:

exitflag = -2;
x_0(2.*nquad) = 0;
while exitflag < 0
    for i = 1:4*nquad
        x_0(i) = rem(rand(1, 1), Xi(end)-Xi(1)+1) + Xi(1);
    end
    dbstop if error
    [wxi, fval, exitflag, output] = fsolve(F, x_0, options)
end

I tried to check the values of my variable, but any variable I tried is not defined. I am in the debugger but anything defined inside my function F is not defined...
Any idea why?
Thanks!

"Sebastiaan " <s.breedveld@erasmusmc.REMOVE.BOO.BOO.nl> wrote in message <h9vnmc$8b3$1@fred.mathworks.com>...
> It does not make sense indeed. It could be that there is a problem forming the matrices due to precision (singularity) problems. (A function could return 0 in stead of a vector of NaNs, for example.)
>
> I would diagnose this problem as follows:
> >> dbstop if error
> >> % run fsolve
> Now it will terminate with the debugger in the subfunction, so you can diagnose the problem. Do your vectors have the correct size? If not, try to find out why not.
>
> Note that you can use dbup to go up one workspace (e.g. go from finitedifferences to
> levenbergMarquardt, where the problem might be).
>
> Sebastiaan

Subject: Matrix dimensions must agree with fsolve

From: Sebastiaan

Date: 30 Sep, 2009 15:10:17

Message: 4 of 9

"Luca Carlon" <carlon.lucaREMOVE_THIS@gmail.com> wrote in message <h9vrq6$k8b$1@fred.mathworks.com>...
> Hi! Thanks for your answer. This is how I called fsolve:
>
> exitflag = -2;
> x_0(2.*nquad) = 0;
> while exitflag < 0
> for i = 1:4*nquad
> x_0(i) = rem(rand(1, 1), Xi(end)-Xi(1)+1) + Xi(1);
> end
> dbstop if error
> [wxi, fval, exitflag, output] = fsolve(F, x_0, options)
> end
>
> I tried to check the values of my variable, but any variable I tried is not defined. I am in the debugger but anything defined inside my function F is not defined...
> Any idea why?
> Thanks!
>
'whos' gives no result?

I cannot try your function: nquad is not defined. Can you give me something that I can execute to diagnose the problem?

Subject: Matrix dimensions must agree with fsolve

From: Luca Carlon

Date: 30 Sep, 2009 15:50:19

Message: 5 of 9

"Sebastiaan " <s.breedveld@erasmusmc.REMOVE.BOO.BOO.nl> wrote in message <h9vscp$uk$1@fred.mathworks.com>...
> "Luca Carlon" <carlon.lucaREMOVE_THIS@gmail.com> wrote in message <h9vrq6$k8b$1@fred.mathworks.com>...
> > Hi! Thanks for your answer. This is how I called fsolve:
> >
> > exitflag = -2;
> > x_0(2.*nquad) = 0;
> > while exitflag < 0
> > for i = 1:4*nquad
> > x_0(i) = rem(rand(1, 1), Xi(end)-Xi(1)+1) + Xi(1);
> > end
> > dbstop if error
> > [wxi, fval, exitflag, output] = fsolve(F, x_0, options)
> > end
> >
> > I tried to check the values of my variable, but any variable I tried is not defined. I am in the debugger but anything defined inside my function F is not defined...
> > Any idea why?
> > Thanks!
> >
> 'whos' gives no result?
>
> I cannot try your function: nquad is not defined. Can you give me something that I can execute to diagnose the problem?

Unfortunately I can't give you the code as it would result in sending you many functions. The F function is an anonymous function:

% Definition of the nonlinear system.
F = @(x)gaussEquations2D(p, n, Xi, q, m, Eta, int, nquad, x);

and gaussEquations2D is a long function which involves many others. nquad is defined of course before fsolve is called. fsolve runs for I think for something like twenty minutes or half an hour, and then, if tolerance is under more or less 1e-5 fails as reported.
This is the output of whos:

K>> whos
  Name Size Bytes Class Attributes

  JacCeqTrans 0x0 0 double
  JacCineqTrans 0x0 0 double
  cEqCurrent 0x0 0 double
  cIneqCurrent 0x0 0 double
  constrfun 0x0 0 double
  deltaX 580x1 4640 double
  fCurrent 290x1 2320 double
  fplus 1x289 2312 double
  fscale 0x0 0 double
  fwdFinDiff 1x1 1 logical
  gcnt 1x1 8 double
  gradf 290x580 1345600 double
  isGrad 1x1 1 logical
  lb 0x0 0 double
  nonEmptyLowerBounds 1x1 1 logical
  nonEmptyUpperBounds 1x1 1 logical
  objfun 1x1 16 function_handle
  options 1x1 5166 struct
  pass_cnt 1x1 8 double
  scaleObjConstr 1x1 1 logical
  sizes 1x1 264 struct
  ub 0x0 0 double
  varargin 0x0 0 cell
  variables 1x580 4640 double
  xCElementOrig 1x1 8 double
  xCurrent 580x1 4640 double

I can't see many other variables that should be defined, like nquad for instance (it was defined when fsolve was called). I can't even see x_0... Is this normal?

Subject: Matrix dimensions must agree with fsolve

From: Sebastiaan

Date: 30 Sep, 2009 19:14:03

Message: 6 of 9

Ok, then I will teach you some bugtracking :)

So, this is the error stack:

Error in ==> finitedifferences at 137
                gradf(:,pass_cnt) = (fplus(:) - fCurrent)/deltaX(gcnt);

Error in ==> levenbergMarquardt at 223
            [JAC,unused1,unused2,numEvals] = finitedifferences(XOUT,funfcn{3},[],[],[], ...

Error in ==> fsolve at 385
    [x,FVAL,JACOB,EXITFLAG,OUTPUT,msg] = ...

Error in ==> computeGussPointsWeights2D at 92
    [wxi, fval, exitflag, output] = fsolve(F, x_0, options)

And these are your variables:
> K>> whos
> Name Size Bytes Class Attributes
>
> deltaX 580x1 4640 double
> fCurrent 290x1 2320 double
> fplus 1x289 2312 double
> gcnt 1x1 8 double

Error in ==> finitedifferences at 137
                gradf(:,pass_cnt) = (fplus(:) - fCurrent)/deltaX(gcnt);

Now you can see that fplus and fCurrent are not of the same size, so they cannot be subtracted.

Question 1: is your problem size 290 or 289? I guess 290, so fplus is computed incorrect.

Check the file finitedifferences.m to see where fplus is computed. Probably here:
    fplus = feval(funfcn{3},xargin{:},vararginAdjusted{:});

Now I would like to help you further, but I need to know your Maltab version, since I have a different version of finitedifferences.

> I can't see many other variables that should be defined, like nquad for instance (it was defined when fsolve was called). I can't even see x_0... Is this normal?
Yes, they are out-of-scope.

Making a function:
function test(a,b,c)
d = a*b*c;
disp(d)

will not show d in your command window, and within this function, only a,b,c and d are defined, and none other.

So:
>> Boo = 5;
>> a = 10;
>> test(Boo, a, 6)
means that in the test function, a=5 (not 10!), b=10, c=6 and d will be 300. It does not know Boo.

Sebastiaan

Subject: Matrix dimensions must agree with fsolve

From: Luca Carlon

Date: 1 Oct, 2009 07:24:02

Message: 7 of 9

"Sebastiaan " <s.breedveld@erasmusmc.REMOVE.BOO.BOO.nl> wrote in message <ha0alr$fl$1@fred.mathworks.com>...
> Ok, then I will teach you some bugtracking :)
>
> So, this is the error stack:
>
> Error in ==> finitedifferences at 137
> gradf(:,pass_cnt) = (fplus(:) - fCurrent)/deltaX(gcnt);
>
> Error in ==> levenbergMarquardt at 223
> [JAC,unused1,unused2,numEvals] = finitedifferences(XOUT,funfcn{3},[],[],[], ...
>
> Error in ==> fsolve at 385
> [x,FVAL,JACOB,EXITFLAG,OUTPUT,msg] = ...
>
> Error in ==> computeGussPointsWeights2D at 92
> [wxi, fval, exitflag, output] = fsolve(F, x_0, options)
>
> And these are your variables:
> > K>> whos
> > Name Size Bytes Class Attributes
> >
> > deltaX 580x1 4640 double
> > fCurrent 290x1 2320 double
> > fplus 1x289 2312 double
> > gcnt 1x1 8 double
>
> Error in ==> finitedifferences at 137
> gradf(:,pass_cnt) = (fplus(:) - fCurrent)/deltaX(gcnt);
>
> Now you can see that fplus and fCurrent are not of the same size, so they cannot be subtracted.
>
> Question 1: is your problem size 290 or 289? I guess 290, so fplus is computed incorrect.

Yes, your guess is correct. 290. I just discovered one mistake. The creation of the initial guess was wrong: I was creating a longer guess. I corrected this mistake, but fsolve fails nonetheless.

> Check the file finitedifferences.m to see where fplus is computed. Probably here:
> fplus = feval(funfcn{3},xargin{:},vararginAdjusted{:});
>
> Now I would like to help you further, but I need to know your Maltab version, since I have a different version of finitedifferences.

I'm running Matlab R2009a.

> > I can't see many other variables that should be defined, like nquad for instance (it was defined when fsolve was called). I can't even see x_0... Is this normal?
> Yes, they are out-of-scope.
>
> Making a function:
> function test(a,b,c)
> d = a*b*c;
> disp(d)
>
> will not show d in your command window, and within this function, only a,b,c and d are defined, and none other.
>
> So:
> >> Boo = 5;
> >> a = 10;
> >> test(Boo, a, 6)
> means that in the test function, a=5 (not 10!), b=10, c=6 and d will be 300. It does not know Boo.

Yes, this is clear. So when I use dbstop, the process is stopped in finitedifferences, right?
Thanks for your help Sebastiaan, it is really appreciated!

Luca

Subject: Matrix dimensions must agree with fsolve

From: Sebastiaan

Date: 1 Oct, 2009 09:08:03

Message: 8 of 9

"Luca Carlon" <carlon.lucaREMOVE_THIS@gmail.com> wrote in message <ha1lei$5da$1@fred.mathworks.com>...
> "Sebastiaan " <s.breedveld@erasmusmc.REMOVE.BOO.BOO.nl> wrote in message <ha0alr$fl$1@fred.mathworks.com>...
> > Ok, then I will teach you some bugtracking :)
> >
> > So, this is the error stack:
> >
> > Error in ==> finitedifferences at 137
> > gradf(:,pass_cnt) = (fplus(:) - fCurrent)/deltaX(gcnt);
> >
> > Error in ==> levenbergMarquardt at 223
> > [JAC,unused1,unused2,numEvals] = finitedifferences(XOUT,funfcn{3},[],[],[], ...
> >
> > Error in ==> fsolve at 385
> > [x,FVAL,JACOB,EXITFLAG,OUTPUT,msg] = ...
> >
> > Error in ==> computeGussPointsWeights2D at 92
> > [wxi, fval, exitflag, output] = fsolve(F, x_0, options)
> >
> > And these are your variables:
> > > K>> whos
> > > Name Size Bytes Class Attributes
> > >
> > > deltaX 580x1 4640 double
> > > fCurrent 290x1 2320 double
> > > fplus 1x289 2312 double
> > > gcnt 1x1 8 double
> >
> > Error in ==> finitedifferences at 137
> > gradf(:,pass_cnt) = (fplus(:) - fCurrent)/deltaX(gcnt);
> >
> > Now you can see that fplus and fCurrent are not of the same size, so they cannot be subtracted.
> >
> > Question 1: is your problem size 290 or 289? I guess 290, so fplus is computed incorrect.
>
> Yes, your guess is correct. 290. I just discovered one mistake. The creation of the initial guess was wrong: I was creating a longer guess. I corrected this mistake, but fsolve fails nonetheless.
>
> > Check the file finitedifferences.m to see where fplus is computed. Probably here:
> > fplus = feval(funfcn{3},xargin{:},vararginAdjusted{:});
> >
> > Now I would like to help you further, but I need to know your Maltab version, since I have a different version of finitedifferences.
>
> I'm running Matlab R2009a.
>
Ok, let's continue then. fplus is computed on line 128:
            fplus = feval(objfun,reshape(xCurrent,sizes.xRows,sizes.xCols),varargin{:});
 and it evaluates function 'objfun' with size 'sizes.xRows by sizes.xCols', parsing additional arguments from the finitedifferences function to objfun.

So something goes wrong here. xCurrent has length 580, so I guess xRows=290 and xCols=2. Nothing wrong so far, so debug the function that is called and try to find out why it returns a vector of length 289.

You probably know which function is called by objfun. Set a breakpoint there and call line 128 (copy/paste in the command window) again, while still being in the debugger.


>
> Yes, this is clear. So when I use dbstop, the process is stopped in finitedifferences, right?
Yes, so you can check the current values of the variables. Once interrupted, call
 fplus = feval(objfun,reshape(xCurrent,sizes.xRows,sizes.xCols),varargin{:});
and it evaluates the function with the current variables.

With dbup you can go up 1 level to levenbergMarquardt.m

> Thanks for your help Sebastiaan, it is really appreciated!
>
> Luca

Subject: Matrix dimensions must agree with fsolve

From: Luca Carlon

Date: 6 Oct, 2009 07:23:02

Message: 9 of 9

"Sebastiaan " <s.breedveld@erasmusmc.REMOVE.BOO.BOO.nl> wrote in message <ha1rhj$9e9$1@fred.mathworks.com>...
> "Luca Carlon" <carlon.lucaREMOVE_THIS@gmail.com> wrote in message <ha1lei$5da$1@fred.mathworks.com>...
> > "Sebastiaan " <s.breedveld@erasmusmc.REMOVE.BOO.BOO.nl> wrote in message <ha0alr$fl$1@fred.mathworks.com>...
> > > Ok, then I will teach you some bugtracking :)
> > >
> > > So, this is the error stack:
> > >
> > > Error in ==> finitedifferences at 137
> > > gradf(:,pass_cnt) = (fplus(:) - fCurrent)/deltaX(gcnt);
> > >
> > > Error in ==> levenbergMarquardt at 223
> > > [JAC,unused1,unused2,numEvals] = finitedifferences(XOUT,funfcn{3},[],[],[], ...
> > >
> > > Error in ==> fsolve at 385
> > > [x,FVAL,JACOB,EXITFLAG,OUTPUT,msg] = ...
> > >
> > > Error in ==> computeGussPointsWeights2D at 92
> > > [wxi, fval, exitflag, output] = fsolve(F, x_0, options)
> > >
> > > And these are your variables:
> > > > K>> whos
> > > > Name Size Bytes Class Attributes
> > > >
> > > > deltaX 580x1 4640 double
> > > > fCurrent 290x1 2320 double
> > > > fplus 1x289 2312 double
> > > > gcnt 1x1 8 double
> > >
> > > Error in ==> finitedifferences at 137
> > > gradf(:,pass_cnt) = (fplus(:) - fCurrent)/deltaX(gcnt);
> > >
> > > Now you can see that fplus and fCurrent are not of the same size, so they cannot be subtracted.
> > >
> > > Question 1: is your problem size 290 or 289? I guess 290, so fplus is computed incorrect.
> >
> > Yes, your guess is correct. 290. I just discovered one mistake. The creation of the initial guess was wrong: I was creating a longer guess. I corrected this mistake, but fsolve fails nonetheless.
> >
> > > Check the file finitedifferences.m to see where fplus is computed. Probably here:
> > > fplus = feval(funfcn{3},xargin{:},vararginAdjusted{:});
> > >
> > > Now I would like to help you further, but I need to know your Maltab version, since I have a different version of finitedifferences.
> >
> > I'm running Matlab R2009a.
> >
> Ok, let's continue then. fplus is computed on line 128:
> fplus = feval(objfun,reshape(xCurrent,sizes.xRows,sizes.xCols),varargin{:});
> and it evaluates function 'objfun' with size 'sizes.xRows by sizes.xCols', parsing additional arguments from the finitedifferences function to objfun.
>
> So something goes wrong here. xCurrent has length 580, so I guess xRows=290 and xCols=2. Nothing wrong so far, so debug the function that is called and try to find out why it returns a vector of length 289.
>
> You probably know which function is called by objfun. Set a breakpoint there and call line 128 (copy/paste in the command window) again, while still being in the debugger.
>
>
> >
> > Yes, this is clear. So when I use dbstop, the process is stopped in finitedifferences, right?
> Yes, so you can check the current values of the variables. Once interrupted, call
> fplus = feval(objfun,reshape(xCurrent,sizes.xRows,sizes.xCols),varargin{:});
> and it evaluates the function with the current variables.
>
> With dbup you can go up 1 level to levenbergMarquardt.m
>
> > Thanks for your help Sebastiaan, it is really appreciated!
> >
> > Luca

Thank you for your help! Debugging I found the error was in my function.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
nonlinear syste... Sprinceana 30 Sep, 2009 11:10:50
matrix Sprinceana 30 Sep, 2009 11:10:38
matrix dimensions Sprinceana 30 Sep, 2009 11:10:38
fsolve Sprinceana 30 Sep, 2009 11:10:38
rssFeed for this Thread

Contact us at files@mathworks.com