lsqcurvefit help - Field assignment to a non-structure array object
이전 댓글 표시
Hi,
I am getting the error "Field assignment to a non-structure array object" in the line I call the lsqcurvefit function and I don't understand why.
I try to fit 2 different models. When I fit model_1 everything is fine. When I fit model_2 I got the error.
a, b - both vectors of size [1,41].
I have the following code:
opts = optimset('Display' ,'off');
for i=1:size(Image,1)
x(i,:) = lsqcurvefit (@model_1, init, a(1:end-1), Image(i, 1:end-1), zeros(size(init)), init*10, opts)
end
for i=1:size(Image,1)
x(i,:) = lsqcurvefit (@model_2, init, a(1:end-1), b(1:end-1), Image(i, 1:end-1), zeros(size(init)), init*10, opts)
end
Why do I get the error when I have one more entry in model_2? i.e. the vector b?
function y = model_2(p, a, b)
A = p(1);
B = p(2);
C = p(3);
D = p(4);
E = p(5);
y = A*exp(-a*C-b/D)+(1-A)*exp(-a*B).*(E*exp(-b/100)+(1-E)*exp(-b/40));
end
function y = model_1(p,a)
A = p(1);
B = p(2);
C = p(3);
y = A*exp(-B*a) + (1-A)*exp(-C*a);
end
답변 (1개)
Walter Roberson
2019년 1월 14일
편집: Walter Roberson
2019년 1월 15일
0 개 추천
lsqcurvefit must have parameter order
- objective. @model_1 or @model_2
- x0. init in both cases
- xdata. a(1:end-1) in both cases
- ydata. Image(1:end-1) in the first case and b(1:end-1) in the second case
- lb. zeroes in the first case and Image(1:end-1) in the second case
- ub. init*10 in the first case and zeroes in the second case
- options. opt (a struct) in the first case and zeroes in the second case
- no documented parameter . absent in the first case and opt (a struct) in the second case.
Internally the code attempts to add additional fields to the struct expected in the 7th position and fails when the parameter is numeric zeroes .
댓글 수: 16
Walter Roberson
2019년 1월 14일
lsqcurvefit is not designed for surface fitting. however you could probably parameterize
lsqcurvefit(@(p,a) model_2(p,a,b(1:end-1)) ....
Torsten
2019년 1월 15일
for i=1:size(Image,1)
x(i,:) = lsqcurvefit (@(p,a)model_2(p,a,b), init, a(1:end-1), Image(i, 1:end-1), zeros(size(init)), init*10, opts)
end
Torsten
2019년 1월 15일
According to the function in model_2, the dimensions of a and b must agree.
Torsten
2019년 1월 15일
Was lsqcurvefit successful in determining p (check the exitflag) ?
Are Image(i,1:end-1) different for all i ?
Did you supply a sensful 5-element vector "init" ?
Torsten
2019년 1월 16일
for i=1:size(Image,1)
[p(i,:),resnorm,residual,exitflag,output] = lsqcurvefit(@(p,a)model_2(p,a,b(1:end-1)), init, a(1:end-1), Image(i, 1:end-1), zeros(size(init)), init*10, opts);
exitflag
end
Torsten
2019년 1월 16일
Means
Number of iterations exceeded options.MaxIterations or number of function evaluations exceeded options.MaxFunctionEvaluations.
Mar
2019년 1월 16일
Torsten
2019년 1월 16일
Before calling "lsqcurvefit", I'd evaluate "model_2" with init, a and b as input and see if it returns senseful values.
Mar
2019년 1월 16일
Torsten
2019년 1월 16일
Then you should do as "lsqcurvefit" suggests: Set a larger value for the maximum number of iterations:
options.MaxIterations and/or options.MaxFunctionEvaluations
카테고리
도움말 센터 및 File Exchange에서 Common Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!