global optimization ga with anonymous function

Hello, I'm practicing using global optimization toolbox. I have matrix of "Observed" and "Time", and function to calculate the least sum of squared, "my_fun". Using ga produced some errors... Could you tell me what's wrong? Note, error messages were translated to English from Japanese, so it might not quite correct.
Thank you!
Observed =
6.0111
5.9374
3.4616
1.1857
1.3133
3.2228
4.8029
5.9841
4.8722
2.3978
>> Time
Time =
1
2
3
4
5
6
7
8
9
10
function z = my_fun(x,Time,Observed) y=x(:,1).*sin(Time)+x(:,2); z=sum(abs(Observed-y));
f = @(x)my_fun(x,Time,Observed) ;
options = gaoptimset('Vectorized','on'); [x fval] = ga(f,2,[],[],[],[],[],[],[],options);
error: .* The dimension of matrix has to match.
error: my_fun (line 5) y=x(:,1).*sin(Time)+x(:,2);
error: @(x)my_fun(x,Time,Observed)
error: createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11) fcn_handle = @(x) fcn(x,FcnArgs{:});
error: makeState (line 64) Score = FitnessFcn(state.Population(initScoreProvided+1:end,:));
error: gaunc (line 40) state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
error: ga (line 356) [x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Cause: Failure in initial user-supplied fitness function evaluation. GA cannot continue.

回答(1 个)

The error is clear:
y=x(:,1).*sin(Time)+x(:,2);
The sizes of the matrices x(:,1) and Time are not the same. You can use the debugger, set a break point in my_fun before line 5, and manually check the sizes of these matrices while the program is running.
Alan Weiss
MATLAB mathematical toolbox documentation

Community Treasure Hunt

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

Start Hunting!

Translated by