In: Electrical Engineering
Write a MATLAB code to obtain the following. Keep your code commented whenever required. Copy your source code and command widow outcomes and screen shots of any plots in your solution.
Generate the following vectors and also implement the Table-1 operations-map by employing the switch statement and an appropriate loop.
DEC1 = |
5 1 3 0 2 5 0 2 |
Table-1 |
|
DEC(ind) |
Operation |
0 |
Res(ind)= x(ind) multiply by y(ind) |
1 |
Res(ind)= x(ind) divided by y(ind) |
2 |
Res(ind)= square root of [x(ind).y(ind)] |
3 |
Res(ind)= x(ind) subtract from y(ind) |
5 |
Res(ind)= x(ind) add to y(ind) |
* In Table-1, ind is presenting the vector index.
The Matlab Code is as follows:
DEC1=[5 1 3 0 2 5 0 2] %
Generates the vector DEC1 with the given values
DEC = [DEC1 DEC1 DEC1 DEC1 DEC1 DEC1 DEC1 DEC1]
%Concatenate the 'DEC1' 8 times to obtain 'DEC'
N = length(DEC) % Finding length of vector 'DEC'
t = 0:(1/N):1 %Generating vector t with 'N' equally
spaced values within the range of 0 to 1
x = round(26*sin(2*pi*9*t)) % round
off the result of 26sin(2pi9t)
y=x*2 % y = 'x'multiply by 2
%For loop for defining Table1 ind values 0 1 2 3 4 5
for ind=0:5
switch ind
% the index of vector is positive in matlab
case 0 % for ind = 0
Res(ind+1)=x(ind+1)*y(ind+1)
% first element
case 1 % for ind = 1
Res(ind+1)=x(ind+1)/y(ind+1)
% second element
case 2 % for ind = 2
Res(ind+1)=sqrt(x(ind+1)*y(ind+1)) %
third element
case 3 % for ind = 3
Res(ind+1)=y(ind+1)-x(ind+1)
% fourth
element
case 5 % for ind = 5
Res(ind+1)=x(ind+1)+y(ind+1)
% sixth element
end
end
Output:
DEC1 = 5 1 3 0 2 5 0 2 DEC = Columns 1 through 20: 5 1 3 0 2 5 0 2 5 1 3 0 2 5 0 2 5 1 3 0 Columns 21 through 40: 2 5 0 2 5 1 3 0 2 5 0 2 5 1 3 0 2 5 0 2 Columns 41 through 60: 5 1 3 0 2 5 0 2 5 1 3 0 2 5 0 2 5 1 3 0 Columns 61 through 64: 2 5 0 2 N = 64 t = Columns 1 through 8: 0 0.0156 0.0312 0.0469 0.0625 0.0781 0.0938 0.1094 Columns 9 through 16: 0.1250 0.1406 0.1562 0.1719 0.1875 0.2031 0.2188 0.2344 Columns 17 through 24: 0.2500 0.2656 0.2812 0.2969 0.3125 0.3281 0.3438 0.3594 Columns 25 through 32: 0.3750 0.3906 0.4062 0.4219 0.4375 0.4531 0.4688 0.4844 Columns 33 through 40: 0.5000 0.5156 0.5312 0.5469 0.5625 0.5781 0.5938 0.6094 Columns 41 through 48: 0.6250 0.6406 0.6562 0.6719 0.6875 0.7031 0.7188 0.7344 Columns 49 through 56: 0.7500 0.7656 0.7812 0.7969 0.8125 0.8281 0.8438 0.8594 Columns 57 through 64: 0.8750 0.8906 0.9062 0.9219 0.9375 0.9531 0.9688 0.9844 Column 65: 1.0000 x = Columns 1 through 16: 0 20 26 12 -10 -25 -22 -3 18 26 14 -8 -24 -23 -5 16 Columns 17 through 32: 26 16 -5 -23 -24 -8 14 26 18 -3 -22 -25 -10 12 26 20 Columns 33 through 48: 0 -20 -26 -12 10 25 22 3 -18 -26 -14 8 24 23 5 -16 Columns 49 through 64: -26 -16 5 23 24 8 -14 -26 -18 3 22 25 10 -12 -26 -20 Column 65: 0 y = Columns 1 through 16: 0 40 52 24 -20 -50 -44 -6 36 52 28 -16 -48 -46 -10 32 Columns 17 through 32: 52 32 -10 -46 -48 -16 28 52 36 -6 -44 -50 -20 24 52 40 Columns 33 through 48: 0 -40 -52 -24 20 50 44 6 -36 -52 -28 16 48 46 10 -32 Columns 49 through 64: -52 -32 10 46 48 16 -28 -52 -36 6 44 50 20 -24 -52 -40 Column 65: 0 Res = 0 Res = 0 0.5000 Res = 0 0.5000 36.7696 Res = 0 0.5000 36.7696 12.0000 Res = 0 0.5000 36.7696 12.0000 0 -75.0000