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

1 view (last 30 days)
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

Accepted Answer

Matt J
Matt J on 7 Jan 2013
Edited: Matt J on 7 Jan 2013
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

More Answers (2)

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

Shing
Shing on 7 Jan 2013
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 Comment
Matt J
Matt J on 7 Jan 2013
Edited: Matt J on 7 Jan 2013
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!