How do I correctly find parameters using the fittype function without it just using my initial guesses?
8 views (last 30 days)
Show older comments
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)
0 Comments
Accepted Answer
Matt J
on 30 Jun 2025
Edited: Matt J
on 30 Jun 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
r0=sqrt(A0*pi^2*b/(k*T)) %Recover r0 from A0
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
r=sqrt(result.A*pi^2*b/(k*T)) %Recover r from A
%plot(result,f(4:end),pxx(4:end), 'fit')
Yresults = result(f);
loglog(f,pxx, ".")
hold on
loglog(f,Yresults)
hold off
More Answers (0)
See Also
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!