Curve fitting with a constrained y value to Zero
Show older comments
I need to fit a curve ( Second-order polynomial ) with a constraint that a specific y-value equal to Zero
4.4 2.367224698
21.1 0
37.8 -1.857318083
54.4 -3.276015126
X & Y values as an example attached X = [ 4.4 21.1 37.8 54.4 ]
I want to fit the cuve where the y-value at x= 21.1 equal to zero
I am new to matlab and i have tried Curve fitting toolbox, I think it is not provided as a constraint in the toolbox
Accepted Answer
More Answers (2)
Serhii Tetora
on 13 Aug 2020
Edited: Serhii Tetora
on 13 Aug 2020
clear;close;clc
x = [4.4 21.1 37.8 54.4 ];
y = [2.367224698 0 -1.857318083 -3.276015126];
w = [1 1000 1 1];
[xData, yData, weights] = prepareCurveData( x, y, w );
% Set up fittype and options.
ft = fittype( 'poly2' );
opts = fitoptions( 'Method', 'LinearLeastSquares' );
opts.Weights = weights;
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
h = plot( fitresult, xData, yData, 'o' );
legend( h, 'y vs. x', 'fit', 'Location', 'NorthEast');
% Label axes
xlabel('x');
ylabel('y');
grid on
1 Comment
Khaled Bahnasy
on 13 Aug 2020
Using lsqlin,
x0=21.1;
x = [4.4 37.8 54.4 ].';
y = [2.367224698 -1.857318083 -3.276015126].';
p=lsqlin(x.^[2,1,0],y,[],[],x0.^[2,1,0],0)
Check:
>> polyval(p,21.1)
ans =
-4.4409e-16
2 Comments
Khaled Bahnasy
on 13 Aug 2020
The approximation error given by my approach is at the limit of floating point precision. It's meaningless to aspire beyond that. The value 21.1 doesn't even have an exact representation in a binary floating point computer. To 47 decimal places, the number that the computer is really holding when you enter x0=21.1 is,
'21.10000000000000142108547152020037174224853515625'
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!