Error messages during using Genetic algorithm and lsqnonlin optimization functions

2 ビュー (過去 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
MOHAMED ABDULAZIM
MOHAMED ABDULAZIM 2020 年 8 月 21 日
Many thanks to you.I really appreciate your valuable notice that solved my problem.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGenetic Algorithm についてさらに検索

製品


リリース

R2013a

Community Treasure Hunt

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

Start Hunting!

Translated by