
Using results from genetic algorithm as initial values in least square non-linear fitting
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have tried to take the initial values from genetic algorithm and use them in the least square nonlinear fit but every time I try to run the script,an error message appears.I have attached the data file.There are 4 variables Nu=f(Re,Theta,Beta).
clc
clear all
% Create an anonymous function that describes the expected relationship
% between X and Y
f=@(c,x) c(1).*(x(:,1).^c(2)).*exp((x(:,3)*c(4))+(x(:,2)*c(3)));
% data set
% Specify x variables from data file,Re,Theta and Beta columns.
x=xlsread('all data for fitting');
% Specify y variable from data file ,(Nu)column.
y=x(:,4);
% objective function
ff=@(c)(f(c,x)-y)./y;
% maximum of objective function
ffmax=@(c)norm(ff(c));
% Identifying population size and number of parameters for genetic
% algorithm
PopSz = 500;
Parms = 4;
% Modifying the options of genetic algotithm optimization function
options = optimoptions('ga', 'PopulationSize',PopSz, 'InitialPopulationMatrix',randi(1E+4,PopSz,Parms)*1E-4, 'MaxGenerations',2E3, 'PlotFcn',@gaplotbestf, 'PlotInterval',1);
% Genetic algorithm application
[theta,fval,exitflag,output] = ga(ffmax, Parms, [],[],[],[],-Inf(Parms,1),Inf(Parms,1),[],[],options);
% Specify a vector of starting conditions for the solvers(Least squre
% nnonlinear fit)
c0=[theta(1) ;theta(2);theta(3); theta(4)];
% Perform a nonlinear regression
c=lsqnonlin(ff,c0);
y1=f(c,x);
diff_Nu=y-y1;
Abs_diff_Nu=abs(diff_Nu);
perc_error=(Abs_diff_Nu./y);
x_line45=0:max(y);y_line45=0:max(y);
[max_error,I_max]=max(perc_error);
[min_error,I_min]=min(perc_error);
x_line_high=0:max(y); y_line_high=(max_error+1)*(0:max(y));
x_line_low=0:max(y); y_line_low=(1-max_error)*(0:max(y));
figure
plot(x_line45,y_line45,'k')
hold on
scatter(y,y1)
hold on
plot(x_line_high,y_line_high,'r')
hold on
plot(x_line_low,y_line_low,'r')
N=[y,y1];
header=['Nuact','Nufit'];
disp(header)
disp(N)
Here is the error:
Error using optimoptions (line 114)
Invalid solver specified. Provide a solver name or handle
(such as 'fmincon' or @fminunc).
Type DOC OPTIMOPTIONS for a list of solvers.
Error in bestfitever (line 20)
options = optimoptions('ga', 'PopulationSize',PopSz,
'InitialPopulationMatrix',randi(1E+4,PopSz,Parms)*1E-4,
'MaxGenerations',2E3, 'PlotFcn',@gaplotbestf,
'PlotInterval',1);
0 Kommentare
Antworten (1)
Dev
am 29 Aug. 2025 um 10:38
The error mentioned in the question arises comes from the following line of code-
options = optimoptions('ga', 'PopulationSize', ...
PopSz, 'InitialPopulationMatrix', ...
randi(1E+4,PopSz,Parms)*1E-4, ...
'MaxGenerations',2E3, ...
'PlotFcn',@gaplotbestf, 'PlotInterval',1);
The main issue is that "ga" belongs to the "Global Optimization Toolbox", but "optimoptions" only supports solvers from the "Optimization Toolbox" (like "fmincon", "lsqnonlin", etc.). In case we aim to use "ga", we need to use "gaoptimset" function instead of "optimoptions". I have attached a reference code snippet for the same below-
options = gaoptimset('PopulationSize', PopSz, ...
'InitialPopulation', randi(1E+4,PopSz,Parms)*1E-4, ...
'Generations', 2E3, 'PlotFcns', ...
@gaplotbestf, 'PlotInterval', 1);
We can then call the "ga" function and the script executes successfully, resulting in the following graph-

For more information regarding the usage of toolboxes and function mentioned above, please refer to the links below-
"Optimization Toolbox"- https://www.mathworks.com/products/optimization.html
"optimoptions"- https://www.mathworks.com/help/optim/ug/optim.problemdef.optimizationproblem.optimoptions.html
"gaoptimset"- https://www.mathworks.com/help/gads/gaoptimset.html
I hope the above explanation helps to solve your question.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Linear Programming and Mixed-Integer Linear Programming finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!