0% found this document useful (0 votes)
30 views60 pages

Matlab Note

The document provides a comprehensive overview of MATLAB, a high-level programming language designed for numerical computation and data analysis. It covers its history, features, advantages, and disadvantages, as well as applications in various fields such as numerical analysis, signal processing, and image processing. Additionally, it includes practical guidance on getting started with MATLAB, basic syntax, matrix operations, and writing MATLAB programs.
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)
30 views60 pages

Matlab Note

The document provides a comprehensive overview of MATLAB, a high-level programming language designed for numerical computation and data analysis. It covers its history, features, advantages, and disadvantages, as well as applications in various fields such as numerical analysis, signal processing, and image processing. Additionally, it includes practical guidance on getting started with MATLAB, basic syntax, matrix operations, and writing MATLAB programs.
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/ 60

MATLAB FOR ENGINEERING FOUNDATION

BY

ENGR (DR) DANIEL OSEZUA AIKHUELE

DEPARTMENT OF MECHANICAL ENGINEERING,

UNIVERSITY OF PORT HARCOURT


Introduction to MATLAB

What is MATLAB?

MATLAB (Matrix Laboratory) is a high-level programming language and environment


specifically designed for numerical computation, data analysis, and visualization. Its high-level
syntax, extensive library, and versatility make it an ideal tool for a wide range of applications.

History of MATLAB

MATLAB was first developed in the late 1970s by Cleve Moler, a mathematician and computer
scientist. The first version of MATLAB was released in 1984.

Features of MATLAB

1. High-Level Syntax: MATLAB has a simple, intuitive syntax that makes it easy to write
and execute code.

2. Matrix Operations: MATLAB is optimized for matrix operations, making it an ideal tool
for linear algebra, numerical analysis, and data analysis.

3. Built-in Functions: MATLAB has an extensive library of built-in functions for tasks such
as data analysis, visualization, and optimization.

4. Toolboxes: MATLAB has a wide range of toolboxes that provide specialized


functionality for tasks such as signal processing, image processing, and control systems.

5. Graphics and Visualization: MATLAB has a powerful graphics engine that allows users
to create high-quality 2D and 3D plots, charts, and graphs.

Advantages of MATLAB

1. Easy to Learn: MATLAB has a simple syntax and is relatively easy to learn, even for
users without prior programming experience.

2. High-Level Abstraction: MATLAB provides a high-level abstraction, allowing users to


focus on the problem at hand without worrying about low-level details.

3. Extensive Library: MATLAB has an extensive library of built-in functions and


toolboxes, making it a versatile tool for a wide range of applications.

Disadvantages of MATLAB

1. Cost: MATLAB can be expensive, especially for individual users or small organizations.

2. Steep Learning Curve for Advanced Topics: While MATLAB is easy to learn for basic tasks,
advanced topics such as object-oriented programming and parallel computing can have a steep
learning curve.
Applications of MATLAB

1. Numerical Analysis: MATLAB is widely used for numerical analysis, including linear algebra,
differential equations, and optimization.

2. Data Analysis: MATLAB is used for data analysis, including data visualization, statistical
analysis, and machine learning.

3. Signal Processing: MATLAB is used for signal processing, including filtering, convolution, and
Fourier analysis.

4. Image Processing: MATLAB is used for image processing, including image filtering,
segmentation, and recognition.

5. Control Systems: MATLAB is used for control systems, including modeling, simulation, and
control of dynamic systems.

Getting Started with MATLAB

1. Installing MATLAB: Download and install MATLAB from the MathWorks website.

2. Starting MATLAB: Launch MATLAB and familiarize yourself with the interface.

3. Basic Syntax: Learn the basic syntax of MATLAB, including variables, data types, and
operators.

4. Built-in Functions: Explore the built-in functions of MATLAB, including mathematical


functions, data analysis functions, and visualization functions.

MATLAB Syntax

