Sum polynomials function help?

44 次查看(过去 30 天)
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.

采纳的回答

Dr. Seis
Dr. Seis 2011-10-7
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 个评论
Dr. Seis
Dr. Seis 2011-10-8
See edited answer above.
Bill Tubbs
Bill Tubbs 2021-8-9
编辑:Bill Tubbs 2021-8-9
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

请先登录,再进行评论。

更多回答(2 个)

John D'Errico
John D'Errico 2011-10-7
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 个评论
Walter Roberson
Walter Roberson 2011-10-8
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 2011-10-8
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

请先登录,再进行评论。


basilis
basilis 2022-11-7
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:')

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by