필터 지우기
필터 지우기

Find the sum of the first n

조회 수: 206 (최근 30일)
bassam792
bassam792 2017년 2월 15일
댓글: Steven Lord 2024년 2월 14일
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+.....
  댓글 수: 1
Yogesh
Yogesh 2024년 2월 14일
Consider the following divergent infinite series:
S=1/2+2/3+3/4+⋯+n/n+1
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

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

답변 (5개)

John Chilleri
John Chilleri 2017년 2월 15일
편집: John Chilleri 2017년 2월 15일
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!
  댓글 수: 7
Walter Roberson
Walter Roberson 2018년 9월 8일
The theoretical sum would be the same. If you are using floating point then the result could differ. The order of operations of built-in functions like harmonic() is not specified. For doing it yourself, replace 1:n by n:-1:1
Walter Roberson
Walter Roberson 2021년 6월 4일
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
Roger Stafford 2017년 2월 15일
Or how about this one-liner:
H = det(diag(2:n)+ones(n-1))/factorial(n);
  댓글 수: 2
Ahmad Taheri
Ahmad Taheri 2020년 12월 26일
hi ,admin
one qustions
∑1/n=?
∑1/n^2=??
Walter Roberson
Walter Roberson 2020년 12월 26일
∑1/n is infinite.
∑1/n^2 is pi^2/6

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


Walter Roberson
Walter Roberson 2018년 9월 8일
syms x n
symsum(1/x, x, 1, n)
  댓글 수: 1
Hesbon Osoro
Hesbon Osoro 2021년 6월 3일
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
Vaibhav 2023년 2월 15일
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
  댓글 수: 1
Walter Roberson
Walter Roberson 2023년 2월 15일
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

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


Yogesh
Yogesh 2024년 2월 14일
Consider the following divergent infinite series:
S=1/2+2/3+3/4+⋯+n/n+1
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
  댓글 수: 1
Steven Lord
Steven Lord 2024년 2월 14일
This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.

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

Community Treasure Hunt

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

Start Hunting!

Translated by