Error in Genetic Algorithm ??

I want to solve linear system of equations Ax=b, with ... A is 27*27 matrix, b is 27*1 vector, and x is 27*1..
A_struuct=load('A.mat');
A=A_struuct.A;
obs_struct=load('obs.mat');
obsb=obs_struct.obs;
x0=ones(27,1);
Fun=@(x) norm(A*x-obs,2);
nvars=27;
best_x=ga(Fun,nvars);
Error is
Error using *
Inner matrix dimensions must agree.
Error in @(x)norm(A*x-obs,2)
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 48)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in gaunc (line 41)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 348)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in last_kubik (line 182)
best_x=ga(Fun,nvars);
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.

2 commentaires

Jan
Jan le 20 Fév 2018
Modifié(e) : Jan le 20 Fév 2018
I've formatted your code using the "{} Code" button. It is easy and makes your message readable.
What is "obs"? You have defined "obsb" only.
M.Shaarawy
M.Shaarawy le 20 Fév 2018
sorry its obs not obsb
what do you mean with "{}Code" button

Connectez-vous pour commenter.

Réponses (1)

Jan
Jan le 20 Fév 2018
Modifié(e) : Jan le 20 Fév 2018

0 votes

As soon as you create a standard function instead of an anonymous one, you can use the debugger to find out the details:
function y = Fun(x, obs)
y = norm(A * x - obs, 2);
end
And to call the optimization tool (anonymous function only to provide the parameter obs):
F = @(x) Fun(x, obs); % Or "obsb"?
best_x = ga(F, nvars);
Now enable the debugger:
dbstop if error
If Matlab stops at the error, check the dimensions of A, x and abs

3 commentaires

M.Shaarawy
M.Shaarawy le 20 Fév 2018
The result is ...
Error using @(x)norm(A*x-obs,2) Too many input arguments.
Error in @(x)Fun(x,obs)
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11) fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 48) firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in gaunc (line 41) state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 348) [x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in ga_new (line 2) best_x = ga(F, nvars);
Caused by: Failure in initial user-supplied fitness function evaluation. GA cannot continue.
M.Shaarawy
M.Shaarawy le 20 Fév 2018
This it is error going to makeState.m
M.Shaarawy
M.Shaarawy le 20 Fév 2018
I want to find x which is 27*1 vector , and now I must define its size..??

Connectez-vous pour commenter.

Question posée :

le 20 Fév 2018

Commenté :

le 20 Fév 2018

Community Treasure Hunt

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

Start Hunting!

Translated by