adding 2 polynomials having lots of trouble

y(x) = p_1*(x^n)+p_2*(x^(n-1))+...+p_n*x+p_n+1
z(x) = s_1*(x^m)+s_2*(x^(m-1))+...+s_m*x+s_m+1
i need to add the coefficients of the terms with the same power when m=n, m<n, and m>n.
h(x) = y(x) + z(x)
The input to the script will be vectors:
p = [p_1,p_2...p_n,p_n+1]
s = [s_1,s_2...s_m,s_m+1]
an example if the input was:
p = [1,2,3,4] and s = [10,20,30,40]
I should get the answer [11,22,33,44]
please help i am very new to matlab and im really having trouble with this one

1 Comment

if you cant understand the question i can email you a picture of the question that might be more readable

Sign in to comment.

 Accepted Answer

p = [11,12,13,14];
s = [101,102];
s = [zeros(1,2) s];
h = p + s;

4 Comments

MY CODE NEEDS TO WORK FOR ANY SIZE VECTORS
Then you just need to figure out how many zeros to add so that your vectors are equal length and the powers of the polynomial are correctly aligned.
BUT I SHOULD NOT HAVE TO ADJUST s = [zeros(1,2) s]; EVERY TIME THE USER INPUTS A DIFFERENT SIZE VECTOR
why don't you require the user to input a vector in ascending powers of x? If your program only allows nonnegative powers of x, then the first element should always be the 0-th power. If that coefficient is zero, then the user should enter a zero for that power and for any other power that has a zero coefficient. Otherwise, the user needs to add some additional input for your program to tell which powers the coefficients go with. For example:
x = [ 2 3 4];
How would you know that 2 was the coefficient for x^3 3 was for x^7, etc.?
Make the user enter
x = [0 0 0 2 0 0 0 3....

Sign in to comment.

More Answers (1)

p = [1,2,3,4];
s = [10,20,30,40];
z = s + p;
should work just fine, so please show your code and what error you are getting.

1 Comment

i dont think my code would be that simple from what i have been working on im having trouble formatting the equations y(x) and z(x)
another example if i input:
p = [11,12,13,14] and s = [101,102] i should get h(x) = [11,12,114,116]

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!