Why is the fit() function producing a horizontal line above my data?

16 ビュー (過去 30 日間)
Luke Fournier
Luke Fournier 2022 年 12 月 3 日
コメント済み: Walter Roberson 2022 年 12 月 4 日
I am using the MATLAB fit() function (single exponential) to fit the decay of miniature excitaotry post synaptic current (mEPSC) events. It works really nicely for some events, but for others the fitted curve is simply a flat line above my data (see represemtative image for an example of each). I cannot empirically determine any differences between the events where the fit looks good compared to those where the fit produces the horizontal line. Any ideas?
  13 件のコメント
Luke Fournier
Luke Fournier 2022 年 12 月 4 日
So I know that model works for some of the events, so this is my confusion as to why with some events which look similar in nature it fails.
Luke Fournier
Luke Fournier 2022 年 12 月 4 日
If I provided the data for event 36 (as I did for event 37), would this help? So you can see the data fitting for one event but not the other? The code I have written is what identifies which points the take to fit (i.e. the blue data points), but maybe if I provided the data of the raw trace of an event where the fit did work (36) would help? Let me know!

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2022 年 12 月 4 日
Activate the "center and scale" option -- but remember that the coefficients you are shown as a result refer to the centered/scaled coordinate system.
  3 件のコメント
Luke Fournier
Luke Fournier 2022 年 12 月 4 日
Problem completely resolved, thank you Walter! For those who may read in the future and wonder about the exact syntax that corresponds to Walter's solution, use fit(x, y, 'exp1', 'Normalize', 'on'). Activating the normalization is what centers/scales the coordinate system. Read more here about fitoptions
Thank you all again for your help!
Walter Roberson
Walter Roberson 2022 年 12 月 4 日
format long g
filename = 'for_walter.xlsx';
T = readmatrix(filename);
fx = T(:,4);
fy = T(:,5);
%data contains lots of nan
mask = ~isfinite(fx) | ~isfinite(fy);
fx(mask) = [];
fy(mask) = [];
p = polyfit(fx,log(fy),1)
p =
-0.023959908827361 - 0.0167790652408582i 847.540790585289 + 595.387462252473i
b = p(1);
a = p(2);
syms x
yfit = exp(sym(a)) * exp(b*x);
for_display = vpa((yfit),16)
for_display = 
Notice the coefficients on the order of 6e+366 -- coefficients that are beyond the range representable by double precision. So when you work in double precision, you cannot get a useful fit.
Let us try a different approach:
syms areal aimag breal bimag
a = areal + 1i*aimag;
b = breal + 1i*bimag;
%residue = sum((fy - a*exp(b*fx)).^2);
residue = norm(fy - a*exp(b*fx));
rfun = matlabFunction(residue, 'vars', {[areal, aimag, breal, bimag]});
[best, fval] = fmincon(rfun, [-.1 -.1 1e-5 1e-5], [], [], [], [], [-1e6 -1e6 -700 -700], [1e6 1e6 700 700])
Local minimum possible. Constraints satisfied. fmincon stopped because the size of the current step is less than the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
best = 1×4
1.0e+00 * -359574.445229496 -148362.537586897 -0.000324026729102402 -0.000189093661138137
fval =
45.7651903184081
a = best(1) + 1i*best(2)
a =
-359574.445229496 - 148362.537586897i
b = best(3) + 1i*best(4)
b =
-0.000324026729102402 - 0.000189093661138137i
y = a*exp(b*x)
y = 
vpa(y)
ans = 
scatter(fx, fy);
hold on
fplot(real(y), [min(fx), max(fx)])
hold off
Coefficients not quite as large, but the plot is still flat.
If you try to fit a*exp(b*x) using fmincon and using only real values, Yes, you get an answer, but the residue is large

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFit Postprocessing についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by