Function - Problem with fminsearch - subscripted assignment dimension mismatch

I am writing a function that is part of a larger project as a demonstration for some students. The program is supposed to do a least squares fit on functions inputted.
The error is as follows:
??? Subscripted assignment dimension mismatch.
Error in ==> fminsearch at 191 fv(:,1) = funfcn(x,varargin{:});
The program is: function [c,res,model1]=lstsq(funct,xdata,ydata,start_point) model1=@fun1; options=optimset('Display','iter','MaxIter',1e40,'MaxFunEvals',1e40,'TolX',1e-40,'TolFun',1e-40); [c,res]=fminsearch(model1,start_point,options);
function [res,Funct]=fun1(c)
Funct=str2func(funct);
res=Funct(c,xdata)-ydata;
end
end
Yes, I realize that there is a nonlinear least squares program already, but I reiterate that this is for a demonstration, so I need to understand why this program gives the error it does. Here are the inputs:
>>lstsq('@(c,xdata)(10.^(-xdata)./(10.^(-xdata)+c(1)))',X,Y,1)
X=[1.20000000000000;2;3.06000000000000;3.77000000000000;4.03000000000000;4.42000000000000;4.90000000000000;8.14000000000000;8.84000000000000;8.88000000000000;10.0800000000000;11.3600000000000]
(this is a 12x1 matrix)
Y=1;0.975446654000000;0.865950488000000;0.585195335000000;0.418113;25000000;0.236419836000000;0.0907522820000000;0;0;0;0;0]
(this is a 12x1 matrix)
Any thoughts as to why fminsearch rejects it with this error? Any help is much appreciated.

 采纳的回答

It does this because fminsearch know how to minimizes only a SINGLE scalar valued function. This is what it expects. Does your function return a scalar value?
To me, it appears that your function returns a vector of residuals. How should fminsearch know that what you really wanted it to minimize is the sum of squares of the residuals, when it sees an entire vector of numbers?
Computers do only what they are told, only how they are programmed to run. When you try to confuse them, they tend to be easily confused.

更多回答(0 个)

类别

Community Treasure Hunt

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

Start Hunting!

Translated by