Fix parameters using fit function
이전 댓글 표시
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
채택된 답변
추가 답변 (2개)
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
Andrea Calvi
2015년 12월 18일
1 개 추천
댓글 수: 4
jgg
2015년 12월 18일
This is a good question. Since I do this occasionally, I figured out a way to do this relatively nicely.
test = @(x)(normcdf(x(1),x(2),x(3))); %the function (just an example)
k = 3; %number of variables in gaub
places = [2,3];
values = [1.5,2.5];
places_count = 1;
y_str = '';
for i = 1:k %k is number of variables
if ismember(i,places)
y_str = strcat(y_str,num2str(values(places_count)));
places_count = places_count + 1;
else
y_str = strcat(y_str,'y(',num2str(i),')');
end
if i < k
y_str = strcat(y_str,',');
end
end
gaub_str = strcat('@(y)([',y_str,'])');
gaub2 = str2func(gaub_str);
gaub3 = @(x)(test(gaub2(x)));
gaub3(0.21)
normcdf(0.21,1.5,2.5)
Basically, the idea is to create the anonymous function dynamically using string concatenation. The only trick is that we have to get around a restriction on workspace variables, which is why there are now 3 of these functions floating around.
Andrea Calvi
2015년 12월 21일
Andrea Calvi
2015년 12월 21일
편집: Andrea Calvi
2015년 12월 21일
jgg
2015년 12월 21일
I don't think so; it's okay though. People should read through if they want to automate it, so it's all good.
카테고리
도움말 센터 및 File Exchange에서 Fit Postprocessing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!