Errors when changing bounds for custom function fit
1 次查看(过去 30 天)
显示 更早的评论
I am trying to fit some data to a custom function, but the fit doesn't seem as accurate as I would like. Additionally, when I change the bounds for the fit I receive an error "NaN/Inf computed by model function, fitting cannot continue. Try using or tightening upper and lower bounds on coefficients." This happens even if I tighten the bounds from ones that worked (for example, changing the upper bounds from inf to 100).
w1+w2=1
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1349874/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1349879/image.png)
xdata = [0; 0.8; 1.05; 1.3; 1.5; 1.75; 2; 2.3; 2.7; 3.6; 4.75];
ydata = [0; 0.1; 0.2; 0.3; 0.4; 0.5; 0.6; 0.7; 0.8; 0.9; 1];
curvezero = fittype( @(wA,nA,nB,kA,kB,x) wA.*nA.*kA.*(exp(-(kA.*x).^nA)).*((kA.*x).^nA).^((nA-1)/nA) + (1-wA).*nB.*kB.*(exp(-(kB.*x).^nB)).*((kB.*x).^nB).^((nB-1)/nB) );
fzero = fit( xdata, ydata, curvezero, 'StartPoint', [0.5, 1, 1, 1, 1], 'Lower', [0.5, 0, 0, 0, 0], 'Upper', [1, inf, inf, inf, inf] );
plot( fzero, xdata, ydata )
0 个评论
回答(1 个)
Rasmita
2023-5-12
Hi,
As per my understanding, you are trying to fit some data to a custom function. You are facing two issues, one is the fit is not accurate and second one; when you try to change the bounds for the fit, you are getting above error. You have tried tightening the bounds, but the issue persists.
One possible reason could be the parameters being fitted may be going beyond the bounds you have specified, which can cause the function to return NaN or Inf.
To avoid this error, you can try tightening the bounds on the parameters. You have already set the lower bounds to zero, which is a good start. Try to specify upper bounds that are reasonable for your problem, based on your knowledge of the domain. For example, if you know that a parameter should not be larger than a certain value, set that as the upper bound. In your case, it seems like the ‘nA’ and ‘nB’ parameters should be positive, so you can try upper bounds of something like 100 or 1000, which should be reasonable.
Further, a workaround you can try is to change your fitting function to make it more stable. Use a logarithmic transformation to rewrite your function so that it is better conditioned for numerical optimization. This could be carried out by taking the logarithm of both sides of your equation and then fitting the transformed data. This can often lead to a more accurate and robust fit.
Hope this helps!
Regards,
Rasmita
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!