# Question about the tolerance of lsqcurvefit

5 ビュー (過去 30 日間)
Johan Sebastian Diaz Tovar 2019 年 11 月 13 日
Commented: Johan Sebastian Diaz Tovar 2019 年 11 月 15 日
Hello,
I'm using a code that i found at MATLAB Answers, but at this time i tried to modify to solve my problem, which is this set of differential equations:
In order to solve this set of differential equations, I have experimental data of [So] and T, and i used this code:
function test01
function C=kinetics(theta,t)
c0=[1;24];
[T,Cv]=ode45(@DifEq,t,c0);
%
function dC=DifEq(t,c)
dcdt=zeros(2,1);
dcdt(1)= -theta(1).*c(1).*c(2) - theta(1).*theta(2).*(c(1).^2).*c(2);
dcdt(2)= theta(3).*c(1);
dC=dcdt;
end
C=Cv;
end
t=[0
65.78
131.56
197.34
263.12
328.9
394.68
460.46
526.24
592.02
657.8];
% 808 cluster data dH2O
c=[0.96437 24
0.81894 26
0.71044 26.9
0.63977 27.4
0.53643 27.5
0.46207 28.1
0.39372 28.3
0.33552 28.8
0.2854 29.1
0.24209 29.3
0.213 29.6];
err=[0.04622
0.03544
0.02994
0.03702
0.01933
0.02637
0.03497
0.0163
0.02451
0.0064
0.01229];
theta0=[1,1,1];
[theta,RSq,Rsd,ExFlg,OptmInfo,Lmda,Jmat]=lsqcurvefit(@kinetics,theta0,t,c);
fprintf(1,'\tRate Constants:\n')
for k1 = 1:length(theta)
fprintf(1, '\t\tTheta(%d) = %8.5f\n', k1, theta(k1))
end
tv = linspace(min(t), max(t));
Cfit = kinetics(theta, tv);
figure(1)
errorbar(t, c(1:11), err(1:11), '*')
hold on
plot(t,c(12:22),'*')
hold on
hlp = plot(tv, Cfit);
hold off
grid
xlabel('Time','FontSize', 20)
ylabel('Concentration','FontSize', 20)
legend(hlp, 'C_1(t)', 'C_2(t)', 'Location','N')
end
However, I'm getting this error:
Warning: Failure at t=5.977269e-02. Unable to meet integration tolerances without reducing the step size below the smallest value allowed (1.110223e-16) at time t.
> In ode45 (line 308)
In test01/kinetics (line 6)
In lsqcurvefit/objective (line 261)
In snls (line 329)
In lsqncommon (line 155)
In lsqcurvefit (line 253)
In test01 (line 55)
Error using - Matrix dimensions must agree.
Error in lsqcurvefit/objective (line 262)
F = F - YDATA;
Error in snls (line 329)
newfvec = feval(funfcn{3},xcurr,varargin{:});
Error in lsqncommon (line 155)
[xC,FVAL,LAMBDA,JACOB,EXITFLAG,OUTPUT,msgData]= ...
Error in lsqcurvefit (line 253)
[xCurrent,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...
Error in test01 (line 55)
[theta,RSq,Rsd,ExFlg,OptmInfo,Lmda,Jmat]=lsqcurvefit(@kinetics,theta0,t,c);
I would like to know if anyone of you guys, has this problem before, and could you tell me how to fix it.
Thank you so much!!!

#### 0 件のコメント

サインイン to comment.

### 採用された回答

John D'Errico 2019 年 11 月 13 日
This is not a problem of lsqcurvefit tolerance. Instead, see that the error makes a statement about INTEGRATION tolerances. That suggests the solver is passing a set of parameters that make the integration fail. So it is ODE45 that is failing, not lsqcurvefit.
You may have better luck using a stiff solver (There are two such solvers in the ODE suite of tools. Anyone that ends in the letter s.)
Or you may need to constrain the parameters in lsqcurvefit, not allowing the solver to try parameter sets that are far out in the weeds.

#### 3 件のコメント

Johan Sebastian Diaz Tovar 2019 年 11 月 14 日
Hello John,
thank so much for clarify my doubt, I tried to do what you suggested to me, but even doing that, I'm getting the same problem that I attachted earlier. I didnot try your second one suggestion because actually I donot how to do it, if you can clarify me a little be more, I'll be thankful, or maybe suggest me another place where i can read information of it.
Once again, thank you so much for your value help!
John D'Errico 2019 年 11 月 14 日
I would use the debugger to see what set of parameters were passed to the ODE solver.
Set it to trap out when the error occurs, and then look at the parameters. Do they look insane? Somehow totally inconsistent with what you would expect? You should know what might make sense, since you know what the parameters mean in context. (I don't, of course.)
But then you should start thinking about the bounds in lsqcurvefit. If the solver is going someplace that seems completely insane, then you should try a set of lower/upper bounds that will keep it away from the bad place.
An important question is if the parameters it wants to use have hugely varying dynamic ranges. That can be a problem in the compiutations used to solve the system for lsqcurvefit. So lsqcurvefit might be getting a bit lost, which in turn will cause the ODE solver to be passed garbage for parameter sets.
So I'd suggest the very first thing to do is to look critically at where lsqcurvefit is trying to go.
Johan Sebastian Diaz Tovar 2019 年 11 月 15 日
John, I put the Delta temperature in the data, and the program run correctly, now I'm facing another problem such as the constants in order to have a better fit.
But thank you so much for your discussion with me!!
Regards.

サインイン to comment.