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

Cpe 102 Presentation 2

Uploaded by

blessedonoriode5
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 views18 pages

Cpe 102 Presentation 2

Uploaded by

blessedonoriode5
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/ 18

CPE 102 LECTURE

9TH MAY, 2024


OUTLINES

Basic Plotting

Matrix Generations

Array operations

Linear equations
BASIC PLOTTING

Line plot: To create a line plot, you can use the plot function. The
simplest way is to provide the x and y coordinates of the points you
want to plot. e.g:

x = 1:10; % x-coordinates
y = [3, 5, 2, 7, 4, 9, 2, 6, 8, 1]; % y-coordinates
plot(x, y)

Scatter plot: To create a scatter plot, you can use the scatter
function. It allows you to plot individual data points without
connecting them with lines. e.g:

<date/time> <footer> 3
BASIC PLOTTING(CONT’D)
x = 1:10; % x-coordinates
y = [3, 5, 2, 7, 4, 9, 2, 6, 8, 1]; % y-coordinates
scatter(x, y)

Bar plot:To create a bar plot, you can use the bar or barh function.
bar function is used for vertical bar plots, and the barh function is
used for horizontal bar plots. e.g:
x = 1:5; % x-coordinates
y = [3, 5, 2, 7, 4]; % y-coordinates
bar(x, y)

<date/time> <footer> 4
BASIC PLOTTING(CONT’D)

Histogram: To create a histogram, you can use the
histogram function. It provides a graphical representation of
the distribution of a dataset. e.g:
data = [2, 4, 2, 3, 5, 7, 3, 4, 6, 4, 5, 3, 2]; % data
histogram(data) % matlab
hist(data) % GNU octave

Pie Chart: To create a pie chart, you can use the pie function. It
is useful for displaying data as proportions of a whole. e.g:

<date/time> <footer> 5
BASIC PLOTTING (CONT’D)

data = [30, 20, 15, 35]; % data


labels = {'A', 'B', 'C', 'D'}; % corresponding labels
pie(data, labels)

<date/time> <footer> 6
MATRIX GENERATION

Zeros: The zeros function creates a matrix filled with zeros. e.g: A = zeros(3,
4); % Creates a 3x4 matrix filled with zeros

Ones: The ones function creates a matrix filled with ones. e.g:B = ones(2, 2);
% Creates a 2x2 matrix filled with ones

Identity: The eye function generates an identity matrix.

e.g: I = eye(3); % Creates a 3x3 identity matrix

Random Numbers: MATLAB provides various functions to generate matrices
with random numbers such “rand”, “randi”, and “randn”. e.g:

<date/time> <footer> 7
MATRIX GENERATION (cont’d)
C = rand(2, 3); % Creates a 2x3 matrix with random numbers
between 0 and 1
D = randn(4, 4); % Creates a 4x4 matrix with random numbers
from a standard normal distribution
E = randi([1, 10], 3, 3); % Creates a 3x3 matrix with random
integers between 1 and 10

linspace: The linspace function generates a vector or matrix
with equally spaced values between a specified range.e.g:

<date/time> <footer> 8
MATRIX GENERATION (cont’d)
x = linspace(0, 10, 5); % Creates a row vector with 5 equally spaced
values from 0 to 10

Meshgrid: The meshgrid function generates coordinate matrices
from two vectors, typically used for creating 2D grids. e.g:
x = 1:5;
y = 10:20;
[X, Y] = meshgrid(x, y); % Creates two 2D matrices X and Y
representing a grid of coordinates

<date/time> <footer> 9
ARRAY OPERATION

Element-wise Operations: MATLAB supports element-wise
operations, where corresponding elements of two arrays are
combined or manipulated independently. e.g:

A = [1, 2, 3];
B = [4, 5, 6];
C = A + B; % Element-wise addition: C = [5, 7, 9]
D = A .* B; % Element-wise multiplication: D = [4, 10, 18]
E = (A > B); % Element-wise logical operation: E = [0, 0, 0]

