Fix parameters using fit function
50 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Andrea Calvi
am 17 Dez. 2015
Bearbeitet: gabodabo
am 19 Mai 2019
Hi all,
This may be a dumb and easy question, but I'm having problems in understanding how to fix parametersin a multiparameter fit function. Going straight to the problem, i have a function which fits a 2D user inputted gaussian as follows:
function [gof,fittato] = fit_gaub(image,error,init)
a = init(1);
b = init(2);
c1 = init(3);
c2 = init(4);
t1 = init(5);
w1 = init(6);
w2 = init(7);
gaub = @(a,b,c1,c2,t1,w1,w2,x,y) a + b.*exp(-(((x-c1).*cosd(t1)+(y-c2).*sind(t1))/w1).^2-((-(x-c1).*sind(t1)+(y-c2).*cosd(t1))/w2).^2)./(pi.*w1.*w2);
lunghezza = numel(image);
z_vect = zeros(lunghezza,1);
k = 1;
for i=1:size(image,2)
for j=1:size(image,1)
r_fit(k) = j;
c_fit(k) = i;
z_vect(k) = image(j,i);
k = k+1;
end
end
weight(1:lunghezza)=error;
% a,b,c1,c2,t1,w1,w2
[fittato, gof] = fit([r_fit', c_fit'], z_vect,gaub,'Robust', 'Bisquare','Algorithm','Trust-Region','weights',weight...
,'StartPoint', init ...
,'Lower', [ -10 0 0 0 0 1 1 ]...
,'Upper', [ 100 10e12 200 50 15 100 100]);
In the framework of this function, how can I tell matlab to fix a parameter without playing with the contranints? I've found something about it only concerning the
lsqcurvefit
function, but I have no idea neither on how to use that function, nor what changes may that bring to my code(get same output from my function).
any help would be much appreciated,
Andrea Calvi
0 Kommentare
Akzeptierte Antwort
jgg
am 17 Dez. 2015
Bearbeitet: jgg
am 17 Dez. 2015
I think I understand what you're trying to do. Your function is this thing:
gaub = @(a,b,c1,c2,t1,w1,w2,x,y) ...%bunch of function stuff
What you'd like to do is, say, fix b to be a particular value, say b = 3 then optimize your function, but you don't want to do this by saying 3< = b <= 3 in the constraints.
The simplest solution is to just refine your function and constaints:
b = 3;
gaub2 = @(a,c1,c2,t1,w1,w2,x,y) gaub(a,b,c1,c2,t1,w1,w2,x,y);
%now gaub2 is gaub, with b fixed at the value set.
Now, you just need to get rid of the b constraint, so in your optimization, set:
'Lower', [ -10 0 0 0 1 1 ]...
'Upper', [ 100 200 50 15 100 100]
by omitting the column of constraints associated with b. You can fiddle with this to be a little more robust, but I think this is the most straightforward way. (For example, to do it for all of your variables, you can set up a switch statement and have seven possible functions).
0 Kommentare
Weitere Antworten (2)
gabodabo
am 19 Mai 2019
Bearbeitet: gabodabo
am 19 Mai 2019
I am nearly 4y late for the discussion, but a very simple way of fixing parameters in the 'fit' function is to put your value in the upper and lower limits. For example, for a fit to a function with four parameters, of which two are fixed:
opts.Lower = [ param1_fix param2_fix -Inf -Inf ];
opts.Upper = [ param1_fix param2_fix Inf Inf ];
[fitresult, gof] = fit( xData, yData, myfunc, opts);
Hope this helps!
Gabriel
0 Kommentare
Andrea Calvi
am 18 Dez. 2015
4 Kommentare
jgg
am 21 Dez. 2015
I don't think so; it's okay though. People should read through if they want to automate it, so it's all good.
Siehe auch
Kategorien
Mehr zu Get Started with Curve Fitting Toolbox finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!