Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.

raw = xlsread('File', 2); % read in sheet number 2
Pc = raw(60:141, 1);
Sw = raw(60:141, 9);
plot(Sw,Pc,'ro')
F = @(x,xdata)1-x(1)*exp(-(x(2)/xdata)^(x(3)));
x0 = [3 6 0.6];
[x,resnorm,~,exitflag,output] = lsqcurvefit(F,x0,Sw,Pc)
I keep getting this error.
Error in Skelt_LS_Code (line 16)
[x,resnorm,~,exitflag,output] = lsqcurvefit(F,x0,Sw,Pc)
Caused by:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.
Any help is appreciated. Thank you

 Accepted Answer

F = @(x,xdata)1-x(1)*exp(-(x(2)./xdata).^(x(3)));

6 Comments

Thank you, now I am having a diferent proplem. The least suares always does one iteration for me. , I changed the initial guess multiple times but even if the numbers of Iterations increase a little I always get a very bad match with my data, See attached . What am I doing wrong?
get in this in command window
Test
Local minimum found.
Optimization completed because the size of the gradient is less than
the value of the optimality tolerance.
<stopping criteria details>
x =
1.5098 7.0574 9.4972
resnorm =
1.2341e+06
exitflag =
1
output =
struct with fields:
firstorderopt: 0
iterations: 1
funcCount: 8
cgiterations: 0
algorithm: 'trust-region-reflective'
stepsize: 10
message: '↵Local minimum found.↵↵Optimization completed because the size of the gradient is less than↵the value of the optimality tolerance.↵↵<stopping criteria details>↵↵Optimization completed: The first-order optimality measure, 0.000000e+00,↵is less than options.OptimalityTolerance = 1.000000e-06.↵↵'
>>
Can you post your data file for us to test with?
raw = xlsread('Dummy data canada'); % read in sheet number
Pc = raw(23:118,1);
Sw = raw(23:118,4)/100; %to convert Sw to fraction
plot(Sw,Pc,'ro')
F = @(x,xdata)1-x(1)*exp(-(x(2)./xdata).^(x(3)));
x0 = [1 60 0.7];
[x,resnorm,~,exitflag,output] = lsqcurvefit(F,x0,Sw,Pc)
hold on
plot(Sw,F(x,Pc))
hold off
Attached is the form I want to fit my data to and the data set that I am using right now, I tried to exclude data from row 1 to row 23 since Sw is always 1 there but it didnt make much difference.
Thanks again
Dear @Walter Roberson, I have data and try to fit as below. could you please solve this problem?
xdata=...
[5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180];
ydata=...
[0.04178 0.081 0.09475 0.1157 0.1326 0.1468 0.1587 0.169 0.178 0.186 0.193 0.199 0.205 0.211 0.215 0.22 0.224 0.229 0.233 0.238 0.242 0.245 0.257 0.261 0.264 0.266 0.269 0.272 0.274 0.277 0.273 0.275];
fun=@(x,xdata)a0*(1+((4.5*x(1)*x(2)*xdata)/(5.5+x(2)*xdata)));
x0=[0.05 10^(4)];
[x,resnorm,~,exitflag,output] = lsqcurvefit(fun,x0,xdata,ydata);
But th e erro occur as:
Error in sample2 (line 7)
[x,resnorm,~,exitflag,output] = lsqcurvefit(fun,x0,xdata,ydata);
Caused by:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.
ydata in previous reply was wrong. this is modification:
a0=4;
xdata=...
[5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180];
ydata=...
[0.04178 0.081 0.09475 0.1157 0.1326 0.1468 0.1587 0.169 0.178 0.186 0.193 0.199 0.205 0.211 0.215 0.22 0.224 0.229 0.233 0.238 0.242 0.245 0.248 0.251 0.254 0.257 0.258 0.261 0.264 0.266 0.269 0.272 0.274 0.277 0.273 0.275];
fun=@(x,xdata)a0*(1+((4.5*x(1)*x(2)*xdata)/(5.5+x(2)*xdata)));
x0=[0.01 0.0001];
>> plot(xdata,ydata,'o')
>> x = lsqcurvefit(fun,x0,xdata,ydata)
Error using lsqcurvefit (line 262)
Function value and YDATA sizes are not equal.

Sign in to comment.

More Answers (1)

Hi,Thaer Ismail, which is your exact fit function: "ydata=1-x1*exp(-(x2/xdata)^x3)" or "ydata=1-x1*exp((-x2/xdata)^x3)" ?
if the former, take initial start values as:
x0 = [-23124 0.3 -2.2];
c245.jpg

1 Comment

It is actually the former.
I tried the initial guesses that you graciously provided but still get a very bad fit.
Thank you.skelt bad 3.JPG

Sign in to comment.

Products

Release

R2019a

Community Treasure Hunt

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

Start Hunting!