using polyval with one of the polynomials of a spline obtained with... (2024)

31 ビュー (過去 30 日間)

古いコメントを表示

T Hafid 約15時間 前

  • リンク

    この質問への直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results

  • リンク

    この質問への直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results

コメント済み: dpb 約10時間 前

採用された回答: David Goodmanson

MATLAB Online で開く

I first interpolated some data using the spline command. Then I calculated the coefficients of the different cubic polynomials that make up the spline. Then I used the polyval command to check the first polynomial but the spline and polyval curves do not overlap.

What am I doing wrong?.

There is definitely something related to the various polynomials that I have not understood correctly. I thank anyone who helps me understand better. See the file script please:

%butta_sto_test

%

clear all

clc

% Data

% Time

Time=[7, 9, 11, 12]

% Temperture

Temperature=[49, 57, 71, 75]

% Data Time query

Timeq=[7:0.1:12];

% Results

% piecewise polynomial structure

pp=spline(Time, Temperature);

% Timeq data interpolation

Temperatureq=spline(Time, Temperature, Timeq);

% Calculating the coefficients of the 3 polynomials that make up the spline

[breaks, coeffs, m, n] = unmkpp(spline(Time,Temperature));

% First polinomial p1

p1=coeffs(1,:)

% First time interval corresponding to the first polynomial of the spline

Timeq1=[7:0.1:9];

% using polyval to calculate the values corresponding to Timeq1 using the polynomial p1

Temperatureq1=polyval(p1,Timeq1);

% Spline plot

figure(1)

plot(Timeq, Temperatureq, Time, Temperature, 'o', Time, Temperature, '--')

hold on

plot(Timeq1, Temperatureq1,'*')

grid on

0 件のコメント

-2 件の古いコメントを表示-2 件の古いコメントを非表示

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

サインインしてこの質問に回答する。

採用された回答

David Goodmanson 約15時間 前

  • リンク

    この回答への直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#answer_1473861

  • リンク

    この回答への直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#answer_1473861

編集済み: David Goodmanson 約15時間 前

MATLAB Online で開く

Hello TH,.

spline polynomials are local to the secgment in question starting from x = 0, so if an interval is, for example 3 to 4.4, polyval uses 0 to 1.4. If you use

Timeq1=[7:0.1:9]; % existing line

Timeq1poly = Timeq1-Timeq1(1);

% using polyval to calculate the values corresponding to Timeq1 using the polynomial p1

Temperatureq1=polyval(p1,Timeq1poly);

then it works.

using polyval with one of the polynomials of a spline obtained with... (3)

3 件のコメント

1 件の古いコメントを表示1 件の古いコメントを非表示

Steven Lord 約14時間 前

このコメントへの直接リンク

https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#comment_3189866

  • リンク

    このコメントへの直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#comment_3189866

That is correct. From the description of the pp output argument on the spline documentation page:

Since the polynomial coefficients in coefs are local coefficients for each interval, you must subtract the lower endpoint of the corresponding knot interval to use the coefficients in a conventional polynomial equation. In other words, for the coefficients [a,b,c,d] on the interval [x1,x2], the corresponding polynomial is

using polyval with one of the polynomials of a spline obtained with... (5)

T Hafid 約11時間 前

このコメントへの直接リンク

https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#comment_3190136

  • リンク

    このコメントへの直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#comment_3190136

I understand perfectly. Thank you very much.

T Hafid 約11時間 前

このコメントへの直接リンク

https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#comment_3190141

  • リンク

    このコメントへの直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#comment_3190141

Thank you

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

その他の回答 (2 件)

dpb 約14時間 前

  • リンク

    この回答への直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#answer_1473891

  • リンク

    この回答への直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#answer_1473891

MATLAB Online で開く

% Data

% Time

Time=[7, 9, 11, 12];

% Temperture

Temperature=[49, 57, 71, 75];

% Data Time query

Timeq=[7:0.1:12];

% Results

% piecewise polynomial structure

pp=spline(Time, Temperature);

% Timeq data interpolation

Temperatureq=spline(Time, Temperature, Timeq);

plot(Time,Temperature,'*')

hold on

plot(Timeq,Temperatureq)

Ts=ppval(pp,Timeq);

plot(Timeq,Ts,'x')

[bk,p]=unmkpp(pp)

bk = 1x4

7 9 11 12

<mw-icon class=""></mw-icon>

<mw-icon class=""></mw-icon>

p = 3x4

-0.3500 2.8500 -0.3000 49.0000 -0.3500 0.7500 6.9000 57.0000 -0.3500 -1.3500 5.7000 71.0000

<mw-icon class=""></mw-icon>

