How to write function according to variables? fminspleas
3 ビュー (過去 30 日間)
古いコメントを表示
fminspleas in file exchange seems to be a very good tool. I wonder if anyone has good experience on it.
If the data can be represented as:
y = a0 + a1*sin(c1*x+c2) + a2*sin(c3*x+c4) + a3*sin(c5*x+c6);
Should the funlist be:
funlist = {1, @(c,xdata) sin(c(1)*xdata+c(2)), @(c,xdata) sin(c(3)*xdata+c(4)), @(c,xdata) sin(c(5)*xdata+c(6))};
Right? Or can it be a simpler formula?
More, if the term numbers are changing depending on cases, how to write it iteratively and then make it function?
functext = '';
for nt =1:N,
functext = sprintf('%s @(c,xdata) sin(c(%d)*xdata+c(%d))',functext,1+2*(nt-1),2*nt);
end
eval? str2func?
Thanks.
2 件のコメント
Madhav Rajan
2015 年 8 月 5 日
I understand that you want to define the function list to pass to the 'fminpleas' solver.
You can refer the following example that has been taken from the documentation of 'fminpleas' to define the function list:
% Fit a sum of several sine terms to data. Beware of terms
% with a similar form - you must use distinct starting
% values for the nonlinear parameters.
%
% x = rand(500,1);
% y = 4 - 3*sin(2*x) + 2*cos(3*x) + randn(size(x))/10;
%
% funlist = {1, @(c,xdata) sin(c(1)*xdata), @(c,xdata) cos(c(2)*xdata)};
% [INLP,ILP] = fminspleas(funlist,[3 4],x,y)
%
% INLP =
% 1.91830441497209 3.04626358883634
%
% ILP =
% 4.16097130716892
% -3.21354044158414
% 1.86372562015106
%
% See also: fminsearch, fminsearchbnd, pleas, lsqcurvefit
% Note: pleas is found as an adjunct to my optimtips document.
You can refer the following link for more information on the 'fminpleas' function:
Regarding your second question, could you please provide more details on why you would want to change the term numbers during every iteration, since it changes your objective function?
採用された回答
Stephen23
2015 年 8 月 5 日
for k = N:-1:2
funlist{k} = @(c,xdata) sin(c(2*k-3)*xdata+c(2*k-2));
end
funlist{1} = 1;
String evaluation is a buggy and slow way to program, and TMW themselves advise that it should be avoided. Beginners love to use eval everywhere in their code, however it actually creates more problems than it solves and removes all of MATLAB's inbuilt code helping tools. Read this to know more:
0 件のコメント
その他の回答 (0 件)
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!