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?

回答(2 个)

Star Strider
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
编辑:Angelo Giuseppe Spinosa 2016-5-27
You are right, I didn't paste the code because the function errorFunction is simply:
function f = errorFunction(k,R,G,F,cosTheta)
% Linear relationship between the function f = deltaR and the
% factor k.
f = (R*G*F*cosTheta)*k;
end
My objective is to find a linear relationship as discussed before, in which k parameter is set for a dimensional compatibility between different physical quantities, like forces, gauge factors and degrees that are derived from a series of sensors. In my case, I would to fit a vector of angles (for which I compute the cosine) with such type of mathematical law.
Star Strider
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
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 个评论
Angelo Giuseppe Spinosa
If I would to follow you suggestions, how may I change the fitting() function? Because I don't understand which is the value of f in both cases: it should be initialized, no?
Matt J
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 CenterFile 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!

Translated by