The term syntax refers to the set of rules that define the structure and arrangement of elements in
a language or system, ensuring they are organized correctly to convey meaning. The concept of
syntax applies in various contexts, including:

Programming: Syntax refers to the rules and structure of a programming language. It defines
how commands, symbols, and statements must be written for the computer to understand
and execute them. MATLAB syntax is designed to work efficiently with matrices, and it includes
various constructs for programming, such as loops, conditional statements, and function
definitions.
Basic MATLAB Syntax Elements
Matrix operations

Matrix operations are a fundamental concept in linear algebra and are used extensively in various
fields such as physics, engineering, computer science, and data analysis.

What is a Matrix?

A matrix is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns.
It is a way to represent a system of equations or a transformation between vectors.

Types of Matrix Operations

There are several types of matrix operations, including:

1. Matrix Addition: The sum of two matrices is obtained by adding corresponding elements
of the two matrices.

2. Matrix Multiplication: The product of two matrices is obtained by multiplying the rows
of the first matrix with the columns of the second matrix.

3. Matrix Transpose: The transpose of a matrix is obtained by interchanging its rows and
columns.

4. Matrix Inverse: The inverse of a matrix is a matrix that, when multiplied by the original
matrix, produces the identity matrix.

5. Matrix Determinant: The determinant of a matrix is a scalar value that can be used to
determine the solvability of a system of linear equations.

Matrix Addition

Matrix addition is a simple operation that involves adding corresponding elements of two matrices.
For example, if we have two matrices A and B, the sum of the two matrices is obtained by adding
corresponding elements:
1 2
𝐴=[ ]
3 4
5 6
𝐵=[ ]
7 8
1+5 2+6 6 8
𝐴+𝐵 =[ ]=[ ]
3+7 4+8 10 12
Matrix Determinant

The determinant of a matrix is a scalar value that can be used to determine the solvability of a
system of linear equations. For example, if we have a matrix A, the determinant of A is obtained
by solving the equation det(A) = ad - bc, where a, b, c, and d are the elements of the matrix:
1 2
𝐴=[ ]
3 4
det(𝐴) = 𝑎𝑑 − 𝑏𝑐

det(𝐴) = 1 ∗ 4 − 3 ∗ 2 = −2

Matrix Multiplication

Matrix multiplication is a more complex operation that involves multiplying the rows of the first
matrix with the columns of the second matrix. For example, if we have two matrices A and B, the
product of the two matrices is obtained by multiplying the rows of A with the columns of B:
1 2
𝐴=[ ]
3 4
5 6
𝐵=[ ]
7 8
1∗5+2∗7 1∗6+2∗8 19 22
𝐴∗𝐵 =[ ]=[ ]
3∗5+4∗7 3∗6+4∗8 43 50
Matrix Transpose

The transpose of a matrix is obtained by interchanging its rows and columns. For example, if we
have a matrix A, the transpose of A is obtained by interchanging its rows and columns:
1 2
𝐴=[ ]
3 4
1 3
𝐴^𝑇 = [ ]
2 4

Matrix Inverse
The inverse of a matrix is a matrix that, when multiplied by the original matrix, produces the
identity matrix. For example, if we have a matrix A, the inverse of A is obtained by solving the
equation AX = I, where I is the identity matrix:
MATRICES AND ARRAYS

In MATLAB, matrices and arrays are fundamental data types, and MATLAB is specifically
designed to perform operations on them efficiently. A breakdown of how to work with matrices
and arrays in MATLAB are presented below;

1. CREATING MATRICES AND ARRAYS


1. Matrix Indexing

Matrix indexing in MATLAB refers to the methods used to access, modify, or extract elements,
rows, columns, or submatrices from a matrix. MATLAB provides a powerful and flexible way to
work with matrix indices.
what will be the answer if A(6) = 50; % Modify the 6th element in column-major order for the
above example
2. ARRAY OPERATIONS

