Is 'bayesopt' such a poor optimizer or am I doing something wrong?
이전 댓글 표시
I am trying to fit a gauss data points with exactly the same model that produced the data. The model has just 4 integer parameters. Yet 'bayesopt' is not able to find them even if I choose InitialX very close to the parameters that generated data.
I am lost. Thank you. :-)
clc
N=300;
% - let's create 300 points with gaussian shape
x = linspace(0,10,N)';
b = [1;3;2;2]; % model parameters
mf=@modelfun
y = mf(b,x);
% - let's create search parameters
num(1) = optimizableVariable('base', [0,9],'Type','integer');
num(2) = optimizableVariable('height', [0,9],'Type','integer');
num(3) = optimizableVariable('location',[0,9],'Type','integer');
num(4) = optimizableVariable('width', [0,9],'Type','integer');
% - define loss function
bf=@(num)bayesfun(num,x,y);
% - call bayesopt with initial parameters close to model values
results = bayesopt(bf,num,...
'InitialX',table(2,2,1,3));
b0=table2array(results.bestPoint);
results.bestPoint
% let's see plot of model data and fit data
%------------------------
plot(x,y,'.b')
hold on
plot(x,mf(b0,x),'.r')
hold off
legend('y','fit')
function out=bayesfun(t,x,y)
y1=modelfun(table2array(t),x);
out=sum((y1-y).^2);
end
function out=modelfun(b,x)
out = b(1);
out= out + b(2)*exp(-((b(3)-x)/b(4)).^2);
end
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!