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일

1 개 추천

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

Steve
Steve 2011년 10월 7일
second option sum polynomials of different degrees.
maybe you have a code of this.
Dr. Seis
Dr. Seis 2011년 10월 7일
so then x2 would yield
y = 5*x^5 + 6*x^4 + 7*x^3 + 8*x^2 + 9*x + 10
Steve
Steve 2011년 10월 7일
and I need to sum them :
y = 1*x^3 + 2*x^2 + 3*x + 4
+
y = 5*x^5 + 6*x^4 + 7*x^3 + 8*x^2 + 9*x + 10
using a matlab function or a code...
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일

1 개 추천

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

Steve
Steve 2011년 10월 7일
I know but say I have k polynomials. I need to add them in a function I am building. Is there a function that will get those polynomials and the result will be the sum of polynomials?
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일

0 개 추천

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:')

카테고리

도움말 센터File Exchange에서 Polynomials에 대해 자세히 알아보기

제품

질문:

2011년 10월 7일

답변:

2022년 11월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by