How do I properly use sum to sum up microstates?

11 views (last 30 days)
Hey, I'm trying to use a function to sum up the microstates in an evenly split two configuration system but can't get my code to work. Here's what I have:
function sumW = microstateSum(N)
n1 = N:0;
n2 = 0:N;
sumW = sum(factorial(N)/(factorial(n1)*factorial(n2)));
I'm getting an error on line 5, the last line, with * telling me that matrix dimension must agree. I just want the n1 and n2 variables to decrement and increment respectively but not interpret it as matrix multiplication. I was working with a for loop but couldn't get that to work either. New to Matlab.

Accepted Answer

Walter Roberson
Walter Roberson on 1 Dec 2013
N:0 is length 0.
N:-1:0 would be length N+1
After that change the / to ./ and change the * to .*
  1 Comment
Ryan
Ryan on 1 Dec 2013
Edited: Ryan on 1 Dec 2013
Thanks, got it. Proper code looks like:
function sumW = microstateSum(N)
n1 = N:-1:0;
n2 = 0:1:N;
sumW = sum(factorial(N)./(factorial(n1).*factorial(n2)));

Sign in to comment.

More Answers (0)

Categories

Find more on Just for fun in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!