polynomial multiplication not accurate, using conv() vs symbolic

조회 수: 5 (최근 30일)
Qiang Li
Qiang Li 2024년 5월 31일
댓글: Qiang Li 2024년 6월 1일
I'm calculating the square of a polynomial inside a loop. It seems to me that, calculating it using conv() is resulting in large errors, compared to converting the polynomial to symbolic and do the calculation there.
Just out of curiosity, I tried to sym2poly the symbolic results back to polynomial, and the error appears again.
However, I can't using symbolic calculation in the loop, because it's too slow.
Any input on why this is happening and how to solve it properly? Thanks in advance.
figure
% the polynomial
D4_poly= 1.0e+06 * [ -0.000001003321333
0.000108002928093
-0.004647941272734
0.099959233380745
-1.074286120871490
4.615719487403345];
D4_fn = matlabFunction(poly2sym(D4_poly));
subplot(2,2,1);fplot(D4_fn,[19.999825010467916,23.158281544048581])
% using conv
SqD4_poly = conv(D4_poly',D4_poly');
SqD4_fn = matlabFunction(poly2sym(SqD4_poly));
subplot(2,2,2);fplot(SqD4_fn,[19.999825010467916,23.158281544048581])
% using poly2sym
temp = poly2sym(D4_poly);
SqD4_s_fn = matlabFunction(temp ^2);
subplot(2,2,3);fplot(SqD4_s_fn,[19.999825010467916,23.158281544048581])
% sym2poly
SqD4_poly_2 = sym2poly(temp ^2);
SqD4_3_fn = matlabFunction(poly2sym(SqD4_poly_2));
subplot(2,2,4);fplot(SqD4_3_fn,[19.999825010467916,23.158281544048581])

채택된 답변

Matt J
Matt J 2024년 5월 31일
편집: Matt J 2024년 5월 31일
To avoid the numerical instabilities of high order polynomials, don't work with the coefficients of D4_poly^2. Just evaluate D4_poly at the desired x and square the result:
D4_poly= 1.0e+06 * [ -0.000001003321333
0.000108002928093
-0.004647941272734
0.099959233380745
-1.074286120871490
4.615719487403345];
fcn=@(x)polyval(D4_poly,x).^2;
fplot(fcn, [19.999825010467916,23.158281544048581] ); axis tight
  댓글 수: 1
Qiang Li
Qiang Li 2024년 6월 1일
Matt, thx for pointing out the stability issue with high order polynomials.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by