Introduction to Numerical Methods and MATLAB
Programming for Engineers
Lecture 3: Introduction to vectors and matrices, Plots,
Bisection
Claudia Zoccarato
E-mail: [email protected]
June, 06 2017
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
Introduction to vectors and matrices
All MATLAB variables are multidimensional arrays, no matter what type
of data. An array data structure, or simply an array, is a data structure
consisting of a collection of elements, each identified by at least one array
index or key.
ARRAY with one index − > VECTOR
ARRAY with two indices − > MATRIX
Scalar value: ARRAY 1 x 1
Row vector: ARRAY 1 x n
Column vector: ARRAY n x 1
Matrix: ARRAY n x m
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
Introduction to vectors and matrices
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
Introduction to vectors and matrices
To transform a row vector in a column vector and vice versa, you can use
the apex symbol, which correspond to the TRANSPOSE operation.
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
MATLAB predefined function
length length(x) length of vector x
size size(A) number of rows and columns of A
’ to transpose a vector or a matrix
who indicate the variables available in the workspace
whos gives information on the dimension, the occupied memory and the type of varia
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
Vectors in MATLAB
Example of an array with one index:
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
Vectors in MATLAB
Define a row vector in MATLAB:
>> x = [1 -1 3 4 0]
Define a column vector in MATLAB:
>> x = [1; -1; 3; 4; 0]
Example: extract the 4th component of the vector (row or column):
>> x(4)
ans =
4
Example: assign a value to the 3rd component of a vector (row or column):
>> x(3) = -2
Use the commands who and whos to verify which are the variables available in the
workspace and the information on their dimensions, occupied memory, etc, ...
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
Vectors in MATLAB - the colon notation
Creation of regularly spaced vectors:
vector = begin:increment:end
>> x = 0:0.1:0.5
x =
0 0.1000 0.2000 0.3000 0.4000 0.5000
Command linspace generates N points between X1 and X2:
linspace(X1,X2,N)
>> x = linspace(0,0.5,6)
The number of points is optional, if not present is equal to 100
Extract the components of a vector:
>> y = x(2:4)
y =
0.1000 0.2000 0.3000
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
Plot of a function: command plot
To plot a function which is not predefined in matlab
1 Command plot (main graphic function of MATLAB)
>> plot(x,y)
Plot of the points defined by (xi , yi ).
The two vectors must be of the same length.
2 Vectorize a function:
>> x = linspace(0,pi,10);
>> y =
(15120-6900*x.^2+313*x.^4)./(15120+660*x.^2+13*x.^4);
3 Element-wise operations: ./, .*, .^
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
Plot of a function: command plot
Some useful option to plot in MATLAB:
1 Specify the line type, color and symbol
Example: Red, dashed line with star symbol
>> plot(x,y, ’r--*’)
r red
g green
b blue
c cyan
m magenta
y yellow
k black
w white
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
Plot of a function: command plot
Some useful option to plot in MATLAB:
1 Open more than one window to plot the functions:
>> figure –> Open a generic window
>> figure(1) –> Open the figure number 1
2 Add title to the plot:
>> title(’name of the title’)
3 Add label to the X-axis:
>> xlabel(’name axis x’)
4 Add label to the Y-axis:
>> ylabel(’name axis y’)
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
The anonymous functions
1 If we want to define the following function:
f (arg1, arg2, ..) = expression
2 The syntax is the following:
>> f = @(x)
(15120-6900*x.^2+313*x.^4)./(15120+660*x.^2+13*x.^4)
3 After the symbol @ the input value of the function is defined.
4 I can evaluate the function in z = 0:
>> f(z)
ans =
1
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
Exercises
1 Evaluate the function p(x) = (x − 1)7 in x = [0.99, 1.01] with
increment ∆x = 0.001.
Plot the function in the given interval.
2 Plot the following function:
y = −4x − 13 per −4 < x ≤ −3
y = 2x + 5 per −3 < x < 0
HINT: after the plot of the first interval, use the command hold on
to plot the function of the second interval in the same figure.
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
The bisection method
The bisection method in mathematics is a root-finding method that
repeatedly bisects an interval and then selects a subinterval in which a
root must lie for further processing. It is a very simple and robust method,
but it is also relatively slow.
1: Choose a0 and b0 such that f (a0 )f (b0 ) < 0
2: while convergence is not reached
3: ck = (ak + bk )/2
4: if f (ck )f (ak ) > 0
5: ak+1 = ck
6: else
7: bk+1 = ck
8: endif
9: τk+1 =|ak+1 − bk+1 |
10: end while
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
The bisection method
Implement the bisection method using a scrip to find the roots of the
function f (x) = ln(x) + x2 − sin(πx) in the interval [0.1, 2π]. Use the
following hints:
1 Start from the interval [a0 , b0 ] = [a, b] and calculate the midpoint c0
2 Check the following cases:
if f (c0 )f (a0 ) > 0 then [a1 , b1 ] = [c0 , b0 ]
if f (c0 )f (b0 ) > 0 then [a1 , b1 ] = [a0 , c0 ]
3 This procedure is repeated each time subdividing the interval. The
procedure will end when a certain accuracy is reached.
4 The while loop is repeated until convergence. The criterion has to be
defined such that τk+1 > toll and iter < itmax
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
The bisection method
Complete the previous code with the following tasks:
1 Plot the function f (x) in the given interval and check if the function
has opposite values.
2 Display on the screen a table with the results, for example:
Iteration number |Value of a |Value of b |solution c |f(c)
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
The bisection method
Complete the code with the appropriate commands instead of dots.
1 clear
2 close all
3 % The b i s e c t i o n method i n MATLAB
4 a = 0.1;
5 b = 2∗ p i ;
6 t o l l = 10ˆ −6;
7 itmax = 100;
8 t a u = a b s ( a−b ) ;
9 f=@( x ) l o g ( x )+x .ˆ2− s i n ( p i ∗x ) ;
10 i t e r = 0;
11 while . . .
12 ...
13 if ...
14 ...
15 else
16 ...
17 end
18 ...
19 fprintf ( ’ ... ’)
20 end
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017