Professional Documents
Culture Documents
Experiment No.3: Tool Used
Experiment No.3: Tool Used
(B)Compute the running sum (check with sum) , where running sum of element
j= the sum of the elements from 1 to j, inclusive.
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
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 matrix, then sum(A) returns a row vector containing the sum of each
column.
2. To add the elements along row or column , use the sum operator sum().
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
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=
>> b=randn(2,3)
b=
>> 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;
>> plot(d);
>> a=1;
>> plot(d);
>> plot(t,d);
>> hold
>> xlabel('time')
>> ylabel('magnitude')