필터 지우기
필터 지우기

For fminsearch: ??? Attempted to access params(3); index out of bounds because numel(params)=2.

조회 수: 1 (최근 30일)
Hi, the objective of the code below is to use fminsearch function for curve fitting purposes so that I will be able to get unknown parameters T1,T2 and C by plotting xdata against ydata which I already have in my database.
I have previously managed to find the value of parameters T1 and T2. However, when I introduced another third parameter C, I could got this error which I could not resolve:
??? Attempted to access params(3); index out of bounds because numel(params)=2.
Below is the code:
--------------------------------------------------------------------------------------------------------------------------------------- FUNCTION CODE:
function [estimates, model] = fmmintest(xdata, ydata) % A random starting point for fminsearch. start_point = rand(1, 2, 2); model = @expfun; estimates = fminsearch(model, start_point);
function [sse, FittedCurve] = expfun(params)
T1 = params(1);
T2 = params(2);
C = params(3);
FittedCurve =C{(T2^2)*(sin(xdata)).^4 +(T1^2)*(cos(xdata).^4)};
FittedCurve = cell2mat(FittedCurve);
ErrorVector = FittedCurve - ydata;
sse = sum(ErrorVector .^ 2);
end
end
Any help will be much appreciated ! Thanks !!
Regards

채택된 답변

Matt J
Matt J 2013년 1월 7일
편집: Matt J 2013년 1월 7일
If you have only 3 parameters, why do you have 4 elements in your start_point array?
>> start_point=rand(1,2,2); numel(start_point)
ans =
4
That wouldn't account for the error you're seeing, however.
I'm a bit skeptical that we're seeing your exact code. It sounds like you're initializing somehow using the previous code with numel(start_point)=2

추가 답변 (2개)

Sean de Wolski
Sean de Wolski 2013년 1월 7일
rand(1,2,2) will create a 1x2x2 array. Perhaps you meant rand(1,3)?

Shing
Shing 2013년 1월 7일
Yes, I made a terrible mistake. I should put rand(1,3) instead. Thank you Sean and Matt!
And Matt, could you please elaborate on what you are skeptical about, I don't quite get what you mean about the initializing part. I previously did the code for two parameters only, and now I'm including the third parameter C.Thank you !
  댓글 수: 1
Matt J
Matt J 2013년 1월 7일
편집: Matt J 2013년 1월 7일
In the error message that you showed us, numel(params)=2 implies that fminsearch thought that you were are solving a 2-parameter problem. fminsearch deduces this from numel(start_point). But in the code you showed, numel(start_point) was equal to 4. I therefore cannot see how it would have assumed you were working in R^2 if the code you showed us was actually the code being run.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by