You are on page 1of 7

MATLAB

Lecture 7
Example 1: Sum series

The result
Example 2: Sum series
Sum series 1/k2 while sum <= 1.6
1/k2 = 1 + 1/4 + 1/9 + 1/16 + 1/25 +…..
And determine how many terms are needed for this sum

The result
Example 3: The sum of Diagonal Elements

The result
:Question
Write a for/while loop to compute the sum
of the squares of all integers from 2 to 20
:Solution
using For using While
;sum=0 ;sum=0
for n=2:20 ;n=2
;sum=sum + n^2 while n<=20
end ;sum=sum+n^2
disp(sum) n=n+1
:Solution
end
disp(sum)
Exercise: Write the programs to compute the following
(1)

Where x = 1:10

(2) Where

You might also like