is there any way to get a 2nd order polynominal through 2 points(each end point)

13 ビュー (過去 30 日間)
형국 김
형국 김 2023 年 3 月 23 日
コメント済み: 형국 김 2023 年 3 月 24 日
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?

回答 (2 件)

John D'Errico
John D'Errico 2023 年 3 月 23 日
編集済み: John D'Errico 2023 年 3 月 23 日
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;
plot(x,y,'o')
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)]
A = 20×3
0.0069 0.0832 1.0000 0.0113 0.1064 1.0000 0.0208 0.1441 1.0000 0.0247 0.1571 1.0000 0.0262 0.1619 1.0000 0.0606 0.2461 1.0000 0.1137 0.3372 1.0000 0.1483 0.3851 1.0000 0.1535 0.3918 1.0000 0.2134 0.4620 1.0000
b = y(:);
Aeq = A([1,end],:); % assumes the points are sorted in x
beq = b([1,end]);
Qcoeff = lsqlin(A,b,[],[],Aeq,beq)
Minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
Qcoeff = 3×1
-0.4724 -0.0640 1.0189
xpred = linspace(x(1),x(end));
ypred = polyval(Qcoeff,xpred);
plot(x,y,'bo',xpred,ypred,'r-')
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)]
Qcoeff2 = 3×1
-0.4724 -0.0640 1.0189
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.
  1 件のコメント
형국 김
형국 김 2023 年 3 月 24 日
Thank you for your reply. i will try to apply to my code. Thanks!

サインインしてコメントする。


Torsten
Torsten 2023 年 3 月 23 日
編集済み: Torsten 2023 年 3 月 23 日
rng("default")
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]);
% Example
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])
P = 
sym2poly(P)
ans = 1×3
0.2119 -3.5487 4.8105
hold on
plot(Xdata,Ydata,'o')
plot(Xdata,double(subs(P,x,Xdata)))
hold off
grid on

カテゴリ

Help Center および File ExchangeLinear Least Squares についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by