<mw-icon class=""></mw-icon>

T1=bk(1):0.1:bk(2);

Tp=polyval(p(1,:),T1-bk(1));

plot(T1,Tp,'+')

xlim([7 9]), ylim([45 60])

using polyval with one of the polynomials of a spline obtained with... (9)

Spline coefficients are local to the break region so to evaluate by conventional polynomial functions you must subract the leading breakpoint from each section. Since this is done for you automagically by ppval, it's nonsense to not use the toolset for the job.

3 件のコメント

1 件の古いコメントを表示1 件の古いコメントを非表示

David Goodmanson 約14時間 前

このコメントへの直接リンク

https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#comment_3189981

  • リンク

    このコメントへの直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#comment_3189981

It may be nonsense to not use spline & ppval for a production-type of job, but it is the exact opposite of nonsense, it's commendable to have curiosity and make the comparison that the OP did here. If everything is treated like a black box provided by Mathworks (and they do not have that attitude), how do people gain knowledge and perspective about the mathematical processes they are using?

dpb 約13時間 前

このコメントへの直接リンク

https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#comment_3190026

  • リンク

    このコメントへの直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#comment_3190026

I didn't say it wasn't a worthwhile exercise to figure out; only to not use the wrong toolset for the job... :)

Perhaps "nonsense" wasn't a good choice, granted.

T Hafid 約11時間 前

このコメントへの直接リンク

https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#comment_3190151

  • リンク

    このコメントへの直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#comment_3190151

Thanks for the explanations

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

John D'Errico 約14時間 前

  • リンク

    この回答への直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#answer_1473906

  • リンク

    この回答への直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#answer_1473906

MATLAB Online で開く

@David Goodmanson and @dpb and @Steven Lord have all said the polynomial segments are intended to be LOCAL with respect to the break points. I'm adding this as an answer only because I want to explain why it is done that way.

Supose I gave you two curves to interpolate using a spline, thus f(x1,y) and f(x2,y).

x1 = 0:1:10;

x2 = 1e8 + (0:1:10);

y = rand(1,11); % I dont give a hoot what values you choose for y.

spl1 = spline(x1,y);

spl2 = spline(x2,y);

The two curves should, in theory, be virtually identical. All I have done is horizontally translate the curve in x by some very large number.

Remember, these will be cubic splines. If the cubic segments for these splines were not local things, then in spl2, you would be forming a cubic polynomial where you are raising numbers on the order of 1e8 to the third power, and then adding and subtracting those values to other nubmers. Those polynomials would be unmanagable. You would expect to see massive numerical problems.

You don't want that. Even smaller shifts would cause serious problems. The solution is trivial. Just formulate each segment to be defined as a cubic polynomial, but where the LEFT end of each segment ALWAYS starts at zero, and has length given by diff(breaks). This resolves the numerical problems as much as possible.

5 件のコメント

3 件の古いコメントを表示3 件の古いコメントを非表示

dpb 約13時間 前

このコメントへの直接リンク

https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#comment_3190046

  • リンク

    このコメントへの直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#comment_3190046

@John D'Errico. I have wondered why the builtin tools don't also use the standardized centering and unit standard deviation as can do with polyfit/polyval, etc., ...

I suppose there are some implementatiions that do.

John D'Errico 約12時間 前

このコメントへの直接リンク

https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#comment_3190096

  • リンク

    このコメントへの直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#comment_3190096

編集済み: John D'Errico 約12時間 前

@dpb That is an entirely valid question. I can offer a few reasons, some more valid than others.

  1. The author did not think of it,
  2. The author did not think it necessary.
  3. Use of centering and scaling adds some slight additional mathematics to compute the polynomial coefficients. In my eyes, the choices would be to shift/scale things so the polynomial lives always on [-1,1], but I could also see arguments to map the interval always to [0,1]. I might like the latter, personally, but the difference is arguably minimal. A [0,1] interval makes the polynomial a bit easier to inspect, just by looking at the coefficients. And since that is something I have actually had reason to do on occasion, I might instinctively prefer it. But, do we need it? See below.

Alternative 1 I can almost dismiss out of hand. There were some seriously good numerical analysts involved in the writing of those codes, and that is not a mistake they would have made, if it might actually be a "mistake".

Ok, so now I need to return to alternative 2, and discuss why, in fact, a scaling is not necessary, as long as a shift is performed. As long as the lower endpoint is zero, we seriously don't care.

Consider a polynomial that lives on the interval [0,h], and another polynomial that lives on the interval [0,1]. Assuming the two polynomials have exactly the same fundamental shape, except for the scaling, what will they look like? Can we transform one polynomial into the other easily enough?

