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

18 Ansichten (letzte 30 Tage)
JJD
JJD am 24 Nov. 2020
Kommentiert: JJD am 24 Nov. 2020

Akzeptierte Antwort

KSSV
KSSV am 24 Nov. 2020
Bearbeitet: KSSV am 24 Nov. 2020
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 Kommentare
Stephan
Stephan am 24 Nov. 2020
Bearbeitet: Stephan am 24 Nov. 2020
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 am 24 Nov. 2020
ohh, ok thank you

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Stephan
Stephan am 24 Nov. 2020
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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by