Polynomial Approximation, is it possible in matlab?

I have four points on the graph with the following coordinates.
x1 - 1
y1 - 3.5
x2 - 2
y2 - 14/3
x3 - 3
y3 - 14
x4 - 4
y4 - 28
Is it possible using the Lagrange approximation polynomial coefficient calculation method to find the polynomial / function given by the four points?
I don't know the algorithm very well and I don't have the strongest matlab knowledge. I'm learning.
Thank you.
MATLAB Version: 8.5.0.197613 (R2015a)

回答 (1 件)

Luna
Luna 2019 年 1 月 3 日
編集済み: Luna 2019 年 1 月 3 日

1 投票

Hi,
Try below (it uses least squares):
For lagrange you can look at that link:
x1 = 1;
y1 = 3.5;
x2 = 2;
y2 = 14/3;
x3 = 3;
y3 = 14;
x4 = 4;
y4 = 28;
x1Array = [x1,x2,x3,x4];
y1Array = [y1,y2,y3,y4];
n = 1; % polynomial degree (you can change it as you wish)
p = polyfit(x1Array,y1Array,n); % p is coefficient of your polynomial: P(X) = P(1)*X^N + P(2)*X^(N-1) +...+ P(N)*X + P(N+1) descending order.
newY = polyval(p,x1Array); % function results
plot(x1Array,y1Array, 'bo',x1Array,newY,'-r');
grid;
legend('Data','Fitted Data');

11 件のコメント

Milky Way
Milky Way 2019 年 1 月 4 日
Ok. But where is the function, the polynomial. When I run the program I only show a graph.
Matwork.png
madhan ravi
madhan ravi 2019 年 1 月 4 日
編集済み: madhan ravi 2019 年 1 月 4 日
reduce n less than 4
Milky Way
Milky Way 2019 年 1 月 4 日
Okay. But how can I find the polynomial? Simple form: aX^n + bX^(n-1) .... the polynomial itself.
matwork.png
Steven Lord
Steven Lord 2019 年 1 月 4 日
See the comment on line 12.
Milky Way
Milky Way 2019 年 1 月 4 日
編集済み: Milky Way 2019 年 1 月 4 日
@Steven Lord Ok. n=3 => P(1)*X^3 + P(2)*X^2 + P(3)*X + P(4)
And how those coefficients can be calculated? (P(1), P(2), P(3) and P(4)). I can't figure it out.
Torsten
Torsten 2019 年 1 月 4 日
p(x)=(x-x2)*(x-x3)*(x-x4)/((x1-x2)*(x1-x3)*(x1-x4))*y1+...
(x-x1)*(x-x3)*(x-x4)/((x2-x1)*(x2-x3)*(x2-x4))*y2+...
(x-x1)*(x-x2)*(x-x4)/((x3-x1)*(x3-x2)*(x3-x4))*y3+...
(x-x1)*(x-x2)*(x-x3)/((x4-x1)*(x4-x2)*(x4-x3))*y4
Multiply out and order according to powers of x.
Milky Way
Milky Way 2019 年 1 月 4 日
And can't be solved in Matlab? I wonder if it can be solved in Matlab to get rid of "manual" work.
Torsten
Torsten 2019 年 1 月 4 日
Yes, the coefficients can be taken from the line
p = polyfit(x1Array,y1Array,n); % p is coefficient of your polynomial: P(X) = P(1)*X^N + P(2)*X^(N-1) +...+ P(N)*X + P(N+1) descending order.
of your above code.
Milky Way
Milky Way 2019 年 1 月 4 日
post.png
Yes. Thank you very much, I'm at the beginning, but as I said, I'll learn. :)
Torsten
Torsten 2019 年 1 月 4 日
But you know that using the interpolating spline method to calculate the coefficients is not what you are supposed to do in your homework (it says something about "Lagrange interpolation polynomial", doesn't it) ??
Luna
Luna 2019 年 1 月 4 日
Yes, it says lagrange that's why I gave the lagrangepoly link from fileexchange in my answer.
Please check the file and use. It does the same thing with only Lagrange method.
It also explains with examples.

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

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

リリース

R2015a

質問済み:

2019 年 1 月 3 日

コメント済み:

2019 年 1 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by