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
Star Strider 2020-8-21
I had no problems with what I believe are the same data in Nonlinear least-squares data fit.
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
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
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

请先登录,再进行评论。

更多回答(0 个)

类别

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

产品


版本

R2013a

Community Treasure Hunt

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

Start Hunting!

Translated by