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

17 visualizzazioni (ultimi 30 giorni)

Risposta accettata

KSSV
KSSV il 24 Nov 2020
Modificato: KSSV il 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 Commenti
Stephan
Stephan il 24 Nov 2020
Modificato: Stephan il 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.

Accedi per commentare.

Più risposte (1)

Stephan
Stephan il 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

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by