Co Lab 01
Co Lab 01
Downloaded by ? A?SAN
lOMoAR cPSD| 24965850
Lab Report 01
Objective:
Introduction to MATLAB: its interface, dealing with variables, matrices & basic functions & their
applications.
Theory:
Introduction to MATLAB:
MATLAB is a high-level programming language and interactive environment widely used for
scientific computing and data analysis. Its name stands for "matrix laboratory," which reflects its
primary focus on matrix computations. In this introduction to MATLAB, we will cover its interface,
how to deal with variables, matrices, and basic functions, as well as their applications.
MATLAB Interface:
The MATLAB interface consists of several windows, including the Command Window, the
Workspace Browser, the Editor, and the Figure Window. The Command Window is where you can
enter MATLAB commands and see the results of those commands. The Workspace Browser displays
the variables currently in the workspace, and the Editor allows you to write and edit MATLAB code.
The Figure Window is where MATLAB plots and other graphical output are displayed.
Downloaded by ? A?SAN
lOMoAR cPSD| 24965850
Defining Variables:
In MATLAB, variables are used to store data. Variables can be defined using the assignment
operator "=" and can be any valid name that starts with a letter and consists of letters, numbers, and
underscores. For example:
x = 10
y = 2.5
name = 'John'
Matrices:
MATLAB is designed to work with matrices. Matrices are defined using square brackets "[ ]" and
elements are separated by spaces or commas. Rows are separated by semicolons ";" and columns are
separated by spaces or commas. For example:
A = [1 2 3; 4 5 6; 7 8 9]
B = [1, 2, 3; 4, 5, 6; 7, 8, 9]
Downloaded by ? A?SAN
lOMoAR cPSD| 24965850
MATLAB has several built-in functions for creating matrices, such as zeros, ones, and eye. You can
also use indexing to access individual elements of a matrix, rows, or columns. For example:
A (2,3) % returns the element in the second row, third column of matrix A
A (:2) % returns the second column of matrix A
MATLAB also has many functions for creating and manipulating plots, such as plot, scatter, and
histogram. For example:
x = Linspace(0,2*pi,100);
y = sin(x);
plot (x, y)
This code creates a plot of the sine function over the range [0, 2π] with 100 points. The "linspace"
function is used to create an evenly spaced vector of 100 points between 0 and 2π.
Downloaded by ? A?SAN
lOMoAR cPSD| 24965850
Task 01
Generate a vector A with elements ranging from 0 to 15 with spacing of 3. Generate a 0 to 15 with 6
elements vector B with elements ranging [0,15].
MATLAB code:
A=[0:3:15]
B=linspace(0,15,6)
Downloaded by ? A?SAN
lOMoAR cPSD| 24965850
Task 02
Add A and B and assign resulting vector to X. Square all the elements of vector X.
MATLAB code:
X= A+B Y=X.^2
Downloaded by ? A?SAN
lOMoAR cPSD| 24965850
Task 03
Find the solution to the following set of equations using the matrix inverse and left and right
division.
í + s + t + w = 10 2í - s + w = 4
3í + s - t - w = -2
í - 2s - 3t + w = -8
MATLAB code:
C= [1 1 1 1
2 -1 0 1
3 1 -1 -1
1 -2 -3 1] D=[10;4;-2;-8]
% matrix inverse V_1=inv(C)*D
% Or by left and right division. V_2=C\D
Downloaded by ? A?SAN
Task 04
Find the solution to the following set of equations using the matrix inverse and left and right division.
2x1 + x2 - 4x3 + 6x4 + 3x5 - 2x6 = 6
- x1 + 2x2 + 3x3 + 5x4 - 2x5 = 7 x1 - 2x2 - 5x3 + 3x4 + 2x5 + x6 = 0 4x1 + 3x2 - 2x3 + 2x4 + x6 = 8
3x1 + x2 - x3 + 4x4 + 3x5 + 6x6 = 16
MATLAB code:
E=[2 1 -4 6 3 -2
-1 2 3 5 -2 0
1 -2 -5 3 2 1
4 3 -2 2 0 1
3 1 -1 4 3 6
5 2 -2 3 1 1] F=[6;7;0;8;16;0]
% matrix inverse W_1=inv(E)*F
% Or by left and right division. W_2=E\F