Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
ahmed lamak
ahmed lamak am 5 Sep. 2016
Kommentiert: Stephen23 am 5 Sep. 2016
How would i write (x-1)(x-2)....(x-n)
for a given n in matlab

Antworten (2)

Image Analyst
Image Analyst am 5 Sep. 2016
Try this:
result = 1
for k = 1 : n
result = result * (x - k);
end
  2 Kommentare
ahmed lamak
ahmed lamak am 5 Sep. 2016
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 am 5 Sep. 2016
You can do this:
result = 1
for k = 1 : length(z)
result = result * (x - z(k));
end

Melden Sie sich an, um zu kommentieren.


Stephen23
Stephen23 am 5 Sep. 2016
Bearbeitet: Stephen23 am 5 Sep. 2016
The simplest solution, without any loops:
prod(x-z)
  2 Kommentare
Walter Roberson
Walter Roberson am 5 Sep. 2016
I do not understand why you are raising to the z'th power ??
Stephen23
Stephen23 am 5 Sep. 2016
@Walter Roberson: experimenting around, and not paying enough attention to the copy-and-paste :(

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by