adding 2 polynomials having lots of trouble

27 ビュー (過去 30 日間)
lowcalorie
lowcalorie 2012 年 3 月 4 日
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 件のコメント
lowcalorie
lowcalorie 2012 年 3 月 4 日
if you cant understand the question i can email you a picture of the question that might be more readable

サインインしてコメントする。

採用された回答

Wayne King
Wayne King 2012 年 3 月 4 日
p = [11,12,13,14];
s = [101,102];
s = [zeros(1,2) s];
h = p + s;
  4 件のコメント
lowcalorie
lowcalorie 2012 年 3 月 4 日
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 2012 年 3 月 4 日
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....

サインインしてコメントする。

その他の回答 (1 件)

Wayne King
Wayne King 2012 年 3 月 4 日
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 件のコメント
lowcalorie
lowcalorie 2012 年 3 月 4 日
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]

サインインしてコメントする。

カテゴリ

Help Center および File ExchangePolynomials についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by