Error messages during using Genetic algorithm and lsqnonlin optimization functions
조회 수: 1 (최근 30일)
이전 댓글 표시
Currently,I am running optimization script file by using the outputs from the genetic algorithm function in the least square nonlinear function lsqnonlin() (as initial values for lsqnonlin()).When I run the script ,the following errors appear.So,what's the problem ?
Optimization terminated: maximum number of generations exceeded.
Error using snls (line 48)
Objective function is returning undefined values at initial
point. lsqnonlin cannot continue.
Error in lsqncommon (line 150)
[xC,FVAL,LAMBDA,JACOB,EXITFLAG,OUTPUT,msgData]=...
Error in lsqnonlin (line 237)
[xCurrent,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...
Error in frf (line 27)
[c,resnorm,residual,exitflag1,output1]=lsqnonlin(ff,c0);
Here is the code:
clc
clear all
% Create an anonymous function that describes the expected relationship
% between X and Y
f=@(c,x) exp((c(1).*(x(:,1).^c(2)))+(x(:,3)*c(4))+(x(:,2)*c(3)))+c(5).*(((x(:,2))./(x(:,3))-0.5).^2)+c(6).*(x(:,2))+c(7);
% data set
% Specify x variables from data file,Re,Theta and Beta columns.
x=xlsread('all data for fitting friction');
% Specify y variable from data file ,(f)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 = 400;
Parms = 7;
% Modifying the options of genetic algotithm optimization function
opts = gaoptimset('PopulationSize',PopSz, 'InitialPopulation',randi(1E+4,PopSz,Parms)*1E-4, 'Generations',2e3,'StallGenLimit',inf, 'PlotFcn',@gaplotbestf, 'PlotInterval',1);
% Genetic algorithm application
[theta,fval,exitflag,output] = ga(ffmax, Parms, [],[],[],[],-Inf(Parms,1),Inf(Parms,1),[],[],opts);
% Specify a vector of starting conditions for the solvers(Least squre
% nnonlinear fit)
c0=[theta(1) ;theta(2);theta(3);theta(4);theta(5);theta(6);theta(7)];
% Perform a nonlinear regression
[c,resnorm,residual,exitflag1,output1]=lsqnonlin(ff,c0);
y1=f(c,x);
diff_f=y-y1;
Abs_diff_f=abs(diff_f);
perc_error=(Abs_diff_f./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')
header=['fact','ffit'];
N=[y,y1];
disp(header)
disp(N)
disp('max. error=')
disp(max_error)
댓글 수: 3
Star Strider
2020년 8월 21일
Note that the documentation for R2013a is no longer available in Archived MathWorks Documentation, that being the reason I could not troubleshoot the reported problem.
채택된 답변
Matt J
2020년 8월 21일
편집: Matt J
2020년 8월 21일
You have quite a few NaNs in your y data
>> nnz(isnan(y))
ans =
7
>> nnz(isnan(ff(theta)))
ans =
7
댓글 수: 5
Matt J
2020년 8월 21일
편집: Matt J
2020년 8월 21일
Make sure you are using the same .xlsx file as the one you attached for us. The one you attached has 15 columns with lots of missing elements and different numbers of rows. xlsread is adding NaNs to pad the missing elements in the shorter columns.
>> x=xlsread('all data for fitting friction.xlsx');
>> whos x
Name Size Bytes Class Attributes
x 119x15 14280 double
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/349676/image.png)
추가 답변 (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!