Nonlinear least-squares data fit
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
I am trying to make a data fit for the data attached to this post,Nu=f(Re,Theta,Beta).I use lsqnonlin(fun,x0) function for this purpose.I have created a script file for this fitting,but everytime I try to run the script,the program always shows error messages.So,what is the problem with this script.
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)).*(x(:,2).^c(3)).*(x(:,3).^c(4))./x(:,4)-1;
% 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);
% Specify a vector of starting conditions for the solvers
c0=[1;1;1;1];
% Perform a nonlinear regression
c=lsqnonlin(f,c0);
채택된 답변
Star Strider
2020년 8월 14일
The objective function needs to be coded as:
ffcn = @(c) f(c,x) - y;
with the complete lsqnonlin call being:
f=@(c,x) c(1).*(x(:,1).^c(2)).*(x(:,2).^c(3)).*(x(:,3).^c(4))./x(:,4)-1;
ffcn = @(c) f(c,x) - y;
c0=[1;1;1;1];
C = lsqnonlin(ffcn, c0);
producing:
C =
1.0308e-01
1.3246e+00
1.9801e-06
-4.6017e-01
.
댓글 수: 13
MOHAMED ABDULAZIM
2020년 8월 14일
I have already made this step in defining the function,I aimed to form the function in the relative error form ((Fit -actual) /actual).
That was not part of the Question you asked.
I do not understand what you want to do.
I used the function you provided. How does that differ from what you want?
This version of the objecctive function runs:
ffcn = @(c) (f(c,x) - y)./y;
and produces approximately the same parameter values.
MOHAMED ABDULAZIM
2020년 8월 14일
you are correct about the objective function and about what you have said,but I mean that the function that you wrote (ffcn = @(c) (f(c,x) - y)./y;),I have already created it in the code(f=@(c,x) c(1).*(x(:,1).^c(2)).*(x(:,2).^c(3)).*(x(:,3).^c(4))./x(:,4)-1) ,the form is different as I simplified the fraction.What is the error that make the code stop from running?
I tried the code with the modifications that you have suggested but there is a message after running the script.
Local minimum possible.
lsqnonlin stopped because the size of the current step is less than
the default value of the step size tolerance.
<stopping criteria details>
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)).*(x(:,2).^c(3)).*(x(:,3).^c(4));
% 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;
% Specify a vector of starting conditions for the solvers
c0=[1;1;1;1];
% Perform a nonlinear regression
c=lsqnonlin(ff,c0);
Here is the code with the modifications
That is perfectly normal. The lsqnonliln message is not an error, simply letting you know that while it cocnverged, it may not have converged on the global minimum.
I ran this with the genetic algorithm function seveeral times, and the best parameter set it found was:
4.8773e+000 479.3000e-003 16.3000e-003 -555.5538e-003
with a fitness of 5.5151. That is likely as close as it is possible to get to the global minimum.
The lsqnonlin function improved on those slightly to return:
4.3202e+000 496.4773e-003 21.7014e-006 -543.8021e-003
Experiment with those values for the parameters.
.
MOHAMED ABDULAZIM
2020년 8월 14일
How to use the genetic algorithm function in this fitting?
That is a bit difficult to describe.
I used this:
D = xlsread('all data for fitting.xlsx');
x = D;
y = x(:,4);
f=@(c,x) c(1).*(x(:,1).^c(2)).*(x(:,2).^c(3)).*(x(:,3).^c(4));
ffcn = @(c) (f(c,x) - y)./y;
ftns = @(c) norm(ffcn(c));
PopSz = 500;
Parms = 4;
opts = optimoptions('ga', 'PopulationSize',PopSz, 'InitialPopulationMatrix',randi(1E+4,PopSz,Parms)*1E-4, 'MaxGenerations',2E3, 'PlotFcn',@gaplotbestf, 'PlotInterval',1);
t0 = clock;
fprintf('\nStart Time: %4d-%02d-%02d %02d:%02d:%07.4f\n', t0)
[theta,fval,exitflag,output] = ga(ftns, Parms, [],[],[],[],-Inf(Parms,1),Inf(Parms,1),[],[],opts)
t1 = clock;
fprintf('\nStop Time: %4d-%02d-%02d %02d:%02d:%07.4f\n', t1)
GA_Time = etime(t1,t0)
fprintf('\nElapsed Time: %23.15E s', GA_Time)
fprintf(1,'\tRate Constants:\n')
for k1 = 1:length(theta)
fprintf(1, '\t\tTheta(%d) = %12.5E\n', k1, theta(k1))
end
The ga function requires the Global Optimization Toolbox. This code should run in the R2013a version, however I cannot test that, so I cannot be certain.
MOHAMED ABDULAZIM
2020년 8월 16일
Did you use the genetic algorithm and lsqnonlin function separately for comparison purpose?
Star Strider
2020년 8월 16일
I used the genetic algorithm ga function first, then used its returned parameter vector as the initial parameter estimates for lsqnonlin.
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.
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);
Star Strider
2020년 8월 18일
I have no idea what is throwing that error. The code appears to be correct, however my code runs correctly on R2020a, so be certain the code is correct for R2013a. There have been many changes in the last 7 years, and I no longer have access to the documentation for R2013a.
MOHAMED ABDULAZIM
2020년 8월 19일
Did you try to run this script on your version?
I used this:
D = xlsread('all data for fitting.xlsx');
x = D;
y = x(:,4);
f=@(c,x) c(1).*(x(:,1).^c(2)).*(x(:,2).^c(3)).*(x(:,3).^c(4));
ffcn = @(c) (f(c,x) - y)./y;
ftns = @(c) norm(ffcn(c));
PopSz = 500;
Parms = 4;
opts = optimoptions('ga', 'PopulationSize',PopSz, 'InitialPopulationMatrix',randi(1E+4,PopSz,Parms)*1E-4, 'MaxGenerations',2E3, 'PlotFcn',@gaplotbestf, 'PlotInterval',1);
t0 = clock;
fprintf('\nStart Time: %4d-%02d-%02d %02d:%02d:%07.4f\n', t0)
[theta,fval,exitflag,output] = ga(ftns, Parms, [],[],[],[],-Inf(Parms,1),Inf(Parms,1),[],[],opts)
t1 = clock;
fprintf('\nStop Time: %4d-%02d-%02d %02d:%02d:%07.4f\n', t1)
GA_Time = etime(t1,t0)
QQQ1 = datetime([zeros(1,5) GA_Time], 'Format','HH:mm:ss.SSS')
fprintf('\nElapsed Time: %23.15E s ', GA_Time)
fprintf(1,'\tRate Constants:\n')
for k1 = 1:length(theta)
fprintf(1, '\t\tTheta(%d) = %12.5E\n', k1, theta(k1))
end
and when I ran that just now, got these parameter estimates:
theta =
2.8517e+000 431.7000e-003 99.6000e-003 -324.6437e-003
with a fitness value of:
fval =
5.5386e+000
.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Model Building and Assessment에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
