Trying to do a non linear fit under non linear constraints using lsqcurvefit: constraint needs more input

4 次查看(过去 30 天)
I am trying to do a non linear fit under non linear constraints using lsqcurvefit.
the objective function looks like this: obj=@(x,xdata) (calculations using x(1), x(2),x(3),xdata)
the non linear constraint condition has the form K=@(x,xdata,ydata,otherdata1) (calculations using x(1),x(2),x(3),xdata,ydata,otherdata1)
the nlcon (constraint argument) in lsqcurvefit takes only x as input (so to be able to pass K to nlcon, i have created a wrapper so that the K_wrapper facilitates passing the extra arguments into nlcon)
it should have the form:
function [c,ceq]=nlcon(x)
c=[]
ceq=K_wrapper
end
i ran nlcon separately and it works, which would mean that the wrapper functions work
i ran lsqcurvefit without the constraint condition and it works
The problem: as soon as i couple constraint and lsqcurvefit together, i get an error saying that
"Error using TopLevelScript>@(x,xdata) obj(x,xdata)
Too many input arguments.
Error in lsqcurvefit (line 225)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in TopLevelScript (line 45)
res=lsqcurvefit(obj,x0,xdata,ydata,lb,ub,A,b,Aeq,beq,nlcon);
Caused by:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.
"
how should i resilve this?
  3 个评论
Asheesh
Asheesh 2023-6-5
编辑:Matt J 2023-6-5
sorry about that, here is the code:
%%newTOplevelScript
load("data.mat")
%%
%%introduce the vectors after loading data
s=s_data(6:end-2); %xdata
a=a_data(6:end-2); %ydata
%%
%define FitFunctionWrapper
Fit = @(params,scattVect,absInt) (a-params(1).*s.^-4- ...
params(2).*s.^2-params(3));
FitFunctionWrapper = @(params,s) Fit(params,s, ...
a);
sigma=1.6;
%%
%%
%define nlcon
pass_on_wrapper = @(s,a,sigma,params) (calculate_sec_der ...
(s,a,sigma,params(1),params(2),params(3)));
transfer_wrapper = @(params) (pass_on_wrapper(s,a,sigma,params));
nlcon_wrapper = @(params) (transfer_wrapper(params));
%% initialize x0
initial_P=0.1;
initial_C1=100;
initial_C2=1;
x0=[initial_P,initial_C1,initial_C2];
%% other arguments for lsqcurvefit
A=[];
b=[];
Aeq=[];
beq=[];
lb=zeros(size(x0));
ub=200*(x0); %maybe adjust it a little more
%%perform lsqcurvfit
zz=nlcon_wrapper(x0);
result=lsqcurvefit(FitFunctionWrapper,x0,s,a,lb,ub,A,b, ...
Aeq,beq,nlcon_wrapper);
%%here, calculat_sec_der(s,a,sigma,params) = 0 is the non linear constraint condition
%%Fitfunction is the objective function and takes params and s as input
%% s and a are 760x1 sized doubles
%% zz gets executed properly, result=lsqcurvefit(FitFunctionWrapper,x0,s,a); also works
%% result=lsqcurvefit(FitFunctionWrapper,x0,s,a,lb,ub,A,b,Aeq,beq,nlcon_wrapper); gives the following error:
Error using newTop>@(params,s)Fit(params,s,a)
Too many input arguments.
Error in lsqcurvefit (line 225)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in newTop (line 45)
result=lsqcurvefit(FitFunctionWrapper,x0,s,a,lb,ub,A,b, ...
Caused by:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.
Torsten
Torsten 2023-6-5
编辑:Torsten 2023-6-5
Your code is incomplete.
Test it here before posting using the code formatting and the green arrow in the menue to execute it.
We don't know "data.mat" and "calculate_sec_der".

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by