Presentación de PowerPoint - Bindura University of Science Education

advertisement
A brief introduction to MATLAB for
students of Science, Engineering
and Mathematics
Jorge Lemagne
Bindura University
Summary
Introduction
Getting started with MATLAB
Illustration in Math courses
2
What is MATLAB?
MATLAB is a high-level language and
interactive environment for numerical
computation, visualization, and
programming (MathWorks, 2013).
3
Some applications of MATLAB
Signal processing and communications
Image and video processing
Control systems
Test and measurement
Computational finance
Computational biology
4
Widely used
More than a million engineers and
scientists in industry and academy use
MATLAB, a language of technical
computing.
5
Also an aid for mathematics students
• Moreover, MATLAB is a teaching and learning
aid for mathematics students.
• It has been integrated as a supplement to the
traditional classroom teaching and learning.
• For example, at University of Ha'il, Saudi
Arabia, a study was made on engineering
mathematics students.
6
Benefits
• The use of the software has enhanced
students' conceptual understanding despite
their weak mathematical skills.
• It has been noticed that students' attitudes
have been positive and their performance in
the course has improved
7
For mathematics courses
• Students taking some mathematics courses
are expected to acquire a basic working
knowledge of MATLAB (University of Utah).
• MATLAB may be used to help complete some
of the homework assignments.
• Also for the completion of designated
computer assignments (University of Utah).
8
Some requirements
• Students need to have access to a computer
with MATLAB installed (Massachusetts
Institute of Technology, 2015).
• They should have a way to access their files
whenever they start working.
• In particular, they need to have access to
MATLAB outside of class hours.
9
A two hours introduction
• In the bibliography there exist several good
tutorials on MATLAB.
• However, a student would need considerable
time to assimilate any of them, and so would a
lecturer for the explanation.
• The brief introduction that is presented in this
contribution can be explained in two hours
and has been exposed as a lecture by the
author at Bindura University.
10
In a computer laboratory
• Preferably, this lecture should be given in a
computer laboratory so that the students can
verify in practice all explanations.
• After learning this brief introduction the
students will be able to solve simple
problems, or to study a tutorial or to pass a
course on MATLAB more easily.
11
Summary (2)
Introduction
 Getting started with MATLAB
