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:

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:
>>x = 2; %if we put a semicolon the result is not displayed on the screen
>>y = 3;
>>x + y
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:
  1. Click on the + sign icon on the interface. A blank sheet will open inside the editor window.
  2. Copy the 3 lines into the space with a gray background below
  3. 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
x = 2;
y = 3;
x + y
ans = 5

Basic Operations

suma = x+y
suma = 5
resta = x-y
resta = -1
producte = x*y
producte = 6
divisio = x/y
divisio = 0.6667
potencia = x^y
potencia = 8

Basic Mathematical Functions

arrelquad=sqrt(x)
arrelquad = 1.4142
arrelcub=x^(1/3)
arrelcub = 1.2599
sinus_a=sin(x)
sinus_a = 0.9093
cosinus_a=cos(x)
cosinus_a = -0.4161
tangent_a=tan(x)
tangent_a = -2.1850
valor_pi=pi %it is a matlab variable for the pi number (do not use this name as a variable!!)
valor_pi = 3.1416
% 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
clear x;
% if we accidentallyy assign a matlab variable and we want to fix it again
% we will use clear
pi = -1
pi = -1
clear pi;
pi
ans = 3.1416
% 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:
  1. directly: v=[1,2,3,4] (comma-separated values ��or whitespace)
  2. by ranks: v=1:4 (the format is v=Beginning:Step:Ending (default step = 1 and it doesn't need to be set))
  3. by spaced values: linspace(1,4,4) (see the linspace function)
vectorF = [1,2,3,4]
vectorF =
1 2 3 4
vectorF = [1 2 3 4] %white space is comma equivalent
vectorF =
1 2 3 4
vecRang = 1:2:8 %range of values
vecRang =
1 3 5 7
vectorC = [1;2;3;4]
vectorC =
1 2 3 4
vecRang(4) = 19 %we access component 4 and modify it. Notice the round brackets.
vecRang =
1 3 5 19
vecRang(1:3) = 6 %we modify the values ��of components 1 to 3
vecRang =
6 6 6 19
ind=[4,3];
vecRang(ind) %we show components 4 and 3 of the vecRang vector
ans =
19 6

Other manipulations that can be done with vectors

If , (do your own examples with vectors)

For example:
u = vectorF*vectorC
u = 30
trasP = vectorC' % transposed vector
trasP =
1 2 3 4
length(vectorF)
ans = 4
length(vectorC)
ans = 4
pEsc = dot(vectorF,vectorC') %both vectors must be rows
pEsc = 30

Ordering of the components of a vector

v = [5,2,-1,9,0];
w = sort(v) %gives the values ��ordered
w =
-1 0 2 5 9
% equivalent wat
[vOrdenat,perm] = sort(v) %perm gives the positions of v with increasing order
vOrdenat =
-1 0 2 5 9
perm =
3 5 2 1 4
w = v(perm) %we see it sorted without losing the order of the original vector
w =
-1 0 2 5 9

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
v = 1:5
v =
1 2 3 4 5
vq = v.^2
vq =
1 4 9 16 25
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:
x = -2:2;
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
v = [10 20 30]
v =
10 20 30
B = [1 2; 3 4; -1 -2]
B =
1 2 3 4 -1 -2
A = [B,v']
A =
1 2 10 3 4 20 -1 -2 30
C = [v; B']
C =
10 20 30 1 3 -1 2 4 -2
% try to understand the results
Afila1 = A(1,:) %the whole first row
Afila1 =
1 2 10
Acolumna2 = A(:,2) %the whole second row
Acolumna2 =
2 4 -2
Asub = A(1:2, [1,3]) %submatrix formed by rows 1 and 2 and columns 1 and 3
Asub =
1 10 3 20

Matrix algebra operations

Homework:

Go to the Mathworks Educative web page and complete the free course MATLAB onramp (about 2h, you'll get a certificate after you finish it).
(c) Numerical Factory 20202