Sum polynomials function help?
조회 수: 19 (최근 30일)
이전 댓글 표시
채택된 답변
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
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
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
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.
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:')
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Polynomials에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!