I am attempting to fit a superguassian function to step function. With the following code. (In practice the code should be able to handle all regimes from a normal guassian to a step function, I'm just testing an edge case)
output = ones(512, 1);
output(start:stop) = min(outline); %outline is defined earlier in code, min(outline) ~ 0.92
A = 1 - min(output);
g = fittype(@(b,c,x) 1 - A*exp(- ( (x-256)/ c ).^(2*round(b))),...
'dependent',{'y'},'independent',{'x'},'coefficients',{'b','c'});
x = reshape(1:numel(output),numel(output),[])
y = reshape(output,numel(output),[])
mdl = fit(x,y,g,'lower',[1 1], 'upper', [1e3 200])
f = figure
scatter(1:numel(output),output)
xlim([0 numel(output)])
hold on
plot(mdl)
hold off
The code produces the following fit:
However, if I arbitrarily constrain c (which controls the width of the graph) to have a minimum value within the following by modifying the fitting code like such:
g = fittype(@(b,c,x) 1 - A*exp(- ( (x-256)/max([120 c]) ).^(2*round(b))),...
'dependent',{'y'},'independent',{'x'},'coefficients',{'b','c'});
Then I get the following fit:
Clearly the function is capable of producing a better fit with a slightly larger c value, yet it doesn't. Any insights as to where I'm making a mistake here? Thanks!

1 件のコメント

Torsten
Torsten 2022 年 3 月 2 日
編集済み: Torsten 2022 年 3 月 2 日
The fittype function must be differentiable with respect to the parameters for "fit" to succeed.
Since this is not the case for your function definitions, the results will be unpredictable and poor.
I suggest using "ga" instead of "fit".

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

 採用された回答

Matt J
Matt J 2022 年 3 月 3 日
編集済み: Matt J 2022 年 3 月 3 日

0 投票

Loop over the discrete variables. Use fit() with respect to the continuous ones.
for b=1:4
g = fittype(@(b,c,x) 1 - A*exp(- ( (x-256)/ c ).^(2*b),...
'dependent',{'y'},'independent',{'x'},'coefficients',{'c'});
[mdl(b),gof(b)] = fit(x(:),y(:),g,'lower',1, 'upper', 200)
end
Then take b as that which gives the best gof(b).

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Get Started with Curve Fitting Toolbox についてさらに検索

製品

リリース

R2021a

タグ

質問済み:

2022 年 3 月 2 日

編集済み:

2022 年 3 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by