필터 지우기
필터 지우기

Summation with looping

조회 수: 1 (최근 30일)
Daniel
Daniel 2011년 12월 5일
댓글: OLUBUKOLA ogunsola 2016년 6월 5일
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
xs
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
Paulo Silva 2011년 12월 5일
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
for n=0:16
xl(n+1) = ((x^n)/factorial(n)); %save every iteration result
end
xs=sum(xl) %calculate the sum of the iteration results
it can also be done without loops
n=0:16;
xs = sum(((x.^n)./factorial(n))); %notice the dots
  댓글 수: 6
Daniel
Daniel 2011년 12월 6일
Thank you Paulo! I will give it a try and see what happens. No worries Sean, you were a great help on the last question. :)
OLUBUKOLA ogunsola
OLUBUKOLA ogunsola 2016년 6월 5일
i hope someone will still answer this after a long time. how can i achieve this without the built in function factorial, also ill like x to vary from say , 1 to 1000?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2011년 12월 6일
I think the problem is in your filename:
"imageanalystbs.m"
That sounds like a horrible name to me.
ImageAnalyst
  댓글 수: 2
Daniel
Daniel 2011년 12월 6일
Sorry, I don't see a problem?
Salah Saad
Salah Saad 2015년 5월 5일
편집: Salah Saad 2015년 5월 5일
he's being sarcastic...ish, it still is a weird filename

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by