Err on using function_handle @ in lsqcurvefit

2 Ansichten (letzte 30 Tage)
Ahmed
Ahmed am 21 Jan. 2015
Beantwortet: Star Strider am 21 Jan. 2015
I have this function_handle@
F1 = @(x,xdata)(xdata-a*(((1-exp(-xdata./(1+a)))*exp(-x(1)*xdata))+(exp(-1./xdata)*(1-exp(-x(1)*xdata))))-b*(1-(((1-exp(-xdata./(1+a)))*exp(-x(1)*xdata))+exp(-1./xdata)*(1-exp(-x(1)*xdata)))));%x(1)=S&xdata=k+
I want to fit this function with my data by using lsqcurvefit. But I got this error several time even if I write it by using sym
Error using feval Undefined function 'F1' for input arguments of type 'double'.
Error in lsqcurvefit (line 199) initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in ptt3 (line 27) [s,resnorm,~,exitflag,output]=lsqcurvefit(F11,x0,k_plus{i},lam{i});
Caused by: Failure in initial user-supplied objective function evaluation. LSQCURVEFIT cannot continue.
my code is that
clc;
clear all;
close all;
%%table read
tablecount=600;
F=cell(tablecount, 1);
dif=cell(tablecount, 1);
xdata=sym('xdata');
x(1)=sym('x(1)');
for i=1:600
f1= sprintf('table%02d.txt',i);
[data,varname,casenames]=tblread(f1);
k_plus{i}=data(:,1);
K_minus_T=data(:,2);
K_minus_D=data(:,3);
lam{i}=data(:,4);
a=K_minus_T(1,1);
b=K_minus_D(1,1);
m=length(k_plus);
%F1 = @(x,xdata)(xdata-a*(((1-exp(-xdata./(1+a)))*exp(-x(1)*xdata))+(exp(-1./xdata)*(1-exp(-x(1)*xdata))))-b*(1-(((1-exp(-xdata./(1+a)))*exp(-x(1)*xdata))+exp(-1./xdata)*(1-exp(-x(1)*xdata)))));%x(1)=S&xdata=k+
F1 = xdata-a*(((1-exp(-xdata./(1+a)))*exp(-x(1)*xdata))+(exp(-1./xdata)*(1-exp(-x(1)*xdata))))-b*(1-(((1-exp(-xdata./(1+a)))*exp(-x(1)*xdata))+exp(-1./xdata)*(1-exp(-x(1)*xdata))));%x(1)=S&xdata=k+
F11=str2func('F1');
x0 =[1];
[s,resnorm,~,exitflag,output]=lsqcurvefit(F11,x0,k_plus{i},lam{i});
S1(i,:)=s;
F{i}=F11(S1(i,:),k_plus{i});
dif{i}=(lam{i}-F{i})./lam{i};
end
F111=vertcat(F{:});
dif11=vertcat(dif{:});
dif1=dif11(:,1);
r=1;
for j=1:25:15000
S(j:j+24,1)=S1(r,1);
r=r+1;
if r>600 break
end
end
tables = cell(tablecount, 1);%read tables one by one into cell array
casenames=cell(tablecount, 1);
for tableidx = 1:tablecount
tablefile = sprintf('table%02d.txt', tableidx);
[tables{tableidx},varname,casenames{tableidx}]= tblread(tablefile); %read table
end
alltables = vertcat(tables{:}); %concatenate all tables
values=horzcat(alltables,S,F111,dif1);
rownames=char(casenames{:});
colnames={'k_plus';'K_minus_T';'K_minus_D';'lambda_L';'<N>';'<T>';'PT_lambda';'PT_<T>';'S';'PT1';'delta_PT1'};
tblwrite(values,colnames,rownames,'ptt2.txt');
Any help would be greatly appreciated. I will be grateful to you.

Antworten (1)

Star Strider
Star Strider am 21 Jan. 2015
When in doubt, vectorise everything !
Also, before you use a function in a nonlinear fit, be sure that it returns the sort of data you want it to. It is a good idea to test it first.
See if this works:
F1 = @(x,xdata)(xdata-a*(((1-exp(-xdata./(1+a))).*exp(-x(1)*xdata))+(exp(-1./xdata).*(1-exp(-x(1)*xdata))))-b*(1-(((1-exp(-xdata./(1+a))).*exp(-x(1).*xdata))+exp(-1./xdata).*(1-exp(-x(1).*xdata)))));

Community Treasure Hunt

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

Start Hunting!

Translated by