MATLAB notes 1
MATLAB notes 1
What is MATLAB?
MATLAB is a programming language and interactive environment used for numerical computing,
data analysis, and visualisation.
In programming, a variable is a named storage location in memory that holds a value. This value can
be a number, text, or other data type, and it can be changed or updated as the program runs.
Naming: A variable has a name (identifier) that is used to reference a stored value.
Data storage: The variable holds a specific piece of data, e.g., a number, string, lists. Creating a
variable reserves a space in the computer memory to store that data.
Data types: Variables have types, e.g., integer, real, string, Boolean. In some programming
languages these need to be specified, while in others can infer it automatically.
In the Workspace browser you can view all variables defined in the workspace.
Functions
In MATLAB there are a large number of pre-defined functions. For a complete list of pre-defined
function can be found in the following link:
https://uk.mathworks.com/help/matlab/referencelist.html?type=function
user-defined functions: It is also possible to write user-defined functions. The basic syntax
for defining a function is:
To define a function you must specify the function command. A funtionName needs to be
specified followed by a list of arguments (i.e., arg1, arg2,…), which are inputs to the function.
Arguments are used within the ‘function block’ to determine a value that will be the output of the
function. The output is assigned to a variable varName is also set to be equal to the
functionName. The command end terminates the function block.
Example 1: Here is an example of such a function for the calculation of the area of a circle. A
function name is required and in the example below is it areaCircle. The argument of the
function is R, which is the input variable to the function. The body of the function
Example 2: Here is a function for determining the yield stress of a material based on the Hall-Petch
relation given by
k
σ y=
√d
where d is the grain size and k is the Hall-Petch parameter.
Vectors and matrices are represented by arrays – these data structures in MATLAB that stores
collections of separate values (scalars).
Arrays in MATLAB can hold numbers, characters, or logical values in multiple dimensions, making
them essential for numerical computations, data analysis, and matrix operations.
1D Array (Vector): A one-dimensional array (or vector) is a list of values stored in a single row
or column.
2D Array (Matrix): A two-dimensional array (or matrix) consists of rows and columns of
values, similar to a table.
Multidimensional Array: MATLAB supports arrays with more than two dimensions. These
are often used for storing 3D or higher-dimensional data, like image data or data over time.
Operations: It is possible to carry out a multitude of operations on arrays and include arithmetic
operations as well as manipulation of the arrays such as transpose of a matrix.
A= ( 14 2 3
5 6 )
B= (161 4 9
25 36 )
What is A+ B?
Multiplication of matrices with vectors we use the * operator. For example the multiplication of a
()
2
3x3 matrix A= (
1 2 3
4 5 6 )
with a 3 component column vector v c = 3 , i.e.,
1
A . vc
In MATLAB
Note that this operation will not work is you try to multiply incompatable matrices and vectors. For
MATLAB uses the dot-operator (.) construction to distinguish between scalar-vectorised operations
and matrix operations. Dot-operators are meant to repeat operations on the members of the array.