<date/time> <footer> 10
ARRAY OPERATION (CONT’D)

Transpose: The transpose operation flips the rows and columns
of an array. In MATLAB, you can use the ' (single quote)
operator or the transpose function to transpose an array. e.g:
A = [1, 2, 3; 4, 5, 6];
B = A'; % Transpose using the single quote operator
OR
C = transpose(A); % Transpose using the transpose function

<date/time> <footer> 11
ARRAY OPERATION (CONT’D)

Reshaping : MATLAB provides functions for reshaping and
concatenating arrays. The reshape function allows you to change the
size or shape of an array while maintaining the order of elements. e.g:
A = [1, 2; 3, 4];
B = reshape(A, 4, 1); % Reshape A into a column vector

Indexing and Slicing: MATLAB allows you to access specific elements,
rows, or columns of an array using indexing and slicing. Indexing starts
at 1 in MATLAB. E.g:

<date/time> <footer> 12
ARRAY OPERATION (CONT’D)
A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
a = A(2, 3); % Access a single element: a = 6
b = A(:, 2); % Access the second column: b = [2; 5; 8]
c = A(1:2, 1:2); % Access a subset of the array: c = [1, 2; 4, 5]

Broadcasting: MATLAB supports broadcasting, where a scalar value can be
combined with an array, and the operation is applied element-wise. e.g:
A = [1, 2, 3; 4, 5, 6];
B = A + 2; % Add 2 to each element of A
C = A * 3; % Multiply each element of A by 3

<date/time> <footer> 13
ARRAY OPERATION (CONT’D)

Reduction Operations: MATLAB provides functions for performing
reduction operations on arrays, such as sum, mean, max, min, std, var,
etc. These functions calculate the sum, mean, maximum, minimum, standard
deviation, variance, etc., of the elements along specific dimensions. e.g:
A = [1, 2, 3; 4, 5, 6];
total = sum(A, 'all'); % Calculate the sum of all elements: total = 21
row_sums = sum(A, 2); % Calculate the sum along each row
col_means = mean(A, 1); % Calculate the mean along each column

<date/time> <footer> 14
LINEAR EQUATION

Matrix Left Division (\): MATLAB's matrix left division operator
(\) can be used to solve a system of linear equations of the form A*x
= b, where A is the coefficient matrix, x is the unknown variable
vector, and b is the right-hand side vector. E.G:
A = [2, 3; 4, 5];
b = [6; 9];
x = A \ b;

<date/time> <footer> 15
LINEAR EQUATION

Matrix Right Division (/): MATLAB's matrix right division operator
(/) can also be used to solve linear equations, but it solves the
equation in a least-squares sense if the system is overdetermined or
inconsistent. e.g:
A = [2, 3; 4, 5; 1, 1];
b = [6; 9; 2];
x = A / b;

<date/time> <footer> 16
LINEAR EQUATION

Gaussian Elimination: MATLAB provides the lu function to perform LU
decomposition, which can be used for Gaussian elimination. The LU decomposition
separates a matrix A into a lower triangular matrix L and an upper triangular matrix U.
The lu function returns the factors L and U, which can be used to solve linear equations.
e.g:
A = [2, 3; 4, 5];
b = [6; 9];
[L, U] = lu(A);
y = L \ b;
x = U \ y;

<date/time> <footer> 17
LINEAR EQUATION

Gauss-Jordan Elimination: MATLAB provides the rref function to perform
Gauss-Jordan elimination, which transforms a matrix into reduced row-echelon
form. The rref function returns the reduced row-echelon form of the matrix,
which can be used to solve linear equations.e.g:
A = [2, 3; 4, 5];
b = [6; 9];
Ab = [A, b];
rrefAb = rref(Ab);
x = rrefAb(:, end);

<date/time> <footer> 18

You might also like