Error in simulated annealing --- "your objective function must return a scalar value"
1 view (last 30 days)
Show older comments
Hi All,
I want to minimize a function using simulated annealing tool of matlab. Here is the matlab file that I used -
clear, close all
fid = fopen('test.dat');
C = textscan(fid, '%s %f');
fclose(fid);
protname = C{1};
slopedata = C{2};
for i=1:length(slopedata)
fn = sprintf('inte_%s.dat',protname{i});
Evals{i} = load(fn);
end
Nwt = 22;
EE=zeros([Nwt length(slopedata)]);
pE=zeros([Nwt length(slopedata)]);
for i=1:length(slopedata)
[~, EE(:,i), pE(:,i)] = gethist(Evals{i},Nwt);
end
wts0 = -exp((1:Nwt)/10)'; % initial weighting function
options = saoptimset('simulannealbnd');
options = saoptimset(options,'Display','iter','TolFun',1e-12,'MaxFunEvals',2000);
deltaslope = @(wts) slope_functn(wts,EE,pE,slopedata);
[wts, resnorm, residual, exitflag] = simulannealbnd(deltaslope,wts0,[],[],options);
slope_pred = slopedata - residual;
[rr , pp] = corr(slope_pred,slopedata)
plot(slope_pred,slopedata,'ks'), hold on
axis equal,
title('slopes from data and model prediction'),
xlabel('slope-predicted'),
ylabel('slope-data'),
hold off
figure,
h=bar(1:Nwt,wts), set(h,'FaceColor',[.5 .5 .5],'EdgeColor','k'), hold on,
plot(1:Nwt,wts0,'.-'),
plot(1:Nwt,zeros(Nwt,1),'--k'),
xlabel('E (scaled)'),
title('Initial (blue) and final weighting functions in \int w(E) E P(E) dE')
hold off
And the error that I got is
??? Error using ==> samakedata at 30
Your objective function must return a scalar value.
Error in ==> simulannealcommon at 113
solverData = samakedata(solverData,problem,options);
Error in ==> simulanneal at 44
[x,fval,exitflag,output,solverData,problem,options] = ...
Error in ==> simulannealbnd at 122
[x, fval, exitflag, output] = simulanneal(FUN, x0, [], [], [], [], lb, ub,
options);
Error in ==> simanneal at 52
[wts, resnorm, residual, exitflag] =
simulannealbnd(deltaslope,wts0,[],[],options);
Error in ==> run at 74
evalin('caller',[script ';']);
Could anyone please suggets me what should I do?
Thanks in advance
0 Comments
Answers (1)
Walter Roberson
on 11 Apr 2011
You should show us the code for slope_functn as that is the function that will need to return a scalar value.
13 Comments
pratik gautam
on 5 Jun 2020
deltaslope = sum(deltaslope); %no need to take sqrt() for optimization purposes
See Also
Categories
Find more on Simulated Annealing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!