Introduction to MATLAB
This material is intended to introduce you to mathematics from a numerical point of view. This is why you need a tool like Matlab (Matrix Laboratory) from MathWorks, which makes the calculations easier and at the same time is enough flexible and powerful to solve problems in all areas of Engineering.
I hope that from now on the Numerical go with you!!
TS.
The Matlab Interface
Once we run Matlab, the main window (interface) is opened, which is separed by 3 principal columns:
- Current Folder (left): It is the working folder ( it can be changed from the top line) where ALL files must be.
- Command Window (center): Communication space with the Matlab. Here we will write all the instructions we need to perform calculations.
- Workspace (right): A place where we visualize all the variables that have to do with our calculations.
Matlab Instructions
To communicate with the Matlab nucleus and do the numerical calculations we need, we need a programming language (Matlab's own) that provides the instructions we will need to perform the calculation. There are two ways to do this:
- One by one per command line (type and press return)
>>x = 2; %if we put a semicolon the result is not displayed on the screen
>>y = 3;
>>x + y
- Many at a time using a file (Matlab files have .m extension)
We'll see examples, but it's about writing all the instructions in a file (also called a script) and pressing Run.
Let's do this:
- Click on the + sign icon on the interface. A blank sheet will open inside the editor window.
- Copy the 3 lines into the space with a gray background below
- Click on the run button. You will be asked for a name for the file, you can put whatever you want, for example: prac0. It will automatically execute and output the result to the Command Window
Basic Operations
Basic Mathematical Functions
valor_pi=pi %it is a matlab variable for the pi number (do not use this name as a variable!!)
% in the same way: asin,acos,atan,sinh,cosh,asinh,acosh,
% exp i log (which is the neperian logarithm)
Release a variable
if we want to delete a variable we will use the clear command
% if we accidentallyy assign a matlab variable and we want to fix it again
% TAKE CARE!! if we write: 'clear all' or only 'clear' we delete ALL variables in the Workspace
Vector Operations
To create a vector we can define it in two ways:
- row vector: It can be generally defined in three ways:
- directly: v=[1,2,3,4] (comma-separated values ��or whitespace)
- by ranks: v=1:4 (the format is v=Beginning:Step:Ending (default step = 1 and it doesn't need to be set))
- by spaced values: linspace(1,4,4) (see the linspace function)
- column vector: v=[1;2;3;4]
vectorF = [1 2 3 4] %white space is comma equivalent
vecRang = 1:2:8 %range of values
vecRang(4) = 19 %we access component 4 and modify it. Notice the round brackets.
vecRang(1:3) = 6 %we modify the values ��of components 1 to 3
vecRang(ind) %we show components 4 and 3 of the vecRang vector
Other manipulations that can be done with vectors
If
, (do your own examples with vectors) - length(u) : it returns the number of components of u
- sum(u) : it returns the sum of the components of u.
- prod(u) : it returns the product of the components of u.
- max(u) : it returns the value of the highest component
- abs(u) : it returns the vector of absolute values.
- sort(u) : it returns the vector u sorted in increasing order.
- dot(u,v) : it returns the scalar product of u and v.
- cross(u,v) : it returns the vector product of u and v (restricted to vectors of
) - cumsum(u) : it returns a vector where each component is the cumulative sum of the components of u from the first one to the mentioned one.
- cumprod(u) : similar to the previous case but with products.
For example:
trasP = vectorC' % transposed vector
pEsc = dot(vectorF,vectorC') %both vectors must be rows
Ordering of the components of a vector
w = sort(v) %gives the values ��ordered
[vOrdenat,perm] = sort(v) %perm gives the positions of v with increasing order
w = v(perm) %we see it sorted without losing the order of the original vector
Consult any function or command in Matlab
One of the advantages of Matlab is that there is a lot of documentation on the internet. You can google any questions related to Matlab. However, if we know the name of the function or instruction we want to consult, it is enough to use the doc command
doc max %it opens the documentation page associated with the max function
Operations per element
If we have a vector and we want the vectors that have the same components but squared, you can't do v^2, you must do v.^2
This point is named element-by-element operator
Similarly, if we want to calculate an operation for all the components of a vector we will use this operator. For example, to compute w = x * sin (x) for an entire vector x, we will do:
w = x.*sin(x) % the elementary functions are ready to act on
w =
1.8186 0.8415 0 0.8415 1.8186
% vectors and therefore it is not necessary to write w=x.*sin(x.);
Manipulation of matrices
To create a matrix we can define it from several vectors:
A = [ 1 2 3 2; 0 -2 3 1; 6, 7, 8, -6] %matrix 3x4 defined by rows
A =
1 2 3 2
0 -2 3 1
6 7 8 -6
% try to understand the results
Afila1 = A(1,:) %the whole first row
Acolumna2 = A(:,2) %the whole second row
Asub = A(1:2, [1,3]) %submatrix formed by rows 1 and 2 and columns 1 and 3
Matrix algebra operations
- Scalar product:

- Sum:
(attention to the dimensions) - Matrix product:
- Vector matrix product:
(it must be a column vector) - Power: A^3 is the same as

Homework:
(c) Numerical Factory 20202