polynomial multiplication not accurate, using conv() vs symbolic

7 views (last 30 days)
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])

Accepted Answer

Matt J
Matt J on 31 May 2024
Edited: Matt J on 31 May 2024
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

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!