Section 9.5 Adding Polynomials
Adding polynomials is easy: you simply add terms of equal powers of x:
Then, if we have
In MATLAB you thus simply add the corresponding vectors of coefficients:
>> p1 = [1 -3 1]; >> p2 = [3 0 2]; >> p = p1 + p2
p = 4 -3 2
The only thing you have to watch out for is polynomials of different orders. You must pad the lower order polynomial with zeros before adding them together since otherwise the vectors of coefficients wouldn’t have the same length. Take a look:
Then, if we have
Without padding the second polynomial this is what happens in MATLAB:
>> p1 = [1 -3 1]; >> p2 = [1 0]; >> p = p1 + p2
Instead, pad the vector p2 so that it has the same length as the vector p1:
>> p1 = [1 -3 1]; >> p2 = [0 1 0]; >> p = p1 + p2
p = 1 -2 1