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

24 次查看(过去 30 天)
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by