How to use "ga" (genetic algorithm) for curve fitting
조회 수: 2 (최근 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?
댓글 수: 0
채택된 답변
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
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 Center 및 File Exchange에서 Genetic Algorithm에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!