Create a function that takes two polynomials as inputs and r
Create a function that takes two polynomials as inputs and returns a polynomial where the coefficients are the maximum of the comparison between each corresponding pair of coefficients. For example, if the inputs are 3x^4 + 1 and 2x^4 + x^3, the output would be 3x^4 + x^3 + 1. Test your function using the following polynomials: p1 = - 23x^9 + x + 7.3x^7 + 3x + 9, p2 = 12x^9 + x + 2.3x^7 + 3x + 19 p1 = x^2 + x + 1, p1 = x^3 p1 = x^5, p2 = 2x^7 + 4x^4 + 5x^3 + x^2 + 5.68
Solution
Matlab code:
% p1 = [-23 0 7.3 0 0 0 0 0 4 9];
% p2 = [12 0 2.3 0 0 0 0 0 4 19];
% p1 = [1 0 0 0];
% p2 = [0 1 1 1];
p2 = [2 0 0 4 5 1 0 5.68];%taking first polynomial in vector form
p1 = [0 0 1 0 0 0 0 0];%taking second polynomial in vector form
[r1 c1]=size(p1);%taking size since size of both polynomials is same
newp = zeros;
for i=1:c1
newp(i)=max(p1(i),p2(i));%taking maximum of each power of x
end
disp(newp);
Ouput of each input respectively:
1output
Columns 1 through 4
12.0000 0 7.3000 0
Columns 5 through 8
0 0 0 0
Columns 9 through 10
4.0000 19.0000
2ouput
1 1 1 1
3output
Columns 1 through 4
2.0000 0 1.0000 4.0000
Columns 5 through 8
5.0000 1.0000 0 5.6800