[0,h] interval:

P_h(x) = a0*x^3 + a1*x^2 + a2*x + a3

[0,1] interval:

P_1(x) = a0*(h*x)^3 + a1*(h*x)^2 + a2*(h*x) + a3

= (a0*h^3)*x^3 + (a1*h^2)*x^2 + (a2*h)*x + a3

So the two polynomials will behave exactly the same! Yes, the coefficients of P_1(x) are different. But look carefully at those coefficients. The interval length merely gets absorbed into the corresponding coefficient from x, which now lives on [0,1]. Therefore we will have no more and no less problems in terms of floating point arithmetic with P_h and P_1.

And I would suggest alternative 2 was the reason, that there is no gain to be had from using a standardized unit interval. You get all of the gain just by making the lower endpoint 0.

dpb 約11時間 前

このコメントへの直接リンク

https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#comment_3190146

  • リンク

    このコメントへの直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#comment_3190146

@John D'Errico, thanks as always for clear elucidation! It is obvious when actually write it down...as the algebra shows, the amplitude is scaled but the relative change in terms has to be the same. Hadn't recognized the zero shift by itself having that effect before.

Steven Lord 約11時間 前

このコメントへの直接リンク

https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#comment_3190156

  • リンク

    このコメントへの直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#comment_3190156

MATLAB Online で開く

Which "builtin tools" are you asking about? Just spline or do you have others in mind? The spline function has been in MATLAB for a long time (it looks like nearly 38 years according to one of the comments):

dbtype 65:70 spline.m

65 %66 % See also INTERP1, MAKIMA, PCHIP, PPVAL, MKPP, UNMKPP.67 68 % Carl de Boor 7-2-8669 % Copyright 1984-2019 The MathWorks, Inc.70

I did a little quick archaeology to determine when the center-and-scale output was added to polyfit etc. It looks like it was in release R12 back in 2000 (about a year before I started at MathWorks), 14 years after spline.m was written by or in conjunction with Prof. de Boor.

dpb 約10時間 前

このコメントへの直接リンク

https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#comment_3190176

  • リンク

    このコメントへの直接リンク

    https://jp.mathworks.com/matlabcentral/answers/2129816-using-polyval-with-one-of-the-polynomials-of-a-spline-obtained-with-the-unmkpp-command-the-results#comment_3190176

I was thinking specifically of both spline and interp1, but also are the options in Curve Fitting TB with fit based on the same algorithms/methods?

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

サインインしてこの質問に回答する。

参考

タグ

  • spline unmkpp polyval interpolation

製品

  • MATLAB

リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

エラーが発生しました

ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。


Translated by using polyval with one of the polynomials of a spline obtained with... (19)

using polyval with one of the polynomials of a spline obtained with... (20)

Web サイトの選択

Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:

また、以下のリストから Web サイトを選択することもできます。

南北アメリカ

  • América Latina (Español)
  • Canada (English)
  • United States (English)

ヨーロッパ

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

アジア太平洋地域

最寄りの営業オフィスへのお問い合わせ

using polyval with one of the polynomials of a spline obtained with... (2024)

FAQs

What is the formula for polyval? ›

y = polyval( p , x ) evaluates the polynomial p at each point in x . The argument p is a vector of length n+1 whose elements are the coefficients (in descending powers) of an n th-degree polynomial: p ( x ) = p 1 x n + p 2 x n − 1 + ... + p n x + p n + 1 .

How to extract coefficients from a polynomial in MATLAB? ›

C = coeffs( p , vars ) returns coefficients of the multivariate polynomial p with respect to the variables vars . [ C , T ] = coeffs(___) returns the coefficient C and the corresponding terms T of the polynomial p . ___ = coeffs(___,'All') returns all coefficients, including coefficients that are 0.

What is the difference between Polyfit and Polyval in MATLAB? ›

polyfit(x,y,n) finds the coefficients of a polynomial p(x) of degree n that fits the y data by minimizing the sum of the squares of the deviations of the data from the model (least-squares fit). polyval(p,x) returns the value of a polynomial of degree n that was determined by polyfit , evaluated at x .

How to generate polynomials in MATLAB? ›

p = poly2sym( c ) creates the symbolic polynomial expression p from the vector of coefficients c . The polynomial variable is x . If c = [c1,c2,...,cn] , then p = poly2sym(c) returns c 1 x n − 1 + c 2 x n − 2 + ... + c n .

What does polyval do in Python? ›

Evaluate a polynomial at specific values. This forms part of the old polynomial API.

What is the formula for the polynomial regression equation? ›

