is there any way to get a 2nd order polynominal through 2 points(each end point)
2 views (last 30 days)
Show older comments
형국 김 on 23 Mar 2023
Commented: 형국 김 on 24 Mar 2023
i have some points and i want to make a 2nd order polynominal function using the points.
and it must be through the 2 points which are the start and end points.
how can i make a polynominal function?
John D'Errico on 23 Mar 2023
Edited: John D'Errico on 23 Mar 2023
You want to fit a 2nd degree polynomial through two points? Of course only a straight line would work there through two points, unless your goal is to find the best quadratic through the rest of the data too. So a quadratic that fits the data bast, but also hits exactly the first and last points. This is pretty simple really. We could use lsqlin. or a variety of other solutions.
For example, suppose your data is:
x = sort(rand(20,1));
y = cos(x) + randn(size(x))/30;
So it looks vaguelty quadratic. We want to force the curve through the first and last data points. I'll show how to do so using lsqlin.
A = [x(:).^2, x(:), ones(numel(x),1)]
b = y(:);
Aeq = A([1,end],:); % assumes the points are sorted in x
beq = b([1,end]);
Qcoeff = lsqlin(A,b,,,Aeq,beq)
xpred = linspace(x(1),x(end));
ypred = polyval(Qcoeff,xpred);
As you can see, it passes exactly through the first and last points.
However, we could also have done the same task easily enough, by realizing the curve we want can be written as the sum of two curves. So we find the linear polynomial that passes exactly through the first and last points. Then find a quadratic term that is zero at the first and last data point, and fits the remainder of the curve. You might call this scheme a cousin of Lagrange interpolation, since I'm using a scheme closely related to those ideas.
% again, assuming the data is sorted in x
P1 = polyfit([x(1),x(end)],[y(1),y(end)],1);
Q = ((x(:) - x(1)).*(x(:) - x(end)))\(y(:) - polyval(P1,x(:)));
% recover the full quadratic polynomial as
Qcoeff2 = [Q;P1(1)-Q*(x(1) + x(end));P1(2)+Q*x(1)*x(end)]
Here too, we get exactly the same coefficients as we got from lsqlin. This is as it must be. The virtue of this latter approach is you don't need to use an optimization toolbox code like lsqlin. Everything is already in MATLAB proper.
Torsten on 23 Mar 2023
Edited: Torsten on 23 Mar 2023
syms a b c x y
n = 30; % number of data points
xdata = sym('xdata',[1 n]); % x values
ydata = sym('ydata',[1 n]); % y values
% Prepare such that polynomial passes through first and last data point
P1 = [xdata(1) ydata(1)];
P2 = [xdata(end) ydata(end)];
eqn = a*x.^2+b*x+c == y;
[asol bsol] = solve([subs(eqn,[x y],[P1(1) P1(2)]),subs(eqn,[x y],[P2(1) P2(2)])],[a b]);
eqn = subs(eqn,[a b],[asol,bsol]);
% Compute polynomial
eqns = arrayfun(@(i)subs(eqn,[x y],[xdata(i),ydata(i)]),2:n-1);
f = sum((lhs(eqns)-rhs(eqns)).^2);
df = diff(f,c);
csol = solve(df==0,c);
p = subs(lhs(eqn),[a b c],[asol,bsol,csol]);
Xdata = 0.1:0.1:3;
Ydata = 0.3*Xdata.^2-3.6*Xdata+4+rand(1,30);
P = subs(p,[xdata ydata],[Xdata Ydata])
Find more on Linear and Nonlinear Regression 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!