Experiment No. 01 Experiment Name: Objectives:: 2.platform Independence. 3
Experiment No. 01 Experiment Name: Objectives:: 2.platform Independence. 3
Experiment No. 01 Experiment Name: Objectives:: 2.platform Independence. 3
01
Experiment Name: Introduction to MATLAB and Signals in MATLAB.
Objectives:
The objectives of this lab is-
1. To plot the 𝑦(𝑥) = 𝑥 2 𝑐𝑜𝑠𝑥, 𝑔(𝑥) = 𝑥𝑐𝑜𝑠𝑥, 𝑓(𝑥) = 2𝑥 𝑠𝑖𝑛𝑥 in the same figure.
1, −2≤𝑡 ≤2
2. To plot the following function, f(t)={ 0, 2≤𝑡≤5
𝑡𝑠𝑖𝑛(4𝜋𝑡) 5≤𝑡≤8
𝜋
3. To write a command to plot 𝑥[𝑛] = 𝑐𝑜𝑠 (0.5𝜋𝑛 + ) + sin(0.5𝜋𝑛).
3
4. To plot the discrete function f[n]= 𝑛2 , where -20≤n≤20.
5. To write a brief description on MATLAB commands, Basic terminology, Basic
arithmatic, Elementwise operators, logic operators, polynomial functions.
Theory:
MATLAB: MATLAB is a programming platform designed specifically for
engineers and scientists to analyze and design systems and products that transform
our world. The heart of MATLAB is the MATLAB language, a matrix-based
language allowing the most natural expression of computational mathematics.
Advantage of MATLAB
There are several advantages of MATLAB programming language:
1.Ease of Use.
2.Platform Independence.
3. Predefined Functions.
4. Device-Independent Plotting.
5. Graphical User Interface.
Page 1 of 10
Workspace:
1.The workspace contains all variables we create while working in MATLAB.
2.Whenever we assign a value to a variable, it automatically gets space in the
workspace.
3.Workspace variables lose their existence after the closing of the environment, so
save these variables in a file to use later on.
4.We can import variables into MATLAB from data files.
5.We can import variables into MATLAB from other programs also.
6.The assignment operator (=) facilitates the creation of variables.
7.To access the variable from the workspace, we need to enter its name at the
command line.
8.To view all available variables in the workspace, enter the command- 'whos' at
the command line.
Command Window: Once you have opened your m-file in the Editor Window, you
can now call and execute that m-file while in the Command Window. Directions for
using specific m-files are included in the m-file section of this document.
MATLAB provides various commands for managing a session. The following
table provides all such commands −
Command Purpose
Page 2 of 10
lookfor Searches help entries for a keyword.
Editor Window: The MATLAB Editor Window is a simple text editor where you
can load, edit and save complete MATLAB programs. The Editor window also has
a menu command (Debug/Run) which allows you to submit the program to the
command window.
Arithmetic Operators:
MATLAB allows two different types of arithmetic operations −
1.Matrix arithmetic operations
2.Array arithmetic operations
Matrix arithmetic operations are same as defined in linear algebra. Array operations
are executed element by element, both on one-dimensional and multidimensional
array.
The matrix operators and array operators are differentiated by the period (.) symbol.
However, as the addition and subtraction operation is same for matrices and arrays,
the operator is same for both cases. The following table gives brief description of
the operators –
Show Examples
1. Addition or unary plus ( + ) : A+B adds the values stored in variables A and B.
A and B must have the same size, unless one is a scalar. A scalar can be added
to a matrix of any size.
2. Subtraction or unary minus ( - ) : A-B subtracts the value of B from A. A and
B must have the same size, unless one is a scalar. A scalar can be subtracted
from a matrix of any size.
Page 3 of 10
3. Matrix multiplication( * ) : C = A*B is the linear algebraic product of the
matrices A and B.
4. Array multiplicationn ( .* ) : A.*B is the element-by-element product of the
arrays A and B. A and B must have the same size, unless one of them is a scalar.
5. Slash or matrix right division ( / ) : Slash or matrix right division. B/A is roughly
the same as B*inv(A). More precisely, B/A = (A'\B')'.
6. Array right division ( \ ) : A./B is the matrix with elements A(i,j)/B(i,j). A and
B must have the same size, unless one of them is a scalar.
7. Array left division ( ./ ) : A.\B is the matrix with elements B(i,j)/A(i,j). A and
B must have the same size, unless one of them is a scalar.
8. Matrix power( ^ ) : X^p is X to the power p, if p is a scalar. If p is an integer,
the power is computed by repeated squaring. If the integer is negative, X is
inverted first. For other values of p, the calculation involves eigenvalues and
eigenvectors, such that if [V,D] = eig(X), then X^p = V*D.^p/V.
9. Array power (.^ ) : A.^B is the matrix with elements A(i,j) to the B(i,j) power.
A and B must have the same size, unless one of them is a scalar.
10. Matrix transpose ( ' ) A' is the linear algebraic transpose of A. For complex
matrices, this is the complex conjugate transpose.
11. Array transpose ( .' ) : A.' is the array transpose of A. For complex matrices,
this does not involve conjugation.
Page 4 of 10
Operation Description
If you select distinctive as the Icon shape, the block appearance indicates its
function. Simulink® software displays a distinctive shape for the selected operator,
conforming to the IEEE® Standard Graphic Symbols for Logic Functions.
To specify the number of input ports, use the number of input ports parameter. The
output type is specified using the output data type parameter. An output value is 1 if
TRUE and 0 if FALSE.
Polynomial functions: MATLAB function polyfit() is defined to fit a specific set of
data points to a polynomialquickly and easily computing polynomial with the least
squares for the given set of data. It generates the coefficients for the elements of the
polynomial, which are used for modeling a curve to fit to the given data.
Polyfit() function uses input vector (x) to form Vandermonde matrix (V ) having
n+1 columns and r = length(x) rows, which is nothing but results in a linear system.
Page 5 of 10
Syntax: p = polyfit(x,y,n)
p= polyfit(x,y,n) returns the coefficients for a polynomial p(x) of degree n that is a
best fit (in a least-squares sense) for the data in y. The coefficients in p are in
descending powers, and the length of p is n+1
p(x)=p1xn+p2xn−1+...+pnx+pn+1.
Lab Tasks:
Lab Tasks 01: Plot the 𝑦(𝑥) = 𝑥 2 𝑐𝑜𝑠𝑥, 𝑔(𝑥) = 𝑥𝑐𝑜𝑠𝑥, 𝑓(𝑥) = 2𝑥 𝑠𝑖𝑛𝑥 in the same
figure.
Editor Code: Output:
clc;
clear all;
close all;
x=0:1:20;
y=((x.^2).*cos(x));
subplot(3,1,1)
plot(x,y,'b')
title('y(x)=x^2cosx')
xlabel('x')
ylabel('y(x)')
x=0:1:20;
g=(x.*cos(x));
subplot(3,1,2)
plot(x,g,'r')
title('g(x)=xcosx')
xlabel('x')
ylabel('g(x)')
x=0:1:20;
f=2.^x;
subplot(3,1,3)
plot(x,f,'g')
title('f(x)=2^x')
xlabel('x')
ylabel('f(x)') Fig. 1.1: Output of lab task 1.
Page 6 of 10
1, −2≤𝑡 ≤2
Lab Tasks 02: Plot the following function, f(t)={ 0, 2≤𝑡≤5
𝑡𝑠𝑖𝑛(4𝜋𝑡) 5≤𝑡≤8
Editor Code:
clc;
clear all;
close all;
subplot(3,1,1)
plot([-2 2],[1 1],'r')
title('f(t)=1')
xlabel('t')
ylabel('f(t)')
subplot(3,1,2)
plot([2 5],[0 0],'g')
title('f(t)=0')
xlabel('t')
ylabel('f(t)')
t=5:0.1:8;
f=(t.*sin(4*pi*t))
subplot(3,1,3)
plot(t,f,'b')
title('f(t)=t*sin(4*pi*t)')
xlabel('t')
ylabel('f(t)')
Page 7 of 10
Output:
𝜋
Lab tasks 03: Write a command to plot 𝑥[𝑛] = 𝑐𝑜𝑠 (0.5𝜋𝑛 + ) + sin(0.5𝜋𝑛).
3
Editor Code:
clc;
clear all;
close all;
n=-20:1:20;
x=(cos((0.5*pi*n)+(pi/3))+sin(0.5*pi*n));
stem(n,x,'r');
title('x[n]=cos((0.5*pi*n)+(pi/3))+sin(0.5*pi*n)')
;
xlabel('n')
ylabel('x[n]')
Page 8 of 10
Output:
Lab Tasks 04: Plot the discrete function f[n]= 𝑛2 , where -20≤n≤20.
Editor Code:
clc;
clear all;
close all;
n=-20:1:20;
f=n.^2;
stem(n,f,'r');
title('f[n]=n^2');
xlabel('n')
ylabel('f[n]')
Page 9 of 10
Output:
Page 10 of 10