Setting constraints in cftool
Mostra commenti meno recenti
I am using cftool to fit my data to a 2-peak Gaussian. I know that both my sigmas are the same (sigma1=sigma2) and that the peaks are 180 points apart (mu2-mu1 = 180). How do I set those constraints? Thank you!
1 Commento
Michael Soskind
il 23 Apr 2020
Hi Tjasa,
cftool is a nice way to introduce in a graphical user interface the power of Matlab. If you want to continue to use this, you can select to use a custom equation. After that, the equation should look something like this, based on what you described:
a.*exp(-((x-b)./c).^2) + d.*exp(-((x-(b+180))./c).^2)
However, I will divest some energy to show some of the power of programming in code. This is particularly useful when one needs to loop through large datasets, as fitting via the GUI interface is quite slow for thousands of plots:
coeff1 = [1,2,5,3,2+180,5]; % Coefficient used in defining test data points
coeff0 = [1,2,5,1,2,5]; % Initial coefficients provided
clc;
x = -200:1:200;
% The function typically used by cftool for two gaussian profiles
f = @(c,xdata) c(1).*exp(-((x-c(2))./c(3)).^2) + c(4).*exp(-((x-c(5))./c(6)).^2);
% A modified function capable of accounting for the shift in center frequency and same widths
f_assumptions = @(c,xdata) c(1).*exp(-((x-c(2))./c(3)).^2) + c(4).*exp(-((x-(c(2)+180))./c(3)).^2);
y = f(coeff1, x);
figure();
plot(x,y, 'x'); hold on; % Plotting the test points
% Defining the first curve-fit which fails because the guess parameters seem too far away
coeff_1 = lsqcurvefit(@(c, xdata) f(c,xdata), coeff0, x, y);
plot(x,f(coeff_1, x), '--')
% Defining the alternative curve fitting with the assumptions included, allowing for more
% arbitrary guesses to be included and used
coeff_2 = lsqcurvefit(@(c, xdata) f_assumptions(c,xdata), coeff0, x, y);
plot(x,f_assumptions(coeff_2, x), '--')

Hopefully this alternative method shows the power of functions like lsqcurvefit which easily allow for modification of particular functions.
Best,
Michael
Risposte (0)
Categorie
Scopri di più su Get Started with Curve Fitting Toolbox in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!