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

 采纳的回答

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 个评论

what does the "." for in the without loop script ?
It is elementwise multiplication, so the calculation works in a vectorized way. This makes the code more efficient and no for loop is needed.

请先登录,再进行评论。

更多回答(1 个)

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