fminsearch doesn’t converge to the right value
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,
EHi 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 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 fminsearch, curve fitting MATLAB Answers — New Questions