fminsearch application for fitting some data
1 view (last 30 days)
Show older comments
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 Comments
Answers (2)
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
on 27 May 2016
Edited: Angelo Giuseppe Spinosa
on 27 May 2016
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.
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
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(:);
See Also
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!