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