Getting Started with Matlab: Summing 2n from 0 to n

  • Context: MATLAB 
  • Thread starter Thread starter phillyj
  • Start date Start date
  • Tags Tags
    Matlab
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 4K views
phillyj
Messages
30
Reaction score
0
Hi, I'm new to matlab.

I'm trying to sum a function similar to 2n from 0 to n where n is specified as a digit and not a list. I am using the sum() function but I don't really know how to get it to work right for this.

Thanks
 
Physics news on Phys.org
Code:
S=0;
n = 10;
for k=0:n;
S = S + 2^k;
end

Or
Code:
ind = 0:1:n;
S = sum(2.^ind);
 
Thanks but I got it. I needed a one-line code like this:

Code:
n = 25
sum(2.^(0:n))