Summation with looping
86 views (last 30 days)
Find ∑((x^)n/n!) for x=1.8 and n=0 to 16.
Okay, my first attempt to do this looked like:
x = 1.8 For(0:16) xs = sum((x^n)/factorial(n)) end
But I received the error: ??? Error: File: imageanalystbs.m Line: 11 Column: 6 Unexpected MATLAB expression.
My line 11 is the one with "For (0:16)"
What am I doing wrong here? Any help is appreciated.
Paulo Silva on 5 Dec 2011
for n=0:16 is the correct syntax, you are also making another error by doing the summation in every iteration, you should instead find all values and do the summation after the loop ends
x = 1.8
xl=zeros(17,1); %preallocation of the storage
xl(n+1) = ((x^n)/factorial(n)); %save every iteration result
xs=sum(xl) %calculate the sum of the iteration results
it can also be done without loops
xs = sum(((x.^n)./factorial(n))); %notice the dots