Matlab Curve Fitting via Optimization

조회 수: 4 (최근 30일)
Bethany Baxter
Bethany Baxter 2018년 2월 16일
댓글: Birsen Ayaz-Maierhafer 2022년 7월 29일
I have tried to follow this tutorial to fit a curve to my dataset . The equation for the curve should be f(t)=log10((wpmcoeff./(t.^2))+((1.038+3.*log(2.*pi.*1e6.*t)).*fpmcoeff./(t.^2))+(wfmcoeff./t))+(ffmcoeff)+(rwfmcoeff.*t).
I have created the following code:
clock='atomicclockgpsworld.txt';
data=importdata(clock);
carrier=10e6;
sig=data(:,2);
t=data(:,1);
sigsq=log10(sig.^2);
fun = @(coeff)sseval(coeff,t,sigsq);
x0 = rand(5,1);
bestx = fminsearch(fun,x0);
wpmcoeff = bestx(1);
fpmcoeff = bestx(2);
wfmcoeff = bestx(3);
ffmcoeff = bestx(4);
rwfmcoeff = bestx(5);
yfit=log10((wpmcoeff./(t.^2))+((1.038+3.*log(2.*pi.*1e6.*t)).*fpmcoeff./(t.^2))+(wfmcoeff./t))+(ffmcoeff)+(rwfmcoeff.*t);
semilogx(t,sigsq,'x');
hold on
semilogx(t,yfit);
saveas(gcf,'fit','png');
and the corresponding function
function sse = sseval(coeff,t,sigsq)
wpmcoeff = coeff(1);
fpmcoeff = coeff(2);
wfmcoeff = coeff(3);
ffmcoeff = coeff(4);
rwfmcoeff = coeff(5);
sse = sum(sigsq - (log10((wpmcoeff./(t.^2))+((1.038+3.*log(2.*pi.*1e6.*t)).*fpmcoeff./(t.^2))+(wfmcoeff./t)+(ffmcoeff)+(rwfmcoeff.*t))));
end
But the fit produced is horrible (my y data should vary between approximately -20 to -22 but the fit produces a curve that reaches 1e59!). Can anyone suggest where I may be going wrong?
  댓글 수: 3
Bethany Baxter
Bethany Baxter 2018년 2월 16일
Hi Torsten,
Thank you for your help. I have now squared the differences and removed the log from my function (it was not necessary, purely for formatting purposes). I have also used fmincon in place of fminsearch to constrain my coefficients between 0-1.
Code:
rng default % for reproducibility
clock='atomicclockgpsworld.txt';
data=importdata(clock);
carrier=10e6;
sig=data(:,2);
t=data(:,1);
sigsq=sig.^2;
fun = @(coeff)sseval(coeff,t,sigsq);
x0 = rand(5,1);
%bestx = fminsearch(fun,x0);
ub = [1,1,1,1,1];
lb = [realmin,realmin,realmin,realmin,realmin];
bestx = fmincon(fun,x0,[],[],[],[],lb,ub);
wpmcoeff = bestx(1);
fpmcoeff = bestx(2);
wfmcoeff = bestx(3);
ffmcoeff = bestx(4);
rwfmcoeff = bestx(5);
yfit=(wpmcoeff./(t.^2))+((1.038+3.*log(2.*pi.*1e6.*t)).*fpmcoeff./(t.^2))+(wfmcoeff./t)+(ffmcoeff)+(rwfmcoeff.*t);
figure
loglog(t,sigsq,'x');
hold on
loglog(t,yfit,'x');
saveas(gcf,'fit','png');
Function:
function sse = sseval(coeff,t,sigsq)
wpmcoeff = coeff(1);
fpmcoeff = coeff(2);
wfmcoeff = coeff(3);
ffmcoeff = coeff(4);
rwfmcoeff = coeff(5);
sse = sum((sigsq - ((wpmcoeff./(t.^2))+((1.038+3.*log(2.*pi.*1e6.*t)).*fpmcoeff./(t.^2))+(wfmcoeff./t)+(ffmcoeff)+(rwfmcoeff.*t))).^2);
end
However I still cannot get a curve which matches up with my data points. Do you have any other suggestions?
Birsen Ayaz-Maierhafer
Birsen Ayaz-Maierhafer 2022년 7월 29일
Hi Bethany, I have exactly the the same issue here. Were you ever able to resolve this issue? If so, how? Thank you

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

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