How to use "ga" (genetic algorithm) for curve fitting

2 ビュー (過去 30 日間)
Majid Shabani
Majid Shabani 2021 年 4 月 16 日
コメント済み: Majid Shabani 2021 年 4 月 17 日
Hi every body.
I have two available vectors and . I want to fit the function on Γ which , , , , , and should be identifed to fit the function. So, I have Γ and x already existing in my workspace. I used the genetic algorithm as bellow to find , , , , , and .
Fun=@(p1,w1,x,phi1,p2,w2,phi2)(p1*sin(w1*x+phi1)+p2*cos(w2*x+phi2));
Cost=@(p1,w1,phi1,p2,w2,phi2)norm(Gamma-Fun(p1,w1,x,phi1,p2,w2,phi2));
coef=ga(Cost,6)
but things go wrong when I run the code:
Not enough input arguments.
Error in @(p1,w1,phi1,p2,w2,phi2)norm(Gamma-Fun(p1,w1,x,phi1,p2,w2,phi2))
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 52)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in gaunc (line 45)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 399)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
Can anybody help me how I may solve this problem with genetic algorithm?

採用された回答

Walter Roberson
Walter Roberson 2021 年 4 月 16 日
cost_wrapper = @(x) Cost(x(1),x(2),x(3),x(4),x(5),x(6));
coef = ga(cost_wrapper, 6)
  3 件のコメント
Walter Roberson
Walter Roberson 2021 年 4 月 16 日
cost_wrapper accepts a vector of 6 values and extracts each of the 6 values and passes them one-by-one to Cost.
All of the Mathworks optimizers require functions that accept vectors of values, not individual parameters.
Majid Shabani
Majid Shabani 2021 年 4 月 17 日
Great explanation. Thank you again.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGenetic Algorithm についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by