How do I correctly find parameters using the fittype function without it just using my initial guesses?

8 vues (au cours des 30 derniers jours)
I am brand new to MatLab, and I am trying to fit a Power Spectral Density of the brownian motion in an optical trap with a Lorenzian function. I need to find the variables f0 and r. Everytime I guess a value, the function just spits out the guess I gave it. The guesses I have here for r and f0 (300,000 and 200) are around what the actual values should be. Does anyone have suggestions on how to fix my code so the fit it produces is finding the best values of r and f0 without just using the values I fed it?
data = readtable("PSmeas2 6.26.25.csv")
fs = 32000
N = height(data)
t = data.Time_s_
xdiff = data.XDIFF_V_
ydiff = data.YDIFF_V_
[pxx,f] = periodogram(xdiff,hann(height(data)),height(data),fs)
k = 1.380649 * 10^(-23);
T = 297.95;
b = 3 * pi * 0.91*10^-3 * 2*10^-6;
ft = fittype(@(r,f0,f) r^2 * k * T ./ (pi^2 * b * (f0^2 + f.^2)),...
'independent', {'f'}, 'dependent', {'pxx'});
result = fit(f(4:end),pxx(4:end),ft,'StartPoint', [300000,200])
%plot(result,f(4:end),pxx(4:end), 'fit')
Yresults = result(f);
loglog(f,pxx, ".")
hold on
loglog(f,Yresults)

Réponse acceptée

Matt J
Matt J le 30 Juin 2025
Modifié(e) : Matt J le 30 Juin 2025
This uses fminspleas, downloadable from,
It requires an initial guess only for f0. The fit appears to be sensitive to the initial guess for r, whose final value is about 50% higher than your initial guess.
load data
flist={@(f0,f) 1./(f0^2 + f.^2)};
[f0,A0]=fminspleas(flist,200, f(4:end),pxx(4:end)) %first pass solve using fminspleas
f0 = 204.4778
A0 = 0.0055
r0=sqrt(A0*pi^2*b/(k*T)) %Recover r0 from A0
r0 = 4.7509e+05
ft = fittype(@(A,f0,f) A./ (f0^2 + f.^2),...
'independent', {'f'}, 'dependent', {'pxx'});
result = fit(f(4:end),pxx(4:end),ft,'StartPoint', [A0,f0]) %now do formal fit
result =
General model: result(f) = A./(f0^2+f.^2) Coefficients (with 95% confidence bounds): A = 0.005485 (0.005311, 0.005658) f0 = 204.5 (200.4, 208.6)
r=sqrt(result.A*pi^2*b/(k*T)) %Recover r from A
r = 4.7509e+05
%plot(result,f(4:end),pxx(4:end), 'fit')
Yresults = result(f);
loglog(f,pxx, ".")
hold on
loglog(f,Yresults)
hold off

Plus de réponses (0)

Catégories

En savoir plus sur Linear and Nonlinear Regression dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by