ga-fitness
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
More Answers (3)
Star Strider
on 23 Jun 2012
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.
0 Comments
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!