Illustration in Math courses
12
GETTING STARTED WITH MATLAB
• The name MATLAB stands for MATrix
LABoratory.
• The basic data element in MATLAB is an array
that does not require dimensioning.
13
Much faster
This allows you to solve many
technical computing problems,
especially those with matrix and
vector formulations, in a fraction of
the time it would take to write a
program in a scalar non-interactive
language such as C or FORTRAN.
14
Starting MATLAB
• On Windows platforms, to start MATLAB,
double click the MATLAB shortcut icon on your
Windows desktop.
• After a short lapse, you will see something like
this:
15
Command Window
For the time being we only need this window, the
Command Window.
If there are others open, we can close them.
16
Vectors
Let us start to use the Command Window,
manipulating some vectors (i.e. arrays of one
dimension).
17
You can work with entire arrays
Where other programming
languages work with numbers one at
a time, MATLAB allows you to work
with entire arrays quickly and easily.
18
To enter arrays
We can enter arrays into MATLAB
in several different ways.
One of them is by entering an
explicit list of elements.
19
Entering vectors: Example
At the command line, after MATLAB prompt
“>>”, type this statement:
>> a = [6 1 4 2];
>>
This instruction or statement assigns the vector
[6 1 4 2] to the variable “a”.
20
An assignment operator
>> a = [6 1 4 2];
• “=” does not have the same meaning as in
Mathematics.
• In this case, it is not a relational operator but
an assignment operator.
21
Workspace
Once you have entered the array, it is
automatically remembered in the MATLAB
Workspace:
You can refer to the array, simply by its name, in
this case “a”.
22
The Colon Operator
The colon “:” is one of the most important MATLAB
operators.
It occurs in several different forms:
The expression 1:7 is a row vector containing the integers
from 1 to 7
1
2
3
4
5
6
7
23
For non-unit spacing
Specify an increment.
>> 0:pi/4:pi
ans =
0 0.7854 1.5708 2.3562 3.1416
Note: When you do not specify an output
variable, MATLAB uses the variable “ans”, short
for answer, to store the results of your
calculation.
24
The Colon Operator: Another example
It is required to plot the “sin” function between
0 and 2𝜋, step 0.1, with blue * markers:
>> x = 0:0.1:2*pi;
>> y = sin(x);
>> plot(x,y,'*')
>>
The following figure is obtained:
25
Sin function between 0 and 2π and
step 0.1 (1)
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0
1
2
3
4
5
6
7
26
Hundreds of available functions
• MATLAB has hundreds of available functions.
• To display documentation of one, for example,
sin, type
>> doc sin
27
M files
M files are text files containing
MATLAB code.
To create an M file we can use the
MATLAB Editor or another text editor.
28
MATLAB Editor (1)
29
The same statements
The M file will contain the same
statements you would type at the
MATLAB command line.
Save the file under a name that ends
in “.m”.
30
MATLAB Editor (Rev)
31
Plotting “sin” with specified step
• It is required to plot the sin function between
0 and 2𝜋, with blue * markers, with a given
step, specified by the user.
• The M file may content the following:
32
Content of M file
% This very simple program plots sin(x) for
% different values between 0 and 2*pi.
Answer = inputdlg(‘Step size=‘);
Step = str2double(Answer);
x = 0:Step:2*pi;
y = sin(x);
plot(x,y,'*')
33
Input dialog box
34
Content of M file (Rev)
% This very simple program plots sin(x) for
% different values between 0 and 2*pi.
Answer = inputdlg(‘Step size=‘);
Step = str2double(Answer);
x = 0:Step:2*pi;
y = sin(x);
plot(x,y,'*')
35
Sin function between 0 and 2π and
step 0.1 (Rev)
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0
1
2
3
4
5
6
7
36
Script
The M file corresponding to the
preceding program is called script.
Scripts are the simplest kind of M files
because they have no input or output
arguments.
37
Matrices and Subscripts
• Let us create the matrix
>> A = [16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1];
>>
• This table is known as Dürer's magic square.
38
Dürer
Albrecht Dürer (1471–1528) was a painter,
printmaker and theorist of the German
Renaissance.
Dürer’s Self-Portrait at 28 (a), Melencolia I (b) and
Detail of Melencolia I (c)
39
Dürer's magic square
[16
5
9
4
3
10
6
15
2
11
7
14
13
8
12
1]
Sum is 34 by: Rows
Columns
Diagonals
Quadrants
In the center four squares
In the corner squares
etc.
40
Sums by columns
A = [16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1]
Let us verify a few of these features. The first
instruction to try is
>> sum(A)
ans =
34 34 34 34
These are the sums by columns.
41
Sums by rows
A = [16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1]
To obtain the sums by rows, we consider first A’, the conjugate
transpose of A. Then, we type:
>> sum(A')'
ans =
34
34
34
34
42
Sum on the main diagonal
A = [16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1]
The sum of the elements on the main diagonal is
obtained with “sum” and “diag” functions:
>> sum(diag(A))
ans =
34
43
Subscripts with colons
• Subscript expressions involving colons refer to
portions of a matrix.
• For example, sum(A(:, 4)) computes the sum
of the elements in the fourth column of A:
>> sum(A(:, 4))
ans =
34
44
Another way for creating matrices
• On the example of order-4 magic square, we
created a matrix by entering an explicit list of
elements.
A = [16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1];
• Another way is by “load” command. See paper.
45
Summary (3)
Introduction
Getting started with MATLAB
 Illustration in Math courses
46
ILLUSTRATION IN MATH COURSES
• The author of this paper is introducing
MATLAB in his courses.
• Here are two examples:
47
Multivariate Methods
The following table shows the heights (in inches) and the weights
(in pounds) for 10 employees of a firm (Taken from Makridakis &
Wheelwright (1983)).
61
59
63
63
64
60
65
68
69
61
163
114
161
144
145
118
156
160
167
141
Examine the extent of the relationship between the two
measures.
48
To solve Height_Weight problem (1)
• Create an M file with the preceding table
• Save it as Height_Weight.m (for example).
• In the Command Window, type:
>> load Height_Weight.m
>> mean(Height_Weight)
ans =
63.3000 146.9000
>> % These are the corresponding means.
49
To solve Height_Weight problem (2)
>> cov(Height_Weight)
ans =
10.9000 44.4778
44.4778 342.3222
>> % This is the covariance matrix.
50
To solve Height_Weight problem (3)
>> corrcoef(Height_Weight)
ans =
1.0000 0.7281
0.7281 1.0000
>> % This is the correlation coefficient matrix.
>>
• The sample is not large, but we might say there is quite
a strong relationship between the two variables.
• “mean”, “cov” and “corrcoef” are only three of the
many functions that can be used in MATLAB.
51
Optimization
Consider the following linear programming problem:
Maximize 𝑧 = 2𝑥1 + 3𝑥2
subject to
2𝑥1 + 𝑥2 ≤ 4,
𝑥1 + 2𝑥2 ≤ 5,
𝑥1 , 𝑥2 ≥ 0,
In this paper, we are only concerned with the graphical
solution.
Applying the graphical facilities of MATLAB we obtain the
following figure:
52
Graphical solution of Optimization
problem
Therefore, the solution is z=8.
53
REFERENCES (1)
• Majid, M.A., Huneiti, Z.A., Al-Naafa, M.A. &
Balachandran, W., (2012), A study of the
effects of using MATLAB as a pedagogical tool
for engineering mathematics students,
Interactive Collaborative Learning (ICL) 15th
International Conference on,
http://ieeexplore.ieee.org/xpl/
54
REFERENCES (2)
• Makridakis, S., & Wheelwright, S (1983),
Forecasting methods and Applications, 2nd
edition, New York Wiley
• Massachusetts Institute of Technology (2015),
Syllabus, MIT OpenCourseWare,
http://ocw.mit.edu/courses/mathematics/18s997-introduction-to-matlab-programmingfall-2011/Syllabus/
55
REFERENCES (3)
• MathWorks, The, Inc., MATLAB R2013a
• University of Utah, The, Department of
Mathematics, (n. d.), MATLAB Information,
http://www.math.utah.edu/
56
REFERENCES (4)
• Wikimedia Foundation, Inc., (2015), Magic
square, Wikipedia,
https://en.wikipedia.org/wiki/
• York University (n. d.), MATLAB LESSON 1,
http://www.yorku.ca/jdc/Matlab/Lesson1.htm
57
Download