How to get the sum of the series: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n?

조회 수: 142 (최근 30일)
Rajdeep Mattu
Rajdeep Mattu 2020년 7월 7일
답변: Image Analyst 2020년 7월 7일
I understand the answer is going to be 2 because I've worked with series before, but this is the first time doing it on MATLAB.There is going to be a while loop since this is an infinite series that converges to 2.
Here's what I've got but I'm getting nowhere.
ksum = 1;
n = 0;
while ksum <= 2
n = n + 1;
ksum = ksum + 1/n;
end
Help, please!
  댓글 수: 4
KSSV
KSSV 2020년 7월 7일
Sum is not 2..how it is 2?
n = 10^5 ;
s = 0 ;
for i = 1:n
s = s+1/i ;
end
s
Rajdeep Mattu
Rajdeep Mattu 2020년 7월 7일
Oh shoot, yeah, you guys are right about that. I thought it converges to 2, but I must've confused it with a different series.
Well to learn MATLAB better, say I wanted to know which kth term would get the sum past some random number, lets say 3, how would someone write a while loop for that?

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

답변 (2개)

KSSV
KSSV 2020년 7월 7일
편집: KSSV 2020년 7월 7일
n = 10^5 ;
s = 0 ;
i = 0 ;
while s <= 3 % fix the sum here
i = i+1 ;
s = s+1/i ;
end
i

Image Analyst
Image Analyst 2020년 7월 7일
Try this:
n = 1000;
denominators = 1 : n;
theTerms = 1 ./ denominators;
seriesSum = sum(theTerms)

카테고리

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