Fit-Curve through certain start point
2 views (last 30 days)
Show older comments
How can I define a fit function out of an array, that goes through a defined start point and end point? Also, if there is a point out of zeros, it should not be considered. I tried it with the fit function and chose the "NonLinearLeastSquares" Method.
data = [0 1 2 3 4 5 6 0 8 9 ; 3 5 4 6 8 6 5 0 5 2]
xdata = data(1,:);
ydata = data(2,:);
start = [xdata(1) ydata(1)];
exclude = [0 0]
options = fitoptions('Method','NonlinearLeastSquares','Startpoint',start,'Exclude',exclude)
fit_xy = fit(xdata',ydata','poly5',options);
ydata_fitted = feval(fit_xy,xdata);
figure
plot(xdata,ydata,'-ob');
hold on
plot(xdata,ydata_fitted,'-or');
plot(fit_xy);
Thanks a lot!
0 Comments
Accepted Answer
dpb
on 25 Apr 2018
Edited: dpb
on 27 Apr 2018
The easiest way is to use one of John D'Errico's inestimably-valuable tools from the FEX; in this case <LSE> which was written expressly for the purpose.
N=2; % choose poly power (quadratic)
iC=[1 length(X)]; % pick points indices for constraints (first, last here)
A=X.^[0:N]; % build a LS model matrix for the chosen power
C=A(iC,:); % and the constrained points subset
b=lse(A,Y,C,Y(iC)) % call John's magic...
b =
3.0000
1.6417
-0.1948
>> C*b % see that constraints were satisfied
ans =
3.0000
2.0000
>> yh=A*b % evaluate model at input points; observe constraints satisfied
yh =
3.0000
4.4470
5.5044
6.1723
6.4507
6.3396
5.8390
3.6692
2.0000
>>
To evaluate the model at any other points, simply build the appropriate A including the desired points in the vector.
ADDENDUM Oh, forgot to address the question of missing data (0-valued); had already done that in the above...
data = [0 1 2 3 4 5 6 0 8 9 ; 3 5 4 6 8 6 5 0 5 2]; % original
data=data.'; % columns are generally more convenient
data=data(any(data,2),:); % eliminate rows that have both columns 0
3 Comments
dpb
on 26 Apr 2018
Edited: dpb
on 28 Apr 2018
Double and triple :) !!!
This most-excellent tool has bailed me out numerous times over the past rather than having to relearn/reinvent the wheel; there's no way to express the level of appreciation that you took the time and effort to build such tools and submit them via FEX...
ERRATUM I see I didn't capitalize the 'D'...didn't recall that; will fix--dpb
GEEZER TALE ALERT Once upon a time when a much younger pup about 45 year ago now, had issues with a 2D surface fit of Rh-emitter incore detectors to estimate power distribution across the 177 fuel assembly locations from the 52 (very strangely distributed; I didn't have anything to do with that; happened and was cast in stone long before I ever got to the vendor) instrumented locations. Compute-power was extremely limited so was a quadratic polynomial in XY when I inherited the software with no constraints. As with all polynomials the fit was very sensitive to noise, especially at the boundary locations for larger XY. I was sure that adding a boundary condition of zero power/flux around the outer edge of the core periphery would help.
I was able to use SAS to model a number of cases for "proof of principle" that was a viable and probable improvement but I hadn't at the time the mathematical prowess to figure out how to code the solution. Fortunately, there was on staff a Senior Mathematician who was available for such problems and Dr. Clark showed me how to solve the problem and it made a big improvement in the overall results in computing local power peaking. He is now long deceased but I learned much from him that was also of absolute critical importance over the next 30 years...
More Answers (0)
See Also
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!