fminsearch application for fitting some data

1 view (last 30 days)
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?

Answers (2)

Star Strider
Star Strider on 27 May 2016
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 Comments
Angelo Giuseppe Spinosa
Angelo Giuseppe Spinosa on 27 May 2016
Edited: Angelo Giuseppe Spinosa on 27 May 2016
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 on 27 May 2016
you’re asking fminsearch to minimise a linear function. That minimum will be -Inf, by definition.
You simply cannot escape that reality.

Sign in to comment.


Matt J
Matt J on 27 May 2016
Edited: Matt J on 27 May 2016
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 Comments
Angelo Giuseppe Spinosa
Angelo Giuseppe Spinosa on 27 May 2016
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 on 27 May 2016
Edited: Matt J on 27 May 2016
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(:);

Sign in to comment.

Categories

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!