Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USE MATLAB TO COMPLETE THIS PROBLEM! The harmonic series, sigma^infinity_k = 1 1

ID: 2247608 • Letter: U

Question

USE MATLAB TO COMPLETE THIS PROBLEM!

The harmonic series, sigma^infinity_k = 1 1/k = 1 + 1/2 + 1/3 + 1/4 + is known to diverge to +infinity. The nth partial sum approaches +infinity at the same rate as log(n). (a) Euler's constant is defined to be gamma = lim_n rightarrow infinity [sigma^n_k = 1 (1/k) - log(n)] almostequalto 0.57721. Write and test a section in your script that calculates the first 5000 partial sums (that is calculate each partial sum for n = 1 to 5000) and estimates Euler's constant using Eq. (I). Write your routine so that each value is calculated, but values are only stored every 250 steps. (b) Euler's constant, gamma, can also be represented by gamma = lim_n rightarrow infinity [sigma^n_k = 1 (1/k) - log (n + 1/2)] almostequalto 0.57721. Write and test a section in your script that estimates Euler's constant when n = 5000 using Eq. (II). Print intermediate answers at every 250 steps. (c) Display your results in a table with columns n, values from part(a) and values from part(b). Label the columns and make the display easy to read. Based on your outputs, which of the two codes converges more rapidly?

Explanation / Answer

clc;
clear all;

n = 5000;
format long g
values = 0;
sum = 0;
j = 1;
const = 0;
term = 0;

%% Part a
totalValues1 = 0;
p = 1;

for k = 1:n
term = term + (1 / k);
sum = (term) - log(n);
const = sum;
totalValues1(p) = sum;
p = p+1;
if rem(k, 250) == 0
values(j) = sum;
j = j+1;
end
end
disp(['Euler''s Constant obtained in part a: ', num2str(const)])
for i = 1:numel(values)
fprintf('%.4f ', values(i));
end

%% Part b
totalValues2 = 0;
q = 1;
value2 = 0;
j = 1;
const2 = 0;
term = 0;
sum = 0;
j = 1;
for k = 1:n
term = term + (1 / k);
sum = (term) - log((n * 2) / 2);
const2 = sum;
totalVaues2(q) = sum;
q = q+1;
if rem(k, 250) == 0
values2(j) = sum;
j = j+1;
end
end
disp('*******************************');
disp(['Euler''s Constant obtained in part b: ', num2str(const2)])
for i = 1:numel(values2)
fprintf('%.4f ', values2(i));
end

disp('################################ Comparison #######################################')
fprintf('N Eulers Constants part a Eulers Constants part b ')
for ii = 1:numel(values)
fprintf('%d %.5f %.5f ', ii, values(ii), values2(ii));
end

CONSOLE OUTPUT

Euler's Constant obtained in part a: 0.57732

-2.4165

-1.7244

-1.3192

-1.0317

-0.8087

-0.6264

-0.4723

-0.3388

-0.2211

-0.1157

-0.0204

0.0666

0.1466

0.2207

0.2897

0.3542

0.4148

0.4720

0.5260

0.5773

*******************************

Euler's Constant obtained in part b: 0.57732

-2.4165

-1.7244

-1.3192

-1.0317

-0.8087

-0.6264

-0.4723

-0.3388

-0.2211

-0.1157

-0.0204

0.0666

0.1466

0.2207

0.2897

0.3542

0.4148

0.4720

0.5260

0.5773

################################ Comparison #######################################

N Eulers Constants part a Eulers Constants part b

1 -2.41652 -2.41652

2 -1.72437 -1.72437

3 -1.31924 -1.31924

4 -1.03172 -1.03172

5 -0.80868 -0.80868

6 -0.62642 -0.62642

7 -0.47232 -0.47232

8 -0.33883 -0.33883

9 -0.22107 -0.22107

10 -0.11573 -0.11573

11 -0.02044 -0.02044

12 0.06656 0.06656

13 0.14659 0.14659

14 0.22068 0.22068

15 0.28967 0.28967

16 0.35420 0.35420

17 0.41481 0.41481

18 0.47197 0.47197

19 0.52603 0.52603

20 0.57732 0.57732

>>

Matlab Code

clc;
clear all;

n = 5000;
format long g
values = 0;
sum = 0;
j = 1;
const = 0;
term = 0;

%% Part a
totalValues1 = 0;
p = 1;

for k = 1:n
term = term + (1 / k);
sum = (term) - log(n);
const = sum;
totalValues1(p) = sum;
p = p+1;
if rem(k, 250) == 0
values(j) = sum;
j = j+1;
end
end
disp(['Euler''s Constant obtained in part a: ', num2str(const)])
for i = 1:numel(values)
fprintf('%.4f ', values(i));
end

%% Part b
totalValues2 = 0;
q = 1;
value2 = 0;
j = 1;
const2 = 0;
term = 0;
sum = 0;
j = 1;
for k = 1:n
term = term + (1 / k);
sum = (term) - log((n * 2) / 2);
const2 = sum;
totalVaues2(q) = sum;
q = q+1;
if rem(k, 250) == 0
values2(j) = sum;
j = j+1;
end
end
disp('*******************************');
disp(['Euler''s Constant obtained in part b: ', num2str(const2)])
for i = 1:numel(values2)
fprintf('%.4f ', values2(i));
end

disp('################################ Comparison #######################################')
fprintf('N Eulers Constants part a Eulers Constants part b ')
for ii = 1:numel(values)
fprintf('%d %.5f %.5f ', ii, values(ii), values2(ii));
end