What is Polynomial Regression? Polynomial Regression models the relationship between an independent variable x and a dependent variable y as an n degree polynomial. It aims to estimate the best coefficients for the equation: y = b_0 + b_1x + b_2x^2+... +b_nx^n +e.

What is polyval MATLAB? ›

Description. y = polyval(p,x) returns the value of a polynomial of degree n evaluated at x . The input argument p is a vector of length n+1 whose elements are the coefficients in descending powers of the polynomial to be evaluated. x can be a matrix or a vector. In either case, polyval evaluates p at each element of x ...

How do you find the coefficients of a polynomial? ›

How to find polynomial coefficients. Polynomial coefficients are the numbers that come before a term. Terms usually have a number and a variable (e.g. 2 x 2 2x^2 2x2, where 2 is the number, and x is the variable). The number portion is the coefficient.

How to derive a polynomial in MATLAB? ›

k = polyder( p ) returns the derivative of the polynomial represented by the coefficients in p , k ( x ) = d d x p ( x ) . k = polyder( a,b ) returns the derivative of the product of the polynomials a and b , k ( x ) = d d x [ a ( x ) b ( x ) ] .

What is the Polyfit equation in MATLAB? ›

Fit Polynomial to Set of Points

In general, for n points, you can fit a polynomial of degree n-1 to exactly pass through the points. p = polyfit(x,y,4); Evaluate the original function and the polynomial fit on a finer grid of points between 0 and 2. x1 = linspace(0,2); y1 = 1./(1+x1); f1 = polyval(p,x1);

What is the use of poly function in MATLAB? ›

The poly function converts the roots back to polynomial coefficients. When operating on vectors, poly and roots are inverse functions, such that poly(roots(p)) returns p (up to roundoff error, ordering, and scaling).

What is the difference between spline and interp1 in MATLAB? ›

While spline performs interpolation on rows of an input matrix, interp1 performs interpolation on columns of an input matrix.

How do you find the product of a polynomial in MATLAB? ›

The matlab function conv (convolution) can be used to perform polynomial multiplication. For example: B1 = [1 1]; % 1st row of Pascal's triangle B2 = [1 2 1]; % 2nd row of Pascal's triangle B3 = conv(B1,B2) % 3rd row % B3 = 1 3 3 1 B4 = conv(B1,B3) % 4th row % B4 = 1 4 6 4 1 % ...

How to add two polynomials in MATLAB? ›

c = gfadd(a,b) adds two GF(2) polynomials, a and b , which can be either polynomial character vectors or numeric vectors. If a and b are vectors of the same orientation but different lengths, then the shorter vector is zero-padded. If a and b are matrices they must be of the same size.

How to get coefficients of a polynomial in MATLAB? ›

To extract symbolic coefficients of a polynomial, use coeffs . This function returns a symbolic vector of coefficients and omits all zeros. For example, syms a b x; c = coeffs(a*x^3 - 5*b,x) returns c = [ -5*b, a] .

What does polyval do in Matlab? ›

polyval (MATLAB Function Reference) y = polyval(p,x) returns the value of the polynomial p evaluated at x . Polynomial p is a vector whose elements are the coefficients of a polynomial in descending powers. x can be a matrix or a vector.

What is the formula for the nth degree Taylor polynomial? ›

The first part of Taylor's Theorem states that f(x)=pn(x)+Rn(x), where pn(x) is the nth order Taylor polynomial and Rn(x) is the remainder, or error, in the Taylor approximation. The second part gives bounds on how big that error can be.

What is the significance of the command polyval()? ›

The polyval command also significantly reduces rounding error when plugging values into higher order polynomials, so it is always better to use polyval than to type out the equation coeffs(1)*xplot + coeffs(2) directly.

How do you calculate interpolating polynomials? ›

To find the interpolation polynomial p(x) in the vector space P(n) of polynomials of degree n, we may use the usual monomial basis for P(n) and invert the Vandermonde matrix by Gaussian elimination, giving a computational cost of O(n3) operations.

Top Articles
Latest Posts
Article information

Author: Horacio Brakus JD

Last Updated:

Views: 5948

Rating: 4 / 5 (71 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Horacio Brakus JD

Birthday: 1999-08-21

Address: Apt. 524 43384 Minnie Prairie, South Edda, MA 62804

Phone: +5931039998219

Job: Sales Strategist

Hobby: Sculling, Kitesurfing, Orienteering, Painting, Computer programming, Creative writing, Scuba diving

Introduction: My name is Horacio Brakus JD, I am a lively, splendid, jolly, vivacious, vast, cheerful, agreeable person who loves writing and wants to share my knowledge and understanding with you.