Polynomial long division script writing

I'm trying to write a code for polynomial long division, for vectors P divided by Q to give remainder R and equation S.
clear all; clc
P = [1 -5 3 -15]
Q = [1 0 3]
lengthQ = length(Q);
i = (length(P)-length(Q))+1;
while i>0
% Making Q the same length as P
diff_length = length(P)-length(Q);
QlengthP = [Q (zeros(1, diff_length))];
% Take the first number from Q and divide the first number from P by this.
Xone = P(1,1)/Q(1,1);
% Multiplying the Q by the divised
for_sub_or_add = Xone*QlengthP
% Deciding whether to subtract or divide
if sign(P(1,1)) == sign(for_sub_or_add(1,1))
P = P - for_sub_or_add
elseif sign(P(1,1)) ~= sign(for_sub_or_add(1,1))
P = P + for_sub_or_add
end
P = P(find(P ~= 0))
Q = for_sub_or_add(find(for_sub_or_add ~= 0))
i = i-1
end
But it just won't work! I know there are short cuts of doing this but i'm not allowed to use those!
Please help, im at my wits end now!
thanks

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangePolynomials についてさらに検索

質問済み:

2013 年 4 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by