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

9 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Walter Roberson
Walter Roberson il 16 Apr 2021
cost_wrapper = @(x) Cost(x(1),x(2),x(3),x(4),x(5),x(6));
coef = ga(cost_wrapper, 6)
  3 Commenti
Walter Roberson
Walter Roberson il 16 Apr 2021
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 il 17 Apr 2021
Great explanation. Thank you again.

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by