lsqcurvefit with a 'zero' in denominator
4 次查看(过去 30 天)
显示 更早的评论
Hello Everyone,
I have a data set [xdata,ydata], which are in fact calculated by a Fortran code. All I want to do is to fit these data into a specific nonlinear function. I have a clear definition of how this specific nonlinear function should be, and therefore I am trying to fit into that.
However, in order to try lsqcurvefit for a easier case, I used;
xdata=linspace(0,3,1000)
ydata=trip(xdata)
where trip is
tripl=1+xdata+xdata.^2+2*xdata.^3+153*xdata.^4-100*xdata.^5+(5-xdata).^-1;
Nonlinear function to be fitted is
function ftripl=ftrip(b,x)
ftripl=b(1)+b(2)*x+b(3)*x.^2+b(4)*x.^3+b(5)*x.^4+b(6)*x.^5+(b(7)-x).^-1;
end
I create initial parameters via
guess=4*rand(1,7)-2;
and run
fit=lsqcurvefit(@ftrip,guess,xdata,ydata);
I get decent values for all coefficients save b(7), which should attain the value 5. I recieve b(7)=1.02 with warning 'local minimum is possible'. How can I overcome this problem and find the value I require?
PS. In reality I will fit my data into a function of 5th order polynomial plus arctangent(a/(x-b)) where a,b and five coefficients of the polynomial will be found and x-b becomes very close to zero frequently.
2 个评论
the cyclist
2013-3-7
编辑:the cyclist
2013-3-7
@Batuhan,
I edited your question to format the code, but I am not 100% certain that I got your definition of trip exactly right, because you had written
trip = trip1 = ...
and I was not quite sure what you meant there. Please check to see if I interpreted it correctly. Sorry if I messed up your intention.
Maybe even better would be to express trip as an anonymous function:
trip = @(x) 1+x+x.^2+2*x.^3+153*x.^4-100*x.^5+(5-x).^-1;
and then
y = trip(xdata);
采纳的回答
Matt J
2013-3-7
编辑:Matt J
2013-3-7
In reality I will fit my data into a function of 5th order polynomial plus arctangent(a/(x-b)) where a,b and five coefficients of the polynomial will be found and x-b becomes very close to zero frequently.
If x-b is near zero at most data points x, you should probably just replace the arctangent term by pi/2*sign(a) and fit the remaining polynomial terms based on those data points only (using POLYFIT, of course, not LSQCURVEFIT). You can try each of the 3 possibilities for sign(a) and see which gives you the best fit.
If you want a good fit to the arctangent term, you'll need more data points away from the singularity.
1 个评论
Matt J
2013-3-7
编辑:Matt J
2013-3-7
If you want a good fit to the arctangent term, you'll need more data points away from the singularity.
As a compromise, after you've fit the polynomial terms b1...b6, you could also try obtaining a and b from the arctan term by doing
p=@(x) b(1)+b(2)*x+b(3)*x.^2+b(4)*x.^3+b(5)*x.^4+b(6)*x.^5 ;
zdata= tan(ydata - p(xdata));
A=[ones(length(zdata),1), zdata(:)];
B=zdata(:).*xdata(:);
ab=A\B;
a=ab(1);
b=ab(2);
更多回答(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!