adding 2 polynomials having lots of trouble

조회 수: 28 (최근 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 CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by