
least square fit "lsqcurvefit" not good enough?
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi All,
Trying to solve "K_a" by least square fitting an equation "func" to a dataset "backgroundImpIm". Bu when I plot them together, the fit doesn't seem good. I have tried to tweak FunctionTolerance and StepTolerance but the fit doesn't improve. Anyone have answers?
Thanks,
clc
close all
clear
background = csvread('0SM.CSV',3,0,[3,0,1603,2]);
backgroundImpIm = background(:,3);
f = background(:,1);
freq = f(1:100);
x0 = [1];
h = 5E-3;
a = 5E-5;
m_e = 9.1E-31;
q = 1.6E-19;
c = 3E8;
omega = 2*pi*freq;
k_0 = omega/c;
beta = k_0;
epsilon_real = 1;
epsilon_im = 0;
x = epsilon_im./epsilon_real;
func = @(K_a,freq)(-K_a./(tan(2*pi*freq*h.*(1 + 0.19./((K_a/60 +1) - 0.81))/c)));
%options = optimoptions(@lsqcurvefit,'FunctionTolerance',1e-100)
K_a = lsqcurvefit(func,x0,freq,backgroundImpIm(1:100))
plot(freq,backgroundImpIm(1:100))
hold on
plot(freq,func(K_a,freq))
0 Kommentare
Akzeptierte Antwort
Mathieu NOE
am 5 Jul. 2023
hello
you can get better results if you start the fit not at index 1 , but a bit above, as your first sample seems a bit off and your data lack some resolution in the lower freq range
this appears more clearly also if you plot the results with a x log spacing
also I am limited to the use of fminsearch as I don't have the optimization toolbox , but you can easily swith back to your original code
Result :
K_a = 50.0846

background = csvread('0SM.CSV',3,0,[3,0,1603,2]);
backgroundImpIm = background(:,3);
f = background(:,1);
ind = (2:100); % <= HERE , start at 2 at lowest , discard 1st sample
freq = f(ind);
x0 = [1];
h = 5E-3;
a = 5E-5;
m_e = 9.1E-31;
q = 1.6E-19;
c = 3E8;
omega = 2*pi*freq;
k_0 = omega/c;
beta = k_0;
epsilon_real = 1;
epsilon_im = 0;
x = epsilon_im./epsilon_real;
func = @(K_a,freq)(-K_a./(tan(2*pi*freq*h.*(1 + 0.19./((K_a/60 +1) - 0.81))/c)));
%options = optimoptions(@lsqcurvefit,'FunctionTolerance',1e-100)
% K_a = lsqcurvefit(func,x0,freq,backgroundImpIm(1:100))
K_a = fminsearch(@(x) norm(func(x,freq)-backgroundImpIm(ind)),x0)
semilogx(freq,backgroundImpIm(ind),'*-r',freq,func(K_a,freq),'b')
legend('raw data,','fit');
2 Kommentare
Mathieu NOE
am 5 Jul. 2023
Another way to demonstrate that your fit shoud not include the first data point

background = csvread('0SM.CSV',3,0,[3,0,1603,2]);
backgroundImpIm = background(:,3);
f = background(:,1);
ind = (2:1600); % <= HERE , start at 2 at lowest , discard 1st sample
freq = f(ind);
x0 = [1];
h = 5E-3;
a = 5E-5;
m_e = 9.1E-31;
q = 1.6E-19;
c = 3E8;
omega = 2*pi*freq;
k_0 = omega/c;
beta = k_0;
epsilon_real = 1;
epsilon_im = 0;
x = epsilon_im./epsilon_real;
func = @(K_a,freq)(-K_a./(tan(2*pi*freq*h.*(1 + 0.19./((K_a/60 +1) - 0.81))/c)));
%options = optimoptions(@lsqcurvefit,'FunctionTolerance',1e-100)
% K_a = lsqcurvefit(func,x0,freq,backgroundImpIm(1:100))
K_a = fminsearch(@(x) norm(func(x,freq)-backgroundImpIm(ind)),x0)
% semilogx(freq,backgroundImpIm(ind),'*-r',freq,func(K_a,freq),'b')
semilogx(f,backgroundImpIm,'*-r',freq,func(K_a,freq),'b')
legend('raw data,','fit');
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Nonlinear Control finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
