How to use use an anonymous function to fit data based on the fittype and fit functions.
5 views (last 30 days)
I have some sort of data point and I want to use an anonymous function to fit this data based on the fittype and fit functions.
Also, the function is look like this:
William Rose on 24 Jun 2022
I think the manual page examples for this are pretty good.
a=1; b=2; c=-1; d=-2; e=3;
p = lsqcurvefit(fun,p0,xdata,ydata)
legend('Clean Data','Noisy Data','Best Fit')
The fit to the data is excellent, and the two curves are so close that they overlap, but the fitted parameters do not match the original parameters. This is because the input diff(x) is a constant, and therefore is not independent of the "c" term, and the input diff(x^2) is a straight line, and therefore not indepndent of the "a" term.
Try this. Good luck.
More Answers (2)
Walter Roberson on 24 Jun 2022
There is no chance of fitting that function.
fit() and fittype() pass in numeric data, and diff() of numeric data is always smaller than the original data. Therefore your a*x^2+b*x+c is an incompatible size to be added to d*diff(x^2)+e*diff(x)
It might make sense to rewrite in terms of gradient()
Does diff(x) happen to be constant (points are equally spaced)? If so then I suspect you can rewrite the formulas for more efficient fitting