how do i write a matlab script to sum this expression?

 채택된 답변

KSSV
KSSV 2020년 11월 24일
편집: KSSV 2020년 11월 24일
N = 100;
thesum = 0 ;
for i = 1:N
thesum = thesum+(1/i+1/((i+2)*(i+3))) ;
end
Without Loop:
N = 100 ;
f = @(i) (1./i+1./((i+2).*(i+3))) ;
i = 1:N ;
s = sum(f(i)) ;

댓글 수: 3

JJD
JJD 2020년 11월 24일
what does the "." for in the without loop script ?
Stephan
Stephan 2020년 11월 24일
편집: Stephan 2020년 11월 24일
It is elementwise multiplication, so the calculation works in a vectorized way. This makes the code more efficient and no for loop is needed.
JJD
JJD 2020년 11월 24일
ohh, ok thank you

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

추가 답변 (1개)

Stephan
Stephan 2020년 11월 24일
Symbolic:
syms n positive integer
N = 10000;
eq = 1/n + 1/((n+2)*(n+3));
pretty(eq)
sol = symsum(eq,n,1,N)
sol_num = double(sol)
results in:
>> Untitled
1 1
--------------- + -
(n + 2) (n + 3) n
sol =
eulergamma + psi(10001) + 10000/30009
sol_num =
10.1208

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

JJD
2020년 11월 24일

댓글:

JJD
2020년 11월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by