-1

I would like to know if there is Matlab code that can solve the multinomial formula. I can write a code for finite number of terms in the multinomial formula, for example, (x_{1}+x_{2}+x_{3})^4. But for general case, I found it is not easy, i.e. (x_{1}+x_{2}+ .... +x_{m})^n

enter image description here

sky-light
  • 111
  • 6
  • What do you want to solve it for? Do you just want to compute the [multinomial coefficients](http://en.wikipedia.org/wiki/Multinomial_theorem#Multinomial_coefficients)? – knedlsepp Jan 04 '15 at 22:39
  • It is not just the multinomial coefficient, I have a case when I need to solve and analyse each of the components inside the multinomial. So, I am looking for a code that can solve a general multinomial problem. for example, I may need to solve the integral that involve this multinomial (x1+x2+...+x6)^7 Therefore, I need to expand the multinomial so that I can solve it. – sky-light Jan 05 '15 at 14:34
  • I don't get it. What do you mean by solve? You can only solve an equation, not an expression! You can't *solve* `(x1 + x2 + ... + x6)^7`... – knedlsepp Jan 05 '15 at 14:36
  • Your question is very unclear! – knedlsepp Jan 05 '15 at 14:38
  • I think my question is very simple to understand; Can you write a matlab code that can solve the multinomial? – sky-light Jan 05 '15 at 14:39
  • Once again: There is no *solving the multinomial*. What would be your answer if I asked: *How do I solve `(x_1 + x_2)^2`?* – knedlsepp Jan 05 '15 at 14:41
  • for that case it is very simple, it is just a binomial and you have only one sum, in fact you can say x1, x2, ... xn are constants, they can take any real value. therefore the matlab code for your case can be as the following; x1=4; x2=8; K=2; sum=0; for i=0:K fact=factorial(K)/(factorial(i)*factorial(K-i)); sum=sum+(fact*x1^(i)*x2^(K-i)); end result=sum; check=(x1+x2)^K; – sky-light Jan 05 '15 at 14:51
  • Why don't you just use your `check` variable as `result = (x1 + x2)^K`? This will have the same result as your for-loop. There is no need in using the loop! – knedlsepp Jan 05 '15 at 14:55
  • In fact it is not always x1 and x2 known look at this syms x1 x2 K=2; sum=0; for i=0:K fact=factorial(K)/(factorial(i)*factorial(K-i)); sum=sum+(fact*x1^(i)*x2^(K-i)); end result=sum; – sky-light Jan 05 '15 at 14:56
  • I don't have the symbolic toolbox installed, so I can't use this. I still think with the symbolic toolbox you should be able to use: `(x1+x2)^2` Also your question doesn't state anything about symbolics! – knedlsepp Jan 05 '15 at 14:58
  • OK, Thanks. simply, can you write a Matlab code for general case? what I did is for 2 term only (i.e. binomial), can you write a code for multinomial? Yes or NO? – sky-light Jan 05 '15 at 19:05
  • I don't see a reason to do so, as `(x1 + x2 + ... + xm)^n` will yield the same result. Doesn't `m = 3; n=4; X = sym('x',m); symsum(X(i),1,m)^n` work? – knedlsepp Jan 05 '15 at 19:34
  • after this all discussion you don't want to say I can't write the code, instead you say I don't see a reason .... – sky-light Jan 05 '15 at 21:25
  • Because you still have not made clear what you actually want! Does my code above not work for you? – knedlsepp Jan 05 '15 at 21:31

1 Answers1

0

I interpret your question as: Using the symbolic toolbox, how do I expand the multinomial

(x_{1} + x_{2} + .... + x_{m})^n

to be able to see all its coefficients?

And my answer for you is:

m = 2; n = 3;
X = sym('x', [1,m]);
expand(sum(X)^n)

Which will yield:

>> x1^3 + 3*x1^2*x2 + 3*x1*x2^2 + x2^3
knedlsepp
  • 6,065
  • 3
  • 20
  • 41