In MATLAB, array operations allow you to perform various mathematical operations on arrays,
including element-wise operations, matrix operations, and linear algebra functions. Below is an
overview of common array operations and their usage in MATLAB.
1. Element-wise Operations

Element-wise operations apply a mathematical operation to each corresponding element of arrays


or matrices. These operations are performed using a dot (.) before the operator.
3. MATRIX FUNCTIONS
In MATLAB, matrix functions are specialized operations that are used to manipulate and analyze
matrices. These functions allow you to perform various matrix operations, such as computing
determinants, eigenvalues, inverses, and solving systems of equations. Below is a detailed
overview of common matrix functions in MATLAB.

1. Matrix Transpose (')

The transpose of a matrix swaps its rows and columns. In MATLAB, the transpose is denoted by
the single quote (').
2. Matrix Determinant (det)

The determinant of a matrix is a scalar value that can be computed for square matrices. It provides
information about the matrix, such as whether the matrix is invertible (if the determinant is non-
zero).
4. Matrix Trace (trace)

The trace of a matrix is the sum of its diagonal elements. It is often used in various matrix
operations.

5. Matrix Diagonal (diag)

The diag function can be used to extract the diagonal elements of a matrix or create a diagonal
matrix from a vector.
4. MATRIX CONCATENATION IN MATLAB

Concatenation is the process of combining two or more matrices (or arrays) into a larger matrix.
In MATLAB, concatenation is a common operation and can be performed along different
dimensions, such as horizontally (side by side) or vertically (stacked on top of each other).

Types of Concatenation

1. Horizontal Concatenation: Combine matrices side by side.

2. Vertical Concatenation: Combine matrices on top of each other.

3. Concatenation Along Higher Dimensions: For multi-dimensional arrays, concatenation


can also be done along the third dimension or higher.

1. Horizontal Concatenation

To concatenate matrices horizontally (side by side), the number of rows must be the same in both
matrices. You use square brackets [] to concatenate them.

In this example, A and B are concatenated along the second dimension (columns), creating a 2x4
matrix.
2. Vertical Concatenation
To concatenate matrices vertically (stacking them on top of each other), the number of columns in
both matrices must be the same. You also use square brackets [] to concatenate them.

In this example, A and B are concatenated along the first dimension (rows), creating a 4x2 matrix.
The two row vectors A and B are concatenated horizontally into a single row vector.

The two column vectors A and B are concatenated vertically into a single column vector.
4. Concatenation with Different Sizes

You can also concatenate matrices of different sizes, as long as they have the same number of rows
(for vertical concatenation) or the same number of columns (for horizontal concatenation).

In this example, A and B are row vectors with different sizes (1x2 and 1x3), but since they are row
vectors, they can be concatenated horizontally.

Concatenation in Higher Dimensions


MATLAB also allows concatenation along the third dimension (and higher) for 3D arrays. For
example, you can stack matrices into a 3D array.
Rules for Higher-Dimensional Concatenation
1. Dimensions Must Match: Except for the dimension specified by dim, all other dimensions
of the arrays being concatenated must be the same.
2. Concatenation Dimension Expands: The size of the resulting array along dim will be the
sum of the sizes of the input arrays along that dimension.
Example 1

Concatenate two or more matrices in MATLAB, you can use the cat function or the square bracket
[] operator.

% Define two matrices

A = [1, 2; 3, 4];

B = [5, 6; 7, 8];

(i) Concatenate the matrices horizontally using cat C = cat (2, A, B);
(ii) Concatenate the matrices vertically using cat D = cat (1, A, B);
(iii) Concatenate the matrices horizontally using square bracket E = [A, B];
(iv) Concatenate the matrices vertically using square bracket F = [A; B]
Display the results disp(C); disp(D); disp(E); disp(F);

Output:

disp(C);

1 2 5 6

3 4 7 8

disp(D);
1 2
3 4

5 6

7 8

disp(E);

1 2 5 6

3 4 7 8

disp(F);
1 2

3 4

5 6

7 8
5. RESHAPING AND RESIZING IN MATLAB

Reshaping and resizing are powerful tools in MATLAB for altering the shape or size of matrices
and arrays without changing their elements. These operations allow you to organize data into
different forms suitable for analysis, computation, or visualization.

Reshaping

The reshape function changes the dimensions of a matrix or array to a specified size while
preserving its elements in column-major order (MATLAB's default element storage order).
The elements are taken column-by-column from A to form the new matrix.
Resizing

Resizing involves changing the size of a matrix or array by either adding or removing elements.
6. LOGICAL INDEXING IN MATLAB

Logical indexing is a powerful feature in MATLAB that allows you to select and manipulate
elements of an array or matrix based on conditions. Instead of using numerical indices, logical
indexing uses a logical array of the same size as the target array, where true indicates the elements
to include and false indicates the elements to exclude.
HOW TO WRITE MATLAB PROGRAMS OR CODE

Writing MATLAB programs involves creating scripts or functions that can perform calculations,
process data, or implement algorithms.

Steps to Write a MATLAB Program

1. Plan the Program

• Define the purpose of the program.

• Identify the input and output requirements.


• Break the task into smaller steps or functions.

2. Open MATLAB Editor


• Open MATLAB and navigate to the Editor window (shortcut: Ctrl + N to create a new
script).

• Save your script with a .m extension (e.g., my_program.m).

3. Write the Code

Start writing the code in the script file. A MATLAB program typically consists of the following
components

Components of a MATLAB Program

A. Comments

• Use comments to describe the purpose and functionality of your program.


• Comments start with % or %% for section comments.

B. Input

• Use functions like input to take user inputs.


C. Variables

• Define variables to store data.

D. Mathematical Operations

• Use built-in operators and functions.

E. Conditional Statements

• Use if, else, elseif, and switch for decision-making.


F. Loops

• Use for or while loops for repetitive tasks.

G. Functions
• Create reusable functions in separate .m files or as subfunctions.

H. Output

• Display results using disp or formatted output using fprintf.


Save and Run the Program

• Save the script (e.g., circle_area.m).

• Run the program by typing its name in the Command Window:

Repmat Function

The repmat function in MATLAB is used to replicate and tile an array or matrix. This function is
particularly useful when you want to create a larger matrix by repeating smaller matrices in a
specific pattern.
The scalar 5 is repeated 3 times in the rows and 4 times in the columns, forming a 3x4 matrix.
The row vector [1, 2, 3] is repeated 4 times vertically to create a 4x3 matrix.

The 2x2 matrix A is replicated 3 times vertically and 2 times horizontally, resulting in a 6x4 matrix.
In this example, the 2x2 matrix A is replicated to form a 4x6 matrix by repeating it 2 times
vertically and 3 times horizontally.

FROM MATHEMATICAL EXPRESSIONS TO MATLAB EXPRESSIONS

Converting mathematical expressions into MATLAB code involves translating mathematical


symbols and operations into MATLAB syntax. MATLAB is designed for numerical computing
and matrix manipulation, so its syntax is well-suited for expressing mathematical operations.
Example 1

To evaluate r in a complex equation involving x, y, and z using the minimum number of


parentheses, you should follow these steps:

1. Identify the equation: Let's assume a complex equation for r in terms of x, y, and z. For
example:

2. Simplify the expression: MATLAB automatically follows operator precedence, so we can


write the equation with minimal parentheses, ensuring that only necessary parentheses are
included for clarity or to override default precedence.
3. Write the MATLAB code: Below is an example of how to implement this equation in
MATLAB.
Consider a slightly more complex equation:
Let's consider a more intricate expression involving powers, sine, and exponentiation:
Consider a more complicated equation with nested functions:
Now, let's consider a more advanced example involving matrix operations:
Consider an equation where the value of rrr depends on a conditional expression:
Let's say we want to evaluate rrr as a sum of expressions inside nested loops:

You might also like