I am trying to minimize the function ARE in file abc.m. But I m getting the error "your objective function must return a scalar value", I know that I minimizing this function with a variable which is not constant. It works when I take any single val

1 Ansicht (letzte 30 Tage)
========================================= mytest.m file ========================================= close all; clear all; clc;
a0 = [.0001 .0001 .0001 .0001 .0001 ]
f =@(a)abc(a);
[a fval] = simulannealbnd(f,a0);
disp(a); ===============================================
second file =============================================== abc.m ===============================================
function ARE = abc(a) ;
w = linspace(0,pi,1000)
ARE = ((1/(j*w)) - ((a(1)*exp(2*j*w) + a(2)*exp(j*w) + a(3)) / (exp(2*j*w)+a(4)*exp(j*w) + a(5)))/(1/(j*w)));

Antworten (2)

Torsten
Torsten am 15 Apr. 2015
What are you trying to do ?
Find different a(1),...,a(5) that minimize ARE for each single value of w ?
Best wishes
Torsten.
  3 Kommentare
Torsten
Torsten am 23 Apr. 2015
What exactly do you mean by
"I want the function to be minimized with range of values at a time"
?
If you want to minimize f for a range of values for w, use a for-loop over the elements of the w-array and therein, call "simulannealbnd" for each element of w separately.
Best wishes
Torsten.
Nitin Rawal
Nitin Rawal am 24 Apr. 2015
Bearbeitet: Nitin Rawal am 24 Apr. 2015
I already tried what are you saying. It comes up with an error message:
??? Error using ==> samakedata at 28 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 129 [x, fval, exitflag, output] = simulanneal(FUN, x0, [], [], [], [], lb, ub, options);
Error in ==> simul at 6 [x,fval]= simulannealbnd(fh,x0);
This is because simulannealbnd's objective function can work properly with constant parameters But here I m using w =linspace(0,pi,10) stat# which evaluates ARE for each value of w but simulannealbnd's objective function can return only scalar value not a vector.
So please help me that how should I code objective function that my objective function minimized: Following is code of objective function
function ARE = abc(a) ; w = linspace(0,pi,1000) ARE =abs(((1./(j*w))-((a(1)*exp(2*j*w)+a(2)*exp(j*w)+a(3))./(exp(2*j*w)+a(4)*exp(j*w)+a(5))))./(1/(j*w)));
If it works like lsqcurvefit non linear function of matlab it is done with my work.
So please also have a look in lsqcurvefit function.
Thanks
Nitin Rawal

Melden Sie sich an, um zu kommentieren.


Torsten
Torsten am 24 Apr. 2015
a0 = [.0001 .0001 .0001 .0001 .0001 ];
w = linspace(0,pi,1000);
for i=1:length(w)
w_scal=w(i);
f =@(a)abc(a,w_scal);
[a_vec fval] = simulannealbnd(f,a0);
a_mat(:,i)=a_vec(:);
end
function ARE = abc(a,w)
ARE = ((1/(j*w)) - ((a(1)*exp(2*j*w) + a(2)*exp(j*w) + a(3)) / (exp(2*j*w)+a(4)*exp(j*w) + a(5)))/(1/(j*w)));
I wonder what j is - the imaginary unit ?
Best wishes
Torsten.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by