How do I display the terms of a binomial expansion (x+y)^n ?

7 visualizzazioni (ultimi 30 giorni)
I want to display the terms in terms of x and y i.e nCr x^(n-r) y^r with r going from 0 to n

Risposta accettata

Matt J
Matt J il 19 Giu 2019
Modificato: Matt J il 19 Giu 2019
One way,
n=4;
e=0:n;
C=arrayfun(@(i)nchoosek(n,i),e);
sprintf('%d*x^%d*y^%d ',[C;e;n-e])
'1*x^0*y^4 4*x^1*y^3 6*x^2*y^2 4*x^3*y^1 1*x^4*y^0 '
  3 Commenti
Anomitra Saha
Anomitra Saha il 19 Giu 2019
Can you tell me how to do this using for loop ?
Matt J
Matt J il 19 Giu 2019
Just call sprintf() differently
sprintf('%d*x^%d*y^%d ',[C;e;n-e])
'1*x^0*y^4 4*x^1*y^3 6*x^2*y^2 4*x^3*y^1 1*x^4*y^0 '

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Prodotti


Release

R2013a

Community Treasure Hunt

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

Start Hunting!

Translated by