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
0 件のコメント
採用された回答
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
0 件のコメント
その他の回答 (2 件)
Sean de Wolski
2013 年 1 月 7 日
rand(1,2,2) will create a 1x2x2 array. Perhaps you meant rand(1,3)?
0 件のコメント
Shing
2013 年 1 月 7 日
1 件のコメント
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.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!