fminsearch doesn't converge to the right value
Afficher commentaires plus anciens
Hi all,
I am trying to solve and minimize the following exponential fit, trying to retrieve the exponential term.
I am using fminsearch to minimize the "sum-squared-error cost function" but somehow it is not really working. I am sure I am making some stupid mistake but I can't figure it out ... It should give me back the 0.1 value but never really get it (even if noise is 0).
Here the code:
x=0:100;
y=exp(-0.1*x)'+0.1*rand(101,1);
plot(x,y)
hold on
%%
[h, ff, s] = sc(0.5, x, y)
plot(x, y)
plot(x,h,x,y,x,ff)
% hold on
% s
function [h ff s]=sc(k, x, y)
co=[exp(-k*x)' ones(size(y))]\y;
%
% ff=co(1)*exp(-k*x)'+co(2)*ones(size(y));
% h=y-ff;
% s=sum(h.^2);
%
ff = @(k,x) co(1).*exp(-k.*x)'+co(2).*ones(size(y));
h = @(k) sum((y-ff(k,x)).^2);
options = optimset('PlotFcns',@optimplotfval,'Display','iter');
s = fminsearch(h,k,options);
ff=exp(-s*x)'+co(2)*ones(size(y));
h=y-ff;
end
Thanks for help,
E
Réponse acceptée
Plus de réponses (1)
Milan Bansal
le 3 Mai 2024
Modifié(e) : Milan Bansal
le 3 Mai 2024
I understand that you are facing some issues when using "fminsearch" for optimizing the "sum-squared-error" cost function to find the best fit for the exponential model
, where A is a constant, B is noise, and "k" is the exponential decay rate you are trying to estimate.
Please refer to the following points to estimate the value of "k":
- Define an objective function that calculates the sum-squared-error between the model predictions and the actual data for a given "k". This function will be minimized to find the best "k".
- Use the fminsearch function to find the "k" that minimizes the objective function. Start with an initial guess for "k" and let fminsearch adjust "k" to minimize the sum-squared-error.
Here is how you can modify your code to resolve the issue:.
x = 0:100;
y = exp(-0.1*x)' + 0.1*randn(101,1);
plot(x, y, 'b.'); % Plotting the original data
hold on;
initialGuess = 0.5; % Initial guess for k
[kOpt, fval] = optimizeK(initialGuess, x, y); % Optimization to find the best k
% Generating the fitted curve with the optimized k
yFitted = exp(-kOpt*x);
plot(x, yFitted, 'r-'); % Plotting the fitted curve
title(sprintf('Fitted Curve with k = %f', kOpt));
hold off;
function [kOpt, fval] = optimizeK(kInitial, x, y)
objectiveFunction = @(k) sum((y - exp(-k*x)').^2); % Objective function to minimize sum-squared-error
% Using fminsearch to find the optimal k
options = optimset('PlotFcns',@optimplotfval, 'Display', 'iter');
[kOpt, fval] = fminsearch(objectiveFunction, kInitial, options);
end
The value of "k" estimated from above code is 0.094, which is close to the desired value 0.1. The difference is due to the added noise. If the noise become zero, resultant "k" will be 0.1.
Please refer to the following documentation to learn more about fminsearch.
Hope this helps!
2 commentaires
Edoardo_a
le 3 Mai 2024
John D'Errico
le 3 Mai 2024
Modifié(e) : John D'Errico
le 3 Mai 2024
Argh. NO!!!! This answer was completely wrong! It did not use a partitioned least squares estimator! It just estimated k, given the model
y= exp(-k*x)
You need to see the difference. What was done here was @Milan Bansal did nothing more than assume the above model, even though initially it ws stated the model would be
y = A*exp(-k*x) + B
I did say before that very few people seem to appreciate what a partitioned least squares estimation is and how it works. This is a good example.
Catégories
En savoir plus sur Get Started with Curve Fitting Toolbox dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




