Effacer les filtres
Effacer les filtres

adding 2 polynomials having lots of trouble

15 vues (au cours des 30 derniers jours)
lowcalorie
lowcalorie le 4 Mar 2012
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 commentaire
lowcalorie
lowcalorie le 4 Mar 2012
if you cant understand the question i can email you a picture of the question that might be more readable

Connectez-vous pour commenter.

Réponse acceptée

Wayne King
Wayne King le 4 Mar 2012
p = [11,12,13,14];
s = [101,102];
s = [zeros(1,2) s];
h = p + s;
  4 commentaires
lowcalorie
lowcalorie le 4 Mar 2012
BUT I SHOULD NOT HAVE TO ADJUST s = [zeros(1,2) s]; EVERY TIME THE USER INPUTS A DIFFERENT SIZE VECTOR
Wayne King
Wayne King le 4 Mar 2012
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....

Connectez-vous pour commenter.

Plus de réponses (1)

Wayne King
Wayne King le 4 Mar 2012
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 commentaire
lowcalorie
lowcalorie le 4 Mar 2012
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]

Connectez-vous pour commenter.

Catégories

En savoir plus sur Polynomials dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by