fminsearch application for fitting some data

조회 수: 3 (최근 30일)
Angelo Giuseppe Spinosa
Angelo Giuseppe Spinosa 2016년 5월 27일
댓글: Star Strider 2016년 5월 27일
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일
편집: 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
Angelo Giuseppe Spinosa 2016년 5월 27일
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