Title | Introduction to Matlab |
---|---|
Author | Gideon Chia |
Course | Mathematics I |
Institution | National University of Singapore |
Pages | 35 |
File Size | 2.7 MB |
File Type | |
Total Downloads | 36 |
Total Views | 138 |
Download Introduction to Matlab PDF
INTRODUCTION TO MATLAB
1. I NSTALLATION The National University of Singapore has a Total Academic Headcount licence for
.
Students may use it for academic, research, and learning. The license allows students to install on personally-owned computers. 1. If you are NOT using NUS network, you are required to use
:
◦ Please sign in with your NUSNET ID in the format: nusstu\nusnetid and password. If you are using NUS network, please proceed to Step 2. 2. Click the link
to download
. It
is required to sign in with your NUSNET ID in the format: nusstu\nusnetid and password. Then follow the instructions. 3. Create a
account using your NUS email address.
(i) ⋄ Email address: Your NUS email account (e.g., [email protected]) ⋄ Location: Singapore
⋄ How will you use MathWorks software? Student use ⋄ Are you at least 13 years or older? Yes (ii) You will receive an email from
with title “Verify Email Ad-
dress”. Click the link in the email to verify your account. (iii) Finish creating your profile. Then you should be able to see the following information: ◦ Your account has been created and license 40707750 has been associated with your account.
4. Click the Download bottom to download and run the installer. (i) When prompted, log in with your
Account (your NUS email account).
(ii) Select your licence (40707750, Student, Academic — Total Headcount). (iii) Choose installation folder. (iv) Select products to install. 1
INTRODUCTION TO MATLAB
2
2. B ASIC O PERATIONS First of all, we learn some basic operations in
.
environment behaves like a super-complex calculator. You can enter the commands at the >> command prompt. The answer appears by pressing We can use the following arithmetic operators: addition , division
, exponentiation
.
, subtraction
, multiplication
.
For example, to add two numbers 123 and 321, we simply type >>
and then press
:
Similarly, we can apply other operators to use
as an ordinary calculator:
>>
>>
>>
>>
You may add a semicolon
at the end of the statement; then
will hide the output.
For example, >> >>
Note that the symbol
is now defined as 3. We may remove it from the memory by using
or remove all variables from the memory by window, we can use
. If we want to clear the command
.
3. B ASIC F UNCTIONS The function >>
computes the principle square root of the number x. For example,
INTRODUCTION TO MATLAB
By default,
3
displays four decimal digits to its answers. But we can change the for-
mat for numeric display. (The percent symbol
is used for indicating a comment line.)
>> >>
>> >>
>> >>
We can also use the following functions in (i) Trigonometric functions:
:
,
(ii) Inverse trigonometric functions: (iii) Exponential and logarithmic functions:
,
,
,
,
, ,
, ,
. .
(base e ),
(base 10).
For trigonometric and inverse trigonometric functions, the angles are measured in radian (π radian = 180◦ ), and the constant π = 3.1415926 · ·· is defined by
(
is case-sensitive).
>>
>>
4. S OLVING B ASIC A LGEBRAIC E QUATIONS The command (i) Use (ii) Use Note that
is used for solving algebraic equations. We shall
to declare the variables. to solve the specific equations. is used to assign a value to a variable; and
shall be used for equality.
4.1. Single Variable Equations. For example, x 2 + x − 1 = 0. >> >>
INTRODUCTION TO MATLAB
We may use
or
to evaluate each element of the symbolic input to d
digits (the default is 32 digits). >>
Sometimes the exact solutions cannot be specifically displayed. For example, x 4 − 7x 3 + 3x 2 − 5x + 9 = 0. >>
Then evaluate using floating points to obtain the decimal expressions of the four roots: >>
Here the symbol
refers to the imaginary unit i where i = e πi /2 such that i 2 = −1.
Alternatively, one may use
to get the decimal expression directly:
>>
can also be used to solve symbolic equations. For example, ax 2 + bx + c = 0. >> >>
4
INTRODUCTION TO MATLAB
4.2. Multi-Variable Equations.
5
can also solve multi-variable equations, for which
the equations shall be put together in square brackets
, so do the variables. Moreover, since
the output consists multiple variables, we shall assign the solution as a vector. For example, 3x + y = 10,
x + y = 20.
>> >>
The solution of the a general linear system in variables x and y ( ax + by = e cx + d y = f
can be obtained as follows: >> >>
5. S UMS If we need to find the sum of a sequence, use the command
. The format is
>>
For example, for the arithmetic sequence {a n } given by a n = a + (n − 1)d , the sum of its first
n terms is (2a + (n − 1)d )n/2. >> >>
>>
For the geometric sequence {a n } given by a n = ar n−1 , the sum of its first n terms is ( a(r n − 1)/(r − 1) if r 6= 1, Sn = an if r = 1.
INTRODUCTION TO MATLAB
6
>> >>
∼ Moreover, we can find the sum to infinity, which is defined by
in
. Recall that
for the geometric sequence {a n } given by a n = ar n−1 , the sum to infinity is ( a/(1 − r ) if |r | < 1, S∞ = does not exist if |r | ≥ 1. >> >> >>
6. V ECTOR A vector a i + b j + c k or (a, b , c) can be defined as
or simply
.
The addition, subtraction and multiplication with numbers can be evaluated using and
respectively.
For example, let u = (1,2,3) and v = (4,5,6). >> >> >>
>>
>>
The dot product u • v of two vectors u and v is defined by >>
We can use the formula |v | = >>
p
v • v to find the norm of v :
.
,
INTRODUCTION TO MATLAB
Alternatively,
provides a command
7
for the norm of a vector.
>>
The cross product u × v is defined by
.
>>
7. F UNCTION 7.1. Standard Function. To define a function, we shall (i) give the name of the function, (ii) use to declare the name of the variable, (iii) provide the expression of the function. For example, define f (x) = x 2 : >>
Then f (2) can be evaluated by >>
Another example: g (x) = sin( x 3 )/x 2 >>
Multi-variable functions can be defined similarly by declaring more variables. For example, p h(x, y) = x 2 + y 2 : >>
To evaluate h(5,12): >>
7.2. Piecewise Function. The absolute value function f (x) = |x| is a piecewise function: ( x if x ≥ 0, f (x) = −x if x < 0, which can be defined in
:
>> >>
The general format is to use the >>
as
INTRODUCTION TO MATLAB
In the example above, the first condition is to 0”. The value
8
and this means “x is greater than or equal
is the expression of the function when the condition
defines f (x) = x when x ≥ 0. The second condition
is satisfied. It
and the second value
defines
f (x) = −x when x < 0.
In order to evaluate the value of the function at given point, for example, f (−2), we use >>
8. C URVE P LOTTING 8.1. Standard Function. Once a function is defined, a specified interval in the form
can be used to plot its graph over
. 2
For example, we plot f (x) = x on the interval (−100,100): >> >>
The command can be used even if the function is undefined somewhere on the specific interval. For example, g (x) = 1/x on (−1,1) is defined at x = 0. >> >>
INTRODUCTION TO MATLAB
9
8.2. Multiple Curves. In order to plot more curves in the same coordinate system, we use the commands
and
. For example,
f (x ) = sin x (We can use
and g ( x ) = cos t ,
−2π < x < 2π.
to start a new line without breaking the command.)
>> >> >> >> >> >>
We can draw multiple graphs on the same plot. white
, black
, blue
, red
, cyan
, green
provides some basic colour options: , magenta
, yellow
to distinguish
INTRODUCTION TO MATLAB
different graphs. For example, f (x ) = x 3 − x + 1, g (x) = x 4 − 3x 2 + x, h(x) = x 5 + 0.3x 4 − 2.8x 3 − 0.3x 2 + 1.8x . >> >> >> >> >> >> >> >>
8.3. Parametric Equations. A curve may be defined using a pair of parametric equations: x = x(t ) In order to use
and
y = y( t ).
, we shall first define the functions for the x- and y-coordinates.
Recall that a unit circle can be parameterized by x = cos t , >> >> >>
y = sin t ,
0 ≤ t ≤ 2π.
10
INTRODUCTION TO MATLAB
can draw a butterfly: >> >> >>
8.4. Piecewise Function. There are two ways to plot a piecewise function. Once a piecewise function is already defined,
f (x) = >> >> >>
(
ex
can directly plot its graph:
if x < 0,
cos x if x ≥ 0.
11
INTRODUCTION TO MATLAB
12
Alternatively, we can plot different branches on different intervals using the same colour. >> >> >> >> >> >>
8.5. Implicit Function. The command
can be used to plot graphs defined by im-
plicit functions, i.e., f (x, y) = 0. The format is: >>
For example, to plot x 3 + y 3 = 3x y, we shall first define the function f (x, y) = x 3 + y 3 − 3x y : >> >>
INTRODUCTION TO MATLAB
13
9. L IMITS The limit of a function describes the behavior of a function near a point or at infinity. The limit lim f (x) can be easily evaluated with the command x→a
>>
For example, lim p x→0
>>
x 1 + 3x − 1
:
:
>>
The one-sided limits can be evaluated by specifying the direction
or
. For ex-
ample, let the floor function ⌊x⌋ denote the greatest integer smaller or equal to x. Then lim ⌊x⌋ = 1,
x→2−
lim ⌊x ⌋x = 2,
x→2+
and
lim ⌊x⌋ does not exist. x→2
>> >>
>>
>>
We can find the limit at infinity >> >>
or negative infinity
³ a ´x . For example, lim 1 + : x→∞ x
INTRODUCTION TO MATLAB
14
Sometimes the limit depends on the value of the parameters. For example, 0 if a < 0, lim 2ax =
x→∞
We may use
1 if a = 0, ∞ if a > 0.
to declare the value of the parameter.
>> >> >>
>> >>
>> >>
10. D ERIVATIVE The derivative of a function f at point a is defined as the limit f ′ (x) = lim
h→0
f (x + h) − f (x) . h
For example, if f (x) = x 2 , then f ′ (x) = 2x : >> >> >>
On the other hand,
includes the command
find the derivative of f (x) = cos(x 2 ) with respect to x : >> >> >>
for differentiation. For example,
INTRODUCTION TO MATLAB
15
p x − 3x x with respect to x : p In order to differentiate g (x) = x >> >> >>
The answer seems complicated. Fortunately
provides the command
which
may help in this situation: >>
Suppose we are looking for f (4) (x), the 4th order derivative of f (x) with respect to 4, of course we may use >>
However, it may be a bit cumbersome. The following shorter commands have the same effect: >>
>>
For example, we can find the local extreme values of f (x) = x 3 − 3x 2 + x − 2. >> >> >> >>
(i) Solve f ′ (x) = 0 to obtain the stationary points. >>
(ii) Check the sign of f ′′ (x) at the stationary points. (Note that you can use in the scripts.) >>
and
INTRODUCTION TO MATLAB
16
>>
p p Hence, f has a local maximum at 1 − 6/3 and a local minimum at 1 + 6/3. We can plot f (x), f ′ (x) and f ′′ (x) in the same coordinate system. >> >> >> >> >>
11. I NTEG RAL 11.1. Indefinite Integral. For a function f (x), its indefinite integral is a function F (x) such that F ′ (x) = f (x), denoted by F (x) =
Z
f (x) d x.
All indefinite integrals differ by a constant only. So we also use Z F (x ) = f ( x ) d x + C to represent the entire family of indefinite integrals of f (x), where C is an arbitrary constant. In
, we can use the command to find an indefinite integral. Z 1 1 x d x = tan−1 x + For example, +C. 2 2 2 (1 + x ) 2(x + 1) 2 >> >> >>
INTRODUCTION TO MATLAB
17
Note that the constant C is dropped in the answer. Another example: Z 5 15 3 1 sin6 x d x = sin 6x + C . x− sin 2x + sin 4x − 192 16 64 64 >> >> >>
11.2. Definite Integral. The definite integral
Zb
f (x) d x represents the net area bounded be-
a
tween the graph of y = f (x) and the x-axis from x = a to x = b. The command
can also be
used to find definite integral by specifying the lower limit a and upper limit b in the form >>
For example,
Z1 0
>>
π 1 1 dx = + . 2 2 (1 + x ) 8 4
>> >>
Another example:
Zπ 0
sin6 x d x =
5 π. 16
>> >> >>
We can verify the fundamental theorem of calculus: Z d x f (t ) d x = f (x). dx a >> >>
It is also possible to integrate over an infinite interval by specifying the lower limit and/or the upper limit as
or
. For example, Z∞ p 2 e −x d x = π. −∞
INTRODUCTION TO MATLAB
18
>>
12. D IFFERENTIAL E QUATION 12.1. General Solution. In order to solve an ordinary differential equation of the form
dy = dx
f (x, y), we shall first declare that y is a function in x, and then use . dy = 1 + x + y. Note that the equal sign is represented by . For example, dx >> >>
Here
refers to an undermined constant. So the general solution of the given equation is x
y = C e − x − 2, where C is an arbitrary constant. Ordinary differential equations with higher order can be solved similarly. dy d2y +2 + 2y = e x . For example, 2 dx dx >> >>
The general solution is y = C 2 e −x sin x + C 3 e −x cos x, where C 2 ,C 3 are arbitrary constants. 12.2. Particular Solution. Suppose an initial condition is given. We can use >>
For example,
dy = 1 + x + y such that y = 1 at x = 0. dx
>> >>
For higher order differential equations, we need more initial conditions. We use >>
The condition y ′ (a) = b shall be defined as >>
For example, >>
if we set
dy d2y +8 − 9y = 0, where y(1) = 2 and y ′ (1) = 0. 2 dx dx
INTRODUCTION TO MATLAB
19
>> >>
>>
12.3. System of Differential Equations. The command
can also be used for a system
of differential equations. Note that the output consists multiple functions. So the output must be in vector form: >>
For example, the following linear system consists of two functions x(t ) and y(t ) with two equations and two initial conditions: (
x ′ (t ) = 3x + 4y
y ′ (t ) = −4x + 3y
where x(0) = 2 and
y(0) = 3.
>> >>
13. P LOT TING
IN THE
S PACE
13.1. Curve in the Space. A curve in the x y z-space can be parametrized as
r ( t ) = x ( t )i + y(t )j + z(t )k,
a ≤ t ≤ b.
Plotting a space curve in the x y z-space is similar to plotting a parametrized curve in the x y plane. The only difference is to replace
by
. For example,
r (t ) = (e −t/10 sin 5t ) i + (e −t/10 cos 5t ) j + t k. >> >> >> >>
INTRODUCTION TO MATLAB
20
13.2. Level Curves. A surface is defined as a two-variable function z = f (x, y). Its level curve at
z = c is the intersection of the surface with the horizontal plane at z = c; that is, f (x, y) = c. The command
plots the level curve of a two-variable function.
>>
For example, f (x, y) = sin x + cos y : >> >>
It is also possible plot the level curves of two surfaces using
and
13.3. Surface. A surface z = f (x, y) in two variables can be plotted in x y z-space using For example, f (x, y) = sin x + cos y . >> >>
.
.
INTRODUCTION TO MATLAB
13.4. Parametrized Surface. A surface can be parametrized by two parameters u and v :
r (u, v ) = x(u , v ) i + y(u, v ) j + z(u, v ) k. We can still use
to plot parametrized surface in the form
>>
For example,
r (u, v ) = u cos v i + u sin v j + v k. >> >> >> >>
21
INTRODUCTION TO MATLAB
22
13.5. Implicit Function. A surface can also be defined by an implicit function, i.e., f (x, y, z) = 0.
It is almost the same as plotting a curve defined by implicit function in the x y-plane. Simply use
instead of
.
For example, plot the sphere x 2 + y 2 + z 2 = 4z and the paraboloid z = x 2 + y 2 . >> >> >> >> >> >>
14. M ULTI -VARIABLE D ERIVATIVES 14.1. Partial Derivatives. For a multi-variable function, we can find its partial derivatives by specifying the variables to be differentiated with respect to properly. ∂f ∂f ∂f For example, for f (x, y, z) = x ln(x y 2 z 3 ), its partial derivatives are given by , and ∂z ∂x ∂y >> >> >>
>>
>>
INTRODUCTION TO MATLAB
The command
23
provides the gradient vector ∇ f of a function f so that we can
obtain all the derivatives at once (the output is a column vector). >>
We may verify the mixed derivative theorem:
∂2 f ∂y ∂x
=
∂2 f : ∂x ∂y
>>
>>
Recall that the directional derivative of f (x, y, z) along a vector v at (a,b,c) is given by ∇ f (a,b,c) • v /|v |. For example, at (1,2,3), the dire...