Azzera filtri
Azzera filtri

Sum polynomials function help?

43 visualizzazioni (ultimi 30 giorni)
Steve
Steve il 7 Ott 2011
Risposto: basilis il 7 Nov 2022
Hello Experts,
I have 2 or more polynomials of different degrees. Let x1 = [1,2,3,4] and x2=[5,6,7,8,9,10]
I need to sum x1 and x2 polynomials, please tell me what function does it.
Will appreciate any help.

Risposta accettata

Dr. Seis
Dr. Seis il 7 Ott 2011
If you mean you have polynomials with coefficients defined in x1 and x2 - for example the values in x1 would yield:
y = 1*x^3 + 2*x^2 + 3*x + 4;
Then you would sum the coefficients by pre-padding the array(s) of polynomial coefficients that are of smaller order. Example:
Create array of polynomial coefficients:
x1 = 1:4;
x2 = 5:10;
Create and save a new Matlab function:
function coeff_sum = sum_poly_coeff(x1, x2)
x1_order = length(x1);
x2_order = length(x2);
if x1_order > x2_order
max_order = size(x1);
else
max_order = size(x2);
end
new_x1 = padarray(x1,max_order-size(x1),0,'pre');
new_x2 = padarray(x2,max_order-size(x2),0,'pre');
coeff_sum = new_x1 + new_x2;
return
You can run this from the command line and view results by:
x3 = sum_poly_coeff(x1, x2);
x = 0:0.1:10;
plot(x,polyval(x1,x),'b-',x,polyval(x2,x),'r-',x,polyval(x3,x),'g-');
Or if you just want to see the result, you don't even have to create a function. Just do this at the command line:
x = 0:0.1:10;
plot(x,polyval(x1,x),'b-',x,polyval(x2,x),'r-',x,polyval(x1,x)+polyval(x2,x),'g-');
  5 Commenti
Dr. Seis
Dr. Seis il 8 Ott 2011
See edited answer above.
Bill Tubbs
Bill Tubbs il 9 Ago 2021
Modificato: Bill Tubbs il 9 Ago 2021
The only exception to this rule, is when the terms are decreasing negative powers. It seems that in this case, the first coefficient in the shorter vector corresponds to order zero (z^0) which is the highest order, and therefore the vector needs to be padded at the end, not the beginning.
Well, I assume that is the convention based on the following test.
1. polynomial in z is pre-padded:
>> tf1 = tf(1, [2 1], Ts)
>> tf1.Numerator{1}
ans =
0 1
2. polynomial in z^-1 is 'post-padded':
>> tf2 = tf(1, [2 1], Ts, 'Variable', 'z^-1')
>> tf2.Numerator{1}
ans =
1 0

Accedi per commentare.

Più risposte (2)

John D'Errico
John D'Errico il 7 Ott 2011
These are not truly polynomials. They are vectors of coefficients, that you choose to interpret as polynomials. How do you add them? Trivial. Pad the shorter one with zeros on the left, so they two vectors are the same length. Then adding the two vectors is truly trivial.
  3 Commenti
Walter Roberson
Walter Roberson il 8 Ott 2011
Find the max() of the length() of the coefficient vectors, and pad everything on the left to match that size, and then just add the padded vectors.
Steve
Steve il 8 Ott 2011
Thanks!
Walter and others who helped me, thank you very much!
You know I have a blog dedicated to learning and sharing information.
If it is acceptable and you are interested here is the address:
shareinfoblog.blogspot.com

Accedi per commentare.


basilis
basilis il 7 Nov 2022
hello experts,i have 2 polynomials to sum with different degree in line 16 (error)
clc
clear
%%Data Input
k=5*(10^6);
m=1000;
a=k/(4*m*(pi^2));
p1=[-1 0 a];
p2=[-1 0 2*a];
p3=[a^2 0 -2*(a^3)];
%%Calculations
p4=p2.^2-a^2;
p5=conv(p1,p4);
P=p3+p5;
roots(P)
%%Results Output
disp('the frequences are:')

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by