Dear All,
I need to optimize parameters of a function using the ga module. I have a coefficient matrix say A(9:4):
A =
-0.0996 -0.0323 -0.0640 -0.0365
0.0859 -0.0636 0.0552 0.0315
0.1805 0.0125 0.1159 0.0662
0.0521 0.0513 0.0335 0.0191
-0.3028 0.0647 -0.1946 -0.1111
-0.2242 -0.0382 -0.1440 -0.0822
0.0475 -0.0191 0.0305 0.0174
0.2170 -0.0010 0.1394 0.0796
0.0437 0.0257 0.0281 0.0160
and a vector B where i store the experimental data say B(9:1):
B =
-4.8700
-3.2100
5.1000
5.3400
-1.6200
-8.1700
-0.4700
4.8300
3.0800
my chromosome will be a vector x(4:1)
I would like to write a fitness function like:
fitnessfcn = abs((A*x)-B) [x,fval]=ga(fitnessfcn,4)
I have used the below function
FitnessFcn = @(x) abs( ((A(1,1)*x(:,1))+(A(1,2)*x(:,2))+(A(1,3)*x(:,3))+(A(1,4)*x(:,4)))-B(1,1))
which only uses (the first row of A) * (column x)-(first element of B)
How can I write in general form to use all the data. I have tried fitnessfcn = abs((A*x)-B) but it says Error using ==> mtimes Inner matrix dimensions must agree.
Do I have to use for loops and how to write a function with for loops?
I would really welcome any sugestion.
Kind regards, Christos

 Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 11 Mai 2011

0 votes

variant
FitnessFcn = @(x)bsxfun(@minus,abs(bsxfun(@times,A,x)),B)

1 commentaire

chris
chris le 22 Juin 2012
Many thanks for your answer. It is very helpful. I am using this in the example above with:
[x]=ga(FitnessFcn,4)
and i am getting an error message:
Subscripted assignment dimension mismatch.
Is it easy to find the problem?
Once again thanks
Kind regards, christos

Connectez-vous pour commenter.

Plus de réponses (3)

Star Strider
Star Strider le 23 Juin 2012

1 vote

If the fitness function is supposed to produce a scalar to be minimized, then I suggest something like:
FitnessFcn = @(A,B,x) ( sum( (A*x-B).^2 ) );
Since A is (9x4), x is (4x1) and B is (9x1), A*x will produce a (9x1) vector. A*x-B produces the error vector, and the FitnessFcn here will produce the sum of the squared errors, a scalar >= 0.
chris
chris le 11 Mai 2011

0 votes

Many thanks for your answer. It is very helpful. I am using this in the example above with:
[x]=ga(FitnessFcn,4)
and i am getting an error message:
Subscripted assignment dimension mismatch.
Is it easy to find the problem?
Once again thanks
Kind regards, christos
chris
chris le 22 Juin 2012

0 votes

Sorry for your time. I am coming back to this old question since after postponing for one year I start again to try make it work. However I haven't solved the problem above. I would be more than happy for any help since my experience with matlab is rather limited.
All the best, chris

Catégories

En savoir plus sur Parallel Computing dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by