# Find the sum of the first n

301 views (last 30 days)
bassam792 on 15 Feb 2017
Commented: Walter Roberson on 15 Feb 2023
Find the sum of the first n terms of the harmonic series where n is an integer greater than one 1+1/2+1/3+1/4+1/5+.....

John Chilleri on 15 Feb 2017
Edited: John Chilleri on 15 Feb 2017
Hello,
This can be done simply with,
n = 100; % whatever you want
sum_harm = 0;
for i = 1:n
sum_harm = sum_harm + 1/i;
end
or even,
n = 100; % whatever you want
sum_harm = sum(1./(1:n));
Hope this helps!
Walter Roberson on 4 Jun 2021
format long g
n = 1e10; % whatever you want
sum_harm_forward = 0;
sum_harm_reverse = 0;
for i = 1:n
sum_harm_forward = sum_harm_forward + 1/i;
sum_harm_reverse = sum_harm_reverse + 1/(n - i + 1);
end
sum_harm_forward
sum_harm_forward =
23.6030665949975
sum_harm_reverse
sum_harm_reverse =
23.6030665948883
sum_harm_forward - sum_harm_reverse
ans =
1.092317347684e-10

Roger Stafford on 15 Feb 2017
H = det(diag(2:n)+ones(n-1))/factorial(n);
##### 2 CommentsShow 1 older commentHide 1 older comment
Walter Roberson on 26 Dec 2020
∑1/n is infinite.
∑1/n^2 is pi^2/6

Walter Roberson on 8 Sep 2018
syms x n
symsum(1/x, x, 1, n)
Hesbon Osoro on 3 Jun 2021
Very much useful to test the convergence of a harmonic series without lagging a machine, so fast code also. It is of great help.

Vaibhav on 15 Feb 2023
the examples in the course videos have been about convergent series. In this example, you will code a series that does not converge: S=1+12√+13√+⋯+1n√
Write a MATLAB code that takes a value of n and returns S. Please report the values below:
Please report the value of S for n=10
Please report the value of S for n=100
Please report the value of S for n=500
Walter Roberson on 15 Feb 2023
Is that the sum of 1 + 1*x^(1/2) + 1*x^(1/3) ... + 1*x^(1/n) ?
Is it the sum of 1 + 1/sqrt(2) + 1/sqrt(3) + ... + 1/sqrt(n) ?
format long g
syms n m
S = 1 + symsum(1/sqrt(factorial(n)), n, 2, m)
S = double(subs(S, m, [10 100 500]))
ans = 1×3
2.46928512326805 2.46950631452105 2.46950631452105