simple coding, how to write (x-1)...(x-n)

1 回表示 (過去 30 日間)
ahmed lamak
ahmed lamak 2016 年 9 月 5 日
コメント済み: Stephen23 2016 年 9 月 5 日
How would i write (x-1)(x-2)....(x-n)
for a given n in matlab

回答 (2 件)

Image Analyst
Image Analyst 2016 年 9 月 5 日
Try this:
result = 1
for k = 1 : n
result = result * (x - k);
end
  2 件のコメント
ahmed lamak
ahmed lamak 2016 年 9 月 5 日
i gave a simple example as the zeros being 1 : n, if they were rather complicated values and denoted as say z(1),z(2),...,z(n) this method wont work?
Image Analyst
Image Analyst 2016 年 9 月 5 日
You can do this:
result = 1
for k = 1 : length(z)
result = result * (x - z(k));
end

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


Stephen23
Stephen23 2016 年 9 月 5 日
編集済み: Stephen23 2016 年 9 月 5 日
The simplest solution, without any loops:
prod(x-z)
  2 件のコメント
Walter Roberson
Walter Roberson 2016 年 9 月 5 日
I do not understand why you are raising to the z'th power ??
Stephen23
Stephen23 2016 年 9 月 5 日
@Walter Roberson: experimenting around, and not paying enough attention to the copy-and-paste :(

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

カテゴリ

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