Matlab - Fit a Curve with Constrained Parameters -
for (x,y) dataset, let have curve given expression in a, b,c... etc, such f='a*exp(b*x)+c', fitted cfit=fit(x,y,f).
suppose have set of constraint such b>0, c+b>a/2. how should use fit command in case?.
for constraints numeric values, such b > 0, can use 'lower' , 'upper' bounds arguments specify those. more complex relationships, c+b>a/2, you'll have take approach james suggests, setting function output high value flintmax generate large error. example, let's define function this:
function y = my_fcn(a, b, c, x) if (c+b > a/2) y = a.*exp(b.*x)+c; else y = flintmax().*ones(size(x)); end end i can create set of noisy test data follows:
a = 4; b = 2; c = 1; x = (0:0.01:2).'; y = my_fcn(a, b, c, x) + 40.*(rand(size(x))-0.5); and fit curve (note have use anonymous function, since function handle won't work reason):
params = fit(x, y, @(a, b, c, x) my_fcn(a, b, c, x), ... 'startpoint', [1 1 1], ... % starting guesses [a b c] 'lower', [-inf 0 -inf]); % set bound 'b' params = general model: params(x) = my_fcn(a,b,c,x) coefficients (with 95% confidence bounds): = 4.297 (2.985, 5.609) b = 1.958 (1.802, 2.113) c = 0.1908 (-4.061, 4.442) note fitted values close original values, don't match due noise. can visualize fit so:
plot(x, y); hold on; plot(x, my_fcn(params.a, params.b, params.c, x), 'r'); 
Comments
Post a Comment