搜索
Hello,
I got the following error when I was trying to run the codes below. I'm glad if someone can tell me the reason for getting this error.
Thank you!
Error:
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is
1-by-201.
Error in fminsearch (line 201)
fv(:,1) = funfcn(x,varargin{:});
Error in fminsearchtest (line 47)
[fitparams_out, fval, ~, ~] = fminsearch(fn, fitparams_guess, options);
Code:
options = optimset('Display', 'iter', ...
'TolX', 1e-6, ... %Termination tolerance on x, default is 1e-4
'TolFun', 1e-6, ... %Term. tolerance on function value, default 1e-4
'MaxFunEvals', 1000 );
fitparams_guess=[0 0 0];
Noise = 2;
C0=pi;
C1 = 2;
C2 = 0.8;
x = -10:0.1:10;
y = C0 + C1*x + C2 * x.^2 + Noise*randn(1,length(x));
fn = @(fitparams) calc_residual(fitparams, x,y); %function handle
[fitparams_out, fval, ~, ~] = fminsearch(fn, fitparams_guess, options);
function residual = calc_residual (fitparams, x,y)
for i =1:length(x)
ymod = fitparams(1) + fitparams(2)*x(i) + fitparams(3) * x(i).^2 ;
end
residual = y-ymod;
end