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

 採用された回答

Andrei Bobrov
Andrei Bobrov 2011 年 5 月 11 日

0 投票

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

1 件のコメント

chris
chris 2012 年 6 月 22 日
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

サインインしてコメントする。

その他の回答 (3 件)

Star Strider
Star Strider 2012 年 6 月 23 日

1 投票

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 2011 年 5 月 11 日

0 投票

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 2012 年 6 月 22 日

0 投票

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

カテゴリ

ヘルプ センター および File ExchangeParallel Computing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by