0% found this document useful (0 votes)
12 views9 pages

DSP Lab Report 1

Uploaded by

madnir99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views9 pages

DSP Lab Report 1

Uploaded by

madnir99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Introduction to MATLAB

2 / 23 / 24

00
EE-20-A

Muhammad Raza Madni 210401034


Riyan Abdullah 210401013
Lab # 00
Objectives:
By the end of this lab students will be familiarize with MATLAB environment through some
preliminary MATLAB functions.

Equipment’s:
• Laptop
• MATLAB (software)

Procedure:
Students are required to go through the steps explained below and then complete the exercises given
at the end of the lab.

Introduction to MATLAB
MATLAB is a high-level programming language that has been used extensively to solve complex
engineering problems.
Task # 1
Operate with the vectors.
V1 = [1 2 3 4 5 6 7 8 9 0], V2 = [0.3 1.2 0.5 2.1 0.1 0.4 3.6 4.2 1.7 0.9], V3 = [4 4 4 4 3 3 2 2 2 1]
• Calculate, respectively, the sum of all the elements in vectors V1, V2, and V3
• How to get the value of the fifth element of each vector?
• What happens if we execute the command V1(0) and V1(11)?
• Remember if a vector has N elements, their subscripts are from 1 to N.
• Generate a new vector V4 from V2, which is composed of the first five elements of V2.
• Generate a new vector V5 from V2, which is composed of the last five elements of V2.
• Derive a new vector V6 from V2, with its 6th element omitted.
• Derive a new vector V7 from V2, with its 7th element changed to 1.4.
• Derive a new vector V8 from V2, whose elements are the 1st, 3rd, 5th, 7th, and 9th elements of V2
• What are the results of
❖ 9-V1
❖ V1*5
❖ V1+V2
❖ V1-V3
❖ V1.*V2
❖ V1*V2
❖ V1.^2
❖ V1.^V3
❖ V1^V3
❖ V1 == V3
❖ V1>6
❖ V1>V3
❖ V3-(V1>2)
❖ (V1>2) & (V1<6)
❖ (V1>2) | (V1<6)
❖ any(V1)
❖ all(V1)
Answers:
Following is the MATLAB code as well as answers along them;
V1 = [1 2 3 4 5 6 7 8 9 0];
V2 = [0.3 1.2 0.5 2.1 0.1 0.4 3.6 4.2 1.7 0.9];
V3 = [4 4 4 4 3 3 2 2 2 1];

Part a
sum(V1)
ans = 45
sum(V2)
ans = 15.0000
sum(V3)
ans = 29

Part b
V1(5)
ans = 5
V2(5)
ans = 0.1000
V3(5)
ans = 3

Part c
V4 = V2(1:5)
V4 = 1×5
0.3000 1.2000 0.5000 2.1000 0.1000

Part d
V5 = V2(6:10)
V5 = 1×5
0.4000 3.6000 4.2000 1.7000 0.9000

Part e
V6 = [V2(1:5) V2(7:10)]
V6 = 1×9
0.3000 1.2000 0.5000 2.1000 0.1000 3.6000 4.2000 1.7000 0.9000

Part f
V7 = V2;
V7(7) = 1.4
V7 = 1×10
0.3000 1.2000 0.5000 2.1000 0.1000 0.4000 1.4000 4.2000 1.7000 0.9000

Part G
V8 = [V2(1) V2(3) V2(5) V2(7) V2(9)]
V8 = 1×5
0.3000 0.5000 0.1000 3.6000 1.7000

Part h
R1 = 9 - V1
R1 = 1×10
8 7 6 5 4 3 2 1 0 9
R2 = V1*5
R2 = 1×10
5 10 15 20 25 30 35 40 45 0
R3 = V1+V2
R3 = 1×10
1.3000 3.2000 3.5000 6.1000 5.1000 6.4000 10.6000 12.2000 10.7000 0.9000
R4 = V1-V3
R4 = 1×10
-3 -2 -1 0 2 3 5 6 7 -1
R5 = V1.*V2
R5 = 1×10
0.3000 2.4000 1.5000 8.4000 0.5000 2.4000 25.2000 33.6000 15.3000 0
R6 = V1*V2
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix
matches the
number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES
(.*) for
elementwise multiplication.

Related documentation

R7 = V1.^2
R7 = 1×10
1 4 9 16 25 36 49 64 81 0
R8 = V1.^V3
R8 = 1×10
1 16 81 256 125 216 49 64 81 0
R9 = V1^V3
Error using ^
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a
scalar.
To operate on each element of the matrix individually, use POWER (.^) for elementwise power.

R10 = V1 == V3
R10 = 1×10 logical array
0 0 0 1 0 0 0 0 0 0
R11 = V1>6
R11 = 1×10 logical array
0 0 0 0 0 0 1 1 1 0
R12 = V1>V3
R12 = 1×10 logical array
0 0 0 0 1 1 1 1 1 0
R13 = V3-(V1>2)
R13 = 1×10
4 4 3 3 2 2 1 1 1 1
R14 = (V1>2) & (V1<6)
R14 = 1×10 logical array
0 0 1 1 1 0 0 0 0 0
R15 = (V1>2) | (V1<6)
R15 = 1×10 logical array
1 1 1 1 1 1 1 1 1 1
R16 = any(V1)
R16 = logical
1
R17 = all(V1)
R17 = logical
0

Errors:
The only error that occurred in the code is as follows:
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix
matches the
number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES
(.*) for
elementwise multiplication.

Related documentation

This error occurred due to the product of two matrixes with unequal rows and columns. This can be resolved
as follows:
%R6 = V1*V2'
%R9 = V1^V3'

Task # 2
Compare a script and a function.
Procedure:
❖ Write a script: In the main menu of MATLAB, select file -> new -> M-file
▪ A new window will pop up. Input the following commands:
 x = 1:5;
 y = 6:10;
 g = x+y;
▪ and then save the file as myscript.m under the default path MATLAB/work
❖ Write a function: Create a new m file following the procedure of above.
▪ Type in the commands:
 function g = myfunction(x,y)
 g = x + y;
▪ and then save it as myfunction.m

❖ Compare their usage.


❖ run the commands one by one:
▪ myscript
▪ x
▪ y
▪ g
▪ z = myscript (error?)
❖ Run command clear all to remove all variables from memory
❖ Run the commands one by one:
▪ x = 1:5;
▪ y = 6:10;
▪ myfunction (error?)
▪ z = myfunction(x,y)
▪ g (error?)
▪ a = 1:10;
▪ b = 2:11;
▪ myfunction(a,b)

Answers:
Following is the MATLAB code as well as answers along them.
Code in .mlx file named myfunction
function g = myfunction(x,y)
g = x + y;

Code in .mlx file named myscript


x = 1:5;
y = 6:10;
g = x+y;

Following are the screenshots of the answers as well as the step that are supposed to be run one by one.
Errors:
myfunction (error?)

Invalid use of operator.
g (error?)

Invalid use of operator.
These are some the two errors that occurred in this code, these errors shows that there is error in parameters
of the function and variable that is assigned.

…the end !

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy