k = 1:10;
fitnessfcn =@(x)( 2+2*k-exp(k*x(1))-exp(k*x(2)) );
x = ga(fitnessfcn, 2) % nvars=2; invoke an optimization routine
I am getting following errors;
Subscripted assignment dimension mismatch. Caused by: Failure in user-supplied fitness function evaluation. GA cannot continue.
Also how to add a Starting guess; x0 = [0.3 0.4]
thanking for kind help!

 Respuesta aceptada

Walter Roberson
Walter Roberson el 20 de Jun. de 2017

0 votos

Your k is a vector, so ( 2+2*k-exp(k*x(1))-exp(k*x(2)) ) is a vector. However, your fitness function is required to return a scalar; https://www.mathworks.com/help/gads/ga.html#inputarg_fitnessfcn
There is no way to add a starting guess for ga. However, you can use an options structure that has InitialPopulationMatrix set in it. Scroll down a bit from https://www.mathworks.com/help/gads/ga.html#bs08mt8-4 to find the options description.

4 comentarios

ARUN BORGOHAIN
ARUN BORGOHAIN el 20 de Jun. de 2017
Editada: ARUN BORGOHAIN el 20 de Jun. de 2017
for k = 1:10;
fun =@(x) sum( 2 + 2*k-exp(k*x(1))-exp(k*x(2)) );
end
lb=0.3; ub=0.4; % lb=zeros(2,1); ub=ones(2,1);
[x, resnorm] = ga(fun,2,[],[],[],[],lb,ub,[])
same as
k = 1:10;
fun =@(x) ( 2 + 2*k-exp(k*x(1))-exp(k*x(2)));
x0 = [0.3 0.4] % Starting guess
[x,resnorm] = lsqnonlin ( fun, x0 ) % Invoke optimizer
but getting different answer as below: using nonlinear least-squares routine=>lsqnonlin x = 0.2578; 0.2578; resnorm = 124.3622;
using ga x=0.3964; 28.8409; resnorm = -4.2096e+120; result fluctuating in ga Optimization terminated: maximum number of generations exceeded. (is it due to lb=0.3; ub=0.4; pl. help, how to correct it;
Walter Roberson
Walter Roberson el 20 de Jun. de 2017
Editada: Walter Roberson el 21 de Jun. de 2017
When you have a "for" loop that assigns to the same unindexed variable each time, and there are no randomization calls or other "side effects" being made, then the effect of the loop is the same as if you had only done the last iteration.
for k = 1:10;
fun =@(x) sum( 2 + 2*k-exp(k*x(1))-exp(k*x(2)) );
end
is the same as
k = 10;
fun =@(x) sum( 2 + 2*k-exp(k*x(1))-exp(k*x(2)) );
In turn, since k is going to be a scalar for that, and none of the sub-expressions create vectors, the result would be the same as
k = 10;
fun =@(x) 2 + 2*k-exp(k*x(1))-exp(k*x(2));
Also lsqnonlin minimizes the sum of squares of the values, whereas what you attempted to submit to ga would minimize the sum of the values, which is very different. The ga equivalent would be
k = 1 : 10;
fun =@(x) sum( (2 + 2*k-exp(k*x(1))-exp(k*x(2))).^2 );
ARUN BORGOHAIN
ARUN BORGOHAIN el 21 de Jun. de 2017
Thanks for clear concept & detail explanation; I've to clear theory part of these functions first!

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

el 20 de Jun. de 2017

Editada:

el 21 de Jun. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by