How can I expand polynomials with matlab?

조회 수: 88 (최근 30일)
Sachi
Sachi 2015년 1월 14일
답변: Star Strider 2015년 1월 14일
Can matlab expand something like the following and represent it in terms of powers of 'x'?
x(x-7)(x-6) + (x+4)(x-9)(x-2) + x(x-8)(x+1) ----> k_1 x^3 + k_2 x^2 + k_3 x + k_4
  댓글 수: 1
Star Strider
Star Strider 2015년 1월 14일
It can if you have the Symbolic Math Toolbox.

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
John D'Errico 2015년 1월 14일
Trivial. With or without the symbolic toolbox. First, with...
syms x
P = expand(x*(x - 7)*(x - 6) + (x + 4)*(x - 9)*(x - 2) + x*(x - 8)*(x + 1))
P =
3*x^3 - 27*x^2 + 8*x + 72
Without the symbolic toobox, but with my sympoly toolbox , as found on the file exchange. Its free, but less capable than the symbolic one.
sympoly x
P = x*(x - 7)*(x - 6) + (x + 4)*(x - 9)*(x - 2) + x*(x - 8)*(x + 1)
P =
72 + 8*x - 27*x^2 + 3*x^3
In either case, it is easy enough to extract the numeric coefficients alone, if that is what you wanted.
And if you have neither toolbox and you like to do your own work (hey the sympoly toolbox is FREE after all)
coef = conv(conv([1 0],[1 -7]),[1 -6]) + ...
conv(conv([1 4],[1 -9]),[1 -2]) + ...
conv(conv([1 0],[1 -8]),[1 1])
coef =
3 -27 8 72

추가 답변 (1개)

Star Strider
Star Strider 2015년 1월 14일
An alternative using poly (assuming I got the maths correct) is:
% f(x) = x(x-7)(x-6) + (x+4)(x-9)(x-2) + x(x-8)(x+1)
rts1 = [ 0 7 6];
rts2 = [-4 9 2];
rts3 = [ 0 8 -1];
trm1 = poly(rts1);
trm2 = poly(rts2);
trm3 = poly(rts3);
ply = trm1 + trm2 + trm3;
produces:
ply =
3 -27 8 72

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by