fminsearch application for fitting some data
1 回表示 (過去 30 日間)
古いコメントを表示
Good evening to everybody. I'm trying to find a solution for the following problem. I have a function f = errorFunction(k,R,G,F,cosTheta) in a linear form:
f = (R*G*F*cosTheta)*k
I've created a function like this one below:
function optimalK = fitting(mass,gaugeFactor,resistance,theta)
initK = 1e-2;
optimalK = zeros(1,length(theta));
options = optimset('TolX',1e-8,'MaxIter',Inf,'MaxFunEvals',5000);
for i=1:length(optimalK)
cosTheta = cosd(theta(i));
optimalK(i) = fminsearch(@(k) errorFunction(k,resistance,gaugeFactor,9.81*mass,cosTheta),initK,options);
end
end
Moreover, I have a vector of angles avgCommerc that come from several measurements in which a commercial inclinometer has been used. When I call the function fitting for getting the minimum k of the function errorFunction, like explained here:
%%STEP 2 - Fitting
% Fitting requires the degrees of the commercial inclinometer on the x
% axis and the ratio deltaR/R on the y axis.
mass = 1;
gaugeFactor = 1;
resistance = 1;
optimalK = fitting(mass,gaugeFactor,resistance,avgCommerc);
disp(sprintf('\n--- STEP 2 - Polynomial fitting: COMPLETE'));
the procedure doesn't work well: it gives me the error
Exiting: Maximum number of function evaluations has been exceeded
- increase MaxFunEvals option.
Current function value: -Inf
How can I solve the problem?
0 件のコメント
回答 (2 件)
Star Strider
2016 年 5 月 27 日
The problem appears to be in ‘errorFunction’. You didn’t post it, so we can’t suggest a specific solution.
It would also help if you told us what you want to do as well as posting your code.
2 件のコメント
Angelo Giuseppe Spinosa
2016 年 5 月 27 日
編集済み: Angelo Giuseppe Spinosa
2016 年 5 月 27 日
Star Strider
2016 年 5 月 27 日
you’re asking fminsearch to minimise a linear function. That minimum will be -Inf, by definition.
You simply cannot escape that reality.
Matt J
2016 年 5 月 27 日
編集済み: Matt J
2016 年 5 月 27 日
I see no "error" calculation in your errorFunction. Presumably, it should actually be
function error = errorFunction(k,R,G,F,cosTheta,f)
% Linear relationship between the function f = deltaR and the
% factor k.
error = norm( f - (R*G*F*cosTheta)*k);
end
where f is some measurement vector.
However, it is overkill to use fminsearch for this. A linear fit can be done as simply as
k=(R*G*F*cosTheta)\f;
2 件のコメント
Matt J
2016 年 5 月 27 日
編集済み: Matt J
2016 年 5 月 27 日
In order to "fit" an equation you need measurements of both left and right hand side quantities. In your case, it looks like you have multiple measurements of cosTheta on the right hand side and presumably also multiple corresponding measurements of f on the left hand side.
As I said, your fitting() function could then just calculate k as,
k=(R*G*F*cosTheta(:))\f(:);
参考
カテゴリ
Help Center および File Exchange で Get Started with Curve Fitting Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!