Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

EXPERIMENT NO.

AIM: Generating a set of commands on a given vector to


(A)Add up the values of the elements(check with sum)

(B)Compute the running sum (check with sum) , where running sum of element
j= the sum of the elements from 1 to j, inclusive.

Also, generating a random sequence using rand() / randn() functions and


plotting them.

TOOL USED: MATLAB 7.0

THEORY:
1. RANDOM SEQUENCE

The rand( ) function generates random numbers between 0 and 1 that are
distributed uniformly (all numbers are equally probable). If you attempt the
extra credit, you likely will need to use the rand( ) function. rand(1) generates
a single random number rand(N) generates a NxN array of random numbers
rand(1,N) generates an array of N random numbers For example,

r = rand(1000,1);

The rand function returns double integer values drawn from a discrete uniform
distribution.

2. CUMSUM

B= cumsum(A) returns the cumulative sum of A starting at the beginning of the


first array dimension in A whose size does not equal 1.

If A is a vector, then cumsum(A) returns a vector containing the cumulative sum


of the elements of A.
If A is a matrix, then cumsum(A) returns a matrix containing the cumulative
sums for each column of A.

B = cumsum(A,dim) returns the cumulative sum of the elements along


dimension dim. For example, if A is a matrix, then cumsum(A,2) returns the
cumulative sum of each row.

3. SUM

S = sum(A) returns the sum of the elements of A along the first array dimension
whose size does not equal 1.

If A is a vector, then sum(A) returns the sum of the elements.

If A is a matrix, then sum(A) returns a row vector containing the sum of each
column.

If A is a multidimensional array, then sum(A) operates along the first array


dimension whose size does not equal 1, treating the elements as vectors. This
dimension becomes 1 while the sizes of all other dimensions remain the same.

S = sum(A,dim) returns the sum along dimension dim. For example, if A is a


matrix, then sum(A,2) is a column vector containing the sum of each row.
PROCEDURE:
1. Create Matrix A and B of 3x3 using the random operator rand().

2. To add the elements along row or column , use the sum operator sum().

3. To find cumulative sum of a Matrix , use the cumsum operator cumsum().

4. For the function of the operator refer to the theory.

5. Verify the result , with the theory given.

6. Use different Matrix for more verification and understanding or practice.


SUGGESTED EXPERIMENT
%magic

magic(4)

x=magic(4)

% rand

a=rand(2,3)

b=randn(2,3)

%sum

sum(x)

sum(x,1)

sum(x,2)

%cumsum

cumsum(x)

cumsum(x,2)

cumsum(x,1)

%function

d=a*sin(w*t);

a=200;

f=50;

w=2*pi*50;

t=0:0.01:1;
t=0:0.001:1;

%plot

plot(d);

plot(t,d);

hold

Current plot held

xlabel('time')

ylabel('magnitude')

title('sin function')

OUTPUT
>> magic(4)

ans =

16 2 3 13

5 11 10 8

9 7 6 12

4 14 15 1

>> a=rand(2,3)

a=

0.8147 0.1270 0.6324

0.9058 0.9134 0.0975

>> b=randn(2,3)

b=

-0.4336 3.5784 -1.3499

0.3426 2.7694 3.0349

>> x=magic(4)

x=

16 2 3 13

5 11 10 8

9 7 6 12

4 14 15 1

>> sum(x)

ans =

34 34 34 34

>> sum(x,1)
ans =

34 34 34 34

>> sum(x,2)

ans =

34

34

34

34

>> cumsum(x)

ans =

16 2 3 13

21 13 13 21

30 20 19 33

34 34 34 34

>> cumsum(x,1)

ans =

16 2 3 13

21 13 13 21

30 20 19 33

34 34 34 34

>> cumsum(x,2)

ans =

16 18 21 34
5 16 26 34

9 16 22 34

4 18 33 34

>> z=rand(1,100);

>> plot(z)

>> d=a*sin(w*t);

>> a=200;

>> f=50;

>> w=2*pi*50;

>> t=0:0.01:1;

>> t=0:0.001:1;

>> d=a*sin (w*t);

>> plot(d);

>> a=1;

>> d=a*sin (w*t);

>> plot(d);

>> plot(t,d);

>> hold

Current plot held

>> xlabel('time')

>> ylabel('magnitude')

>> title('sin function')

You might also like