Error while running "lsqcurvefit" function. "Failure in initial objective function evaluation. LSQCURVEFIT cannot continue."

Hi all, I keep getting this error while running the "lsqcurvefit" function.
The error states "Error in lsqcurvefit (line 225)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in untitled (line 85)
c = lsqcurvefit(fun,c0,xdata,zdata);
Caused by:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue."
Can anyone please help me?
My code should have an equation with 7 variables and the experimental data contains 4 independet variables.
This is the code
fun = @(c,x) c(1)*c(2).*x(:,1).^c(3).*exp(-(c4).*(x(:,1)-c(5)).^2).*(x(:,2)/300)^(c(6))*(x(:,3)/1)^c(7)*100
% Define the initial guess for the constants
c0 = [1, 2.094, 1.068, 0.424, 1.075, 2.9, -0.04];
% Load the experimental data from a file (example: data.mat)
mech10 = 'C:/Users/aljabrhh/Desktop/PC-H2/Niraj_SI_Optical_Engine/Pure_H2_CASES/New_Cases_23_3_2023/Laminar_flame_speed/Chemkin/Burke/export_10_bar.xlsx';
for i=1:17
data=xlsread(mech10,i);
S_L_mech102(:,i)=data(:,1);
S_L_mech101(:,i)=data(:,2);
S_L_mech10(:,i)= data(:,3);
end
mech20 = 'C:/Users/aljabrhh/Desktop/PC-H2/Niraj_SI_Optical_Engine/Pure_H2_CASES/New_Cases_23_3_2023/Laminar_flame_speed/Chemkin/Burke/export_20_bar.xlsx';
for i=1:17
data=xlsread(mech20,i);
S_L_mech202(:,i)= data(:,1);
S_L_mech201(:,i)= data(:,2);
S_L_mech20(:,i)= data(:,3);
end
mech30 = 'C:/Users/aljabrhh/Desktop/PC-H2/Niraj_SI_Optical_Engine/Pure_H2_CASES/New_Cases_23_3_2023/Laminar_flame_speed/Chemkin/Burke/export_30_bar.xlsx';
for i=1:17
data=xlsread(mech30,i);
S_L_mech302(:,i)= data(:,1);
S_L_mech301(:,i)= data(:,2);
S_L_mech30(:,i)= data(:,3);
end
mech40 = 'C:/Users/aljabrhh/Desktop/PC-H2/Niraj_SI_Optical_Engine/Pure_H2_CASES/New_Cases_23_3_2023/Laminar_flame_speed/Chemkin/Burke/export_40_bar.xlsx';
for i=1:17
data=xlsread(mech40,i);
S_L_mech402(:,i)= data(:,1);
S_L_mech401(:,i)= data(:,2);
S_L_mech40(:,i)= data(:,3);
end
mech50 = 'C:/Users/aljabrhh/Desktop/PC-H2/Niraj_SI_Optical_Engine/Pure_H2_CASES/New_Cases_23_3_2023/Laminar_flame_speed/Chemkin/Burke/export_50_bar.xlsx';
for i=1:17
data=xlsread(mech50,i);
S_L_mech502(:,i)= data(:,1);
S_L_mech501(:,i)= data(:,2);
S_L_mech50(:,i)= data(:,3);
end
% Extract the x, y,z and z1 data from each set of experimental data
xdata1 = S_L_mech102;
ydata1 = S_L_mech101;
zdata1 = S_L_mech10;
z1data1 = 10*ones(length(S_L_mech10),17);
xdata2 = S_L_mech202;
ydata2 = S_L_mech201;
zdata2 = S_L_mech20;
z1data2 = 20*ones(length(S_L_mech10),17);
xdata3 = S_L_mech301
ydata3 = S_L_mech301
zdata3 = S_L_mech30
z1data3 = 30*ones(length(S_L_mech10),17);
xdata4 = S_L_mech401
ydata4 = S_L_mech402
zdata4 = S_L_mech40
z1data4 = 40*ones(length(S_L_mech10),17);
xdata5 = S_L_mech501
ydata5 = S_L_mech502
zdata5 = S_L_mech50
z1data5 = 50*ones(length(S_L_mech10),17);
% Add more sets of experimental data as needed
% Combine the x, y, and z data into a single matrix
xdata = [xdata1, ydata1,z1data1; xdata2, ydata2,z1data2; xdata3, ydata3,z1data3; xdata4, ydata4,z1data4; xdata5, ydata5,z1data5]; % Combine the x, y, and z data from all sets
% Combine the z data from all sets
zdata = [zdata1; zdata2; zdata3; zdata4; zdata5];
% Fit the equation to the data using lsqcurvefit
c = lsqcurvefit(fun,c0,xdata,zdata);

답변 (1개)

Before calling lsqcurvefit, try to evaluate your function by
fun(c0,xdata)
and see what you get. If this evaluation is not possible without getting an error or NaN or Inf values, the same will be true when lsqcurvefit tries to evaluate your function:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.
Use .*, ./ and .^ in all places for your function definition where you multiply, divide or exponentiate. I see at least three places where this is necessary, but you use usual *, / or ^ .

댓글 수: 4

Yes, you were right.
Thanks a lot.
Now I am getting this error
"Function value and YDATA sizes are not equal."
Now I am getting this error
"Function value and YDATA sizes are not equal."
And is this the case ?
Is size(fun(c0,xdata)) not equal to size(zdata) ?
Then you have to make them equal.
They both have the some number of rows but different number of coulmns.
lsqcurvefit assumes that fun(c,xdata) is an approximation for zdata.
Thus it attempts to make fun(c,xdata) - zdata small.
But subtraction is not possible if both matrices have different sizes.
So you should reconsider where your logical error in the problem setup is.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

질문:

2023년 5월 13일

편집:

2023년 5월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by