Document

Unit 1
1
Matlab/GNU Octave
http://www.maths.dundee.ac.uk/~ftp/na-reports/MatlabNotes.pdf
http://www.octave.org
2
The first helpful command is the command help.
Try and type:
help help
help
3
Matlab/GNU Octave
as a calculator
+
*
/
^
4
Matlab/GNU Octave
as a calculator
perform a number of basic calculations.
Choose them at your taste.
Here are just some examples:
>> ...
>> 4+5/12
>> (4+5)/12
>> 1.278e-2 + 0.23
>> 1.5^5.8
>> -2^2
>> 2*pi
>> 103^2.4+4*3.7-1.2/4^1.2
>> ..
5
Matlab/GNU Octave
as a calculator
familiarize with the various formats.
2
For instance, let us get the number e or
whatever number is popping up to your mind.
format
format
format
format
format
Type
short
long
short e E
long e E
bank
help format
for a full list
6
Variables
> x=3
> y=x*5
y=15
any combinations of letters and digits can be a name for a
variable: it must begin with a letter though.
The following are special names (pre-defined variable) and
must be avoided:
ans
pi = 3.141592654... = π
e
= 2.718281828... = Euler's number
eps= 2.22044...e-16 = 2-54
Usually i and j are used for the imaginary unit
−1
7
Variables
Re-make the previous calculations using variable this
time; e.g.:
>>
>>
>>
>>
>>
>>
>>
a=4
b=5
c=12
a+b/c
ans = 4.416666...67
(a+b)/c
ans = 0.75
8
Built-in Elementary Functions
sin
cos
tan
exp
log
sqrt
...
9
Make some calculations using
built-in elementary functions
sin(pi/3)
cos(pi/4)
tan(pi/4)
exp(e)
log(e^2)
sqrt(121)
...
10
Define variables that convert from degrees to radiants
and viceversa
deg2rad
rad2deg
They allow you to use trigonometric functions with
angles given in degrees. Check that, e.g.:
cos(60*deg2rad) = 0.5
atan(1.0)*rad2deg = 45
11
Vectors
ROW
COLUMN
>> a = [1.2 2.4 3.6]
>> b=[1.2;2.4;3.6]
>> a = [1.2,2.4,3.6]
>> b=[1.2
2.4
3.6]
Transposing
>> a'
>> b'
12
Vectors of equally spaced values
>> k = [ 0.1:0.3:1.9]
>> x = linspace(xmin, xmax, n)
>> help linspace
>> x = logspace(log10(xmin),
log10(xmax), n)
>> help logspace
13
Operations on a vector
>> k(2)
>> 3*k
>> k-sqrt(3)
>> length(k)
>> norm(k)
>> sort(k)
>> sin(k)
>> ...
14
Operations between vectors
+
*
dot
cross
.*
./
.^
15
Let us try this:
You may know that:
e=lim x ∞
 
1
1
x
x
Find an estimate of e and compare it to the built-in
value.
16
Plotting Elementary Functions
>> n=10; h=1/n; x=[-pi:h:pi]
>> y=sin(x)
>> plot (x,y, 'k+')
>> title ('graph of y=sin(x)')
>> xlabel('x')
>> ylabel('y')
17
18
Plotting Elementary Functions
Type help plot to learn more on plotting
Plot the function cos(x) with the abscissae in
degrees
Plot some other functions, e.g. exp(x), log(x),
log7(x), sqrt(x2+1) ...
19
Plotting Elementary Functions
Power laws, e.g. y=xk
>> x=logspace(log10(1), log10(1000), 100)
>> plot(log10(x), log10(x.^3), 'o')
>> loglog(x,x.^3,'+')
Can you comment on the differences between plot and
loglog ?
20
Plotting Elementary Functions
Colors and Lines
Symbol
y
Color
Line Style
yellow
Symbol
.
m
magenta
o
circle
c
cyan
x
x
r
red
+
plus
g
green
-
solid
b
blue
*
star
w
white
:
dotted
k
black
-.
dashdot
--
dashed
point
21
Plotting Elementary Functions
>> grid
>> axis
>> hold on / hold off
>> figure()
>> print -dDEVICE NAMEFILE
22
Plotting Elementary Functions
Plot the function f(x)=x2exp(-x) using both
solid line and symbols.
Add the axis labels.
23
24
Plotting Elementary Functions
subplot, e.g.:
>>
>>
>>
>>
>>
>>
>>
>>
>>
x=linspace(0,2*pi,100)
y=sin(x)
y2=y.^2
y3=y.^3
y4=y.^4
subplot(2,2,1), plot(x,y)
subplot(2,2,2), plot(x,y2)
subplot(2,2,3), plot(x,y3)
subplot(2,2,4), plot(x,y4)
25
26
Plotting Elementary Functions
Plot the following functions, adding axis labels and
titles, in the interval 0 < x < 10
1
u  x =
x
2
 x−1
1
3
w  x =
 10−x  −2
1
2 2
 4− x 
2
x 1
v  x= 2
x −4
sin x 
y  x =
x
27
Scripts or m-files
p2.m
%Example on plotting
%Plotting the second Legendre polynomial
%in the interval [0;1]
%
x=linspace(0,1,100)
p=1.5*x.*x - 0.5
plot(x,p,'k-')
xlabel('x')
ylabel('P_2 (x)')
title('the second Legendre polynomial')
28
Scripts or m-files
Write a script evaluating and plotting the following
function in the interval [0;1]:
3
3
1
1− x x²− x³
5
20
60
y  x =
2
1
1 x  x²
5
20
and compare it graphically to the function exp(-x).
29
Elementary
Numerical Derivation
30
Analitical and Numerical Derivative: the tangent and the secant
31
Numerical Derivatives:
first-order approximation
f  x x − f  x 
f  x ≈
x
'
32
Numerical Derivatives:
first-order approximation
% f(x)=sqrt(x) defined in the interval [a,b]
clear all
a=0; b=10; N=10;
x=linspace(a ,b, N);
f=sqrt(x);
il=(1:1:N-1); ir=(2:1:N);
deltax=x(ir)-x(il);
deltaf=f(ir)-f(il);
fp=deltaf./deltax;
plot(x(il),fp, 'bo', x(ir), fp, 'bx', x(il)
+deltax/2, fp, 'bs', x, 0.5./sqrt(x),'k-')
xlabel('x')
ylabel('fp(x)')
33
Numerical Derivatives:
second-order approximation
f  x x − f  x− x
f  x ≈
2 x
'
34
Numerical Derivatives:
second-order approximation
% f(x)=sqrt(x) defined in the interval [a,b]
clear all
a=0; b=10; N=10;
x=linspace(a ,b, N);
f=sqrt(x);
il=(1:1:N-2); ir=(3:1:N);
deltax=x(ir)-x(il);
deltaf=f(ir)-f(il);
fp=deltaf./deltax;
plot(x(il),fp, 'ro', x(ir), fp, 'rx', x(il)
+deltax/2, fp, 'rs', x, 0.5./sqrt(x),'k-')
xlabel('x')
ylabel('fp(x)')
35
Numerical Derivatives:
first and second-order approximation
Write a script (m-file) evaluating and plotting the
derivative of the function:
−x
g  x=e
2
Compare to the exact derivative
36
Elementary
Numerical Integration
37
Numerical Integration
why is it necessary ?
often not easy to find
primitive functions;
e.g.:

2
0
E  k =∫
 1−k sin  d 
2
2
Analytical Integration
seldom possible
often only some points of
a function are given
38
Numerical Integration
In general:
b
m
l
s=∫ f  x  dx≈ ∑ ∑ w ij f
a
i=0 j =0
i 
 a ij 
39
Numerical Integration
In particular, elementary formulae:
b
l
s=∫ f  x  dx≈ ∑ w j f  a j 
a
j=0
40
Elementary Formulae
in one dimension
b
k
x i1
∫ f  x  dx=∑ ∫ f  x  dx
a
i=0 x i
41
Elementary Formulae
in one dimension
x i1
∫
xi
1
hi
f  x  dx= ∫ i  r  dr
2 −1
hi =x i1− xi
2 x−x i 1 −x i
r=
hi
x i x i1hi r
i  r  = f
2


42
Elementary Formulae
in one dimension
l
i  r  ≈∑ i   j  L j  r 
j =0
1
1
 j = ∫ L j  r  dr
2 −1
1
l
∫ i  r  dr≈2 ∑  j i   j 
−1
-1
j=0
1
43
Elementary Formulae
in one dimension
x i1
∫
xi
l
f  x dx≈hi ∑  j f  x ij 
j=0
x i x i1h i  j
x ij =
2
44
Elementary Formulae
in one dimension
RECTANGLE METHOD, l =0 :
1
∫ i  r  dr≈2 i  0 
−1
45
Elementary Formulae
in one dimension
TRAPEZOID METHOD, l =1:
1
 0= 1=
2
1
∫ i  r  dr≈i −1 i  1 
−1
46
Elementary Formulae
in one dimension
SIMPSON'S METHOD, l =2 :
1
2
 0= 2= ; 1=
6
3
1
1
∫ i  r  dr≈ 3 [ i −1 4 i  0 i  1 ]
−1
47
Elementary Formulae
in one dimension
b
∫ f  x  dx =
a
k
x i1
k −1
l
= ∑ ∫ f  x  dx≈∑ hi ∑  j f  x ij 
i =0 x i
i=0
j =0
48
Elementary Formulae
in one dimension
Trapezoid formula
[
T h=h f
a 
2
 f  ah   f  a2 h  ... f  b−h   f
b
2
]
Simpson's formula
h
S  h= [ f  a  4 f  ah  2 f  a2 h  ...2 f  b−2 h  4  b−h   f  b  ]
3
49
Elementary Formulae
in one dimension

1 
I =∫ e cos  x =−  e 1 
2
0

h=
n
x
50
Elementary Formulae
in one dimension
1
2
I =∫  x=
3
0

h=
n
51
Elementary Formulae
in one dimension
Method
Error
Rectangle
Trapezoid
~f (ξ)(b-a)h
(2)
2
~f (ξ)(b-a)h
Simpson's
~f (ξ)(b-a)h
(1)
(4)
4
52
Elementary Formulae
in one dimension
Last but not least … Romberg's Method:
Trapezoid and Simpson's together going beyond both.
b
∫ f  x  dx≈T  h 
a
n −1
T  h  =h ∑ f  x i 
i=1
h
b−a
f
x

f
x
,
x
=ai
h
,
h=
 0  n] i
2[
n
 
b−a
T m ,0 =T
, m≥0
m
2
T m ,n 1 =
4
n 1
T m ,n −T m−1, n
4
n 1
−1
,
n=0,1, ... , m−1
b
T m , n=∫ f  x  dxO  h
a
2n2
,
b−a
h= m
2
53
Elementary Formulae
in one dimension
Romberg's Method Performance
in the case

∫ e x cos  x 
0
m/n
0
1
2
3
4
0
1
2
4
22.708
5.318
-0.477
1.265
-5.926e-02
0.311
-0.8540e02
-6.134e-03
-8.497e-04
7.724e-05
7.766e-02
-3.919e-03
-9.536e-06
3.814e-06
TRAPEZOID
SIMPSON'S
3.814e-06
54
Elementary Formulae
in one dimension
Making use of the trapezoid rule, find an estimate of
the integral:
2
∫0  x dx
and compare it to the exact value. Remember that:
[
T h=h f
a 
2
 f  ah   f  a2 h  ... f  b−h   f
b
2
]
55
%Evaluate int_0^2 sqrt(x) dx
%using Trapezoid method
%
clear all;
x=linspace(0,2,100);
f=sqrt(x);
h=x(2)-x(1);
int=h*(sum(f)-0.5*(f(1)+f(100)))
exact=2^(1.5)*2/3
err=100*(int-exact)/exact
56
Elementary Formulae
in one dimension
Making use of the Simpson's rule, find an estimate of
the integral:

∫0 sin
2
 x dx
and compare it to the exact value. Remember that:
h
S  h= [ f  a  4 f  ah  2 f  a2 h  ...2 f  b−2 h  4  b−h   f  b  ]
3
57
%Evaluate int_0^pi sin^2(x) dx
%using Simpson's method
%
clear all;
x=linspace(0,pi,101);
y=sin(x).*sin(x);
h=x(2)-x(1);
il=(2:2:100);
ir=(3:2:99);
y(il)=4*y(il);
y(ir)=2*y(ir);
int=h*sum(y)/3
exact=pi/2
err=100*(int-exact)/exact
58
Elementary Formulae
in one dimension
Evaluate the following integral:

2
0
E  k =∫
 1−k
2
2
sin  d 
for several values of k and then plot the function
E(k).
59
Elementary Formulae
in one dimension
Consider the following function:
x
F  x =∫0 exp −t  cos3 t dt
2
in the interval [0:2π]. How to evaluate and plot it ?
60
Elementary Formulae
in one dimension
Consider first the integrand and its graph:
2
f t =exp−t cos 3t 
61
Elementary Formulae
in one dimension
2
f t =exp−t cos 3t 
62
63
%Evaluate cumulatively
%int_0^2*pi exp(-x^2)*cos(3x) dx
%rectangle method
%
clear all;
x=linspace(0,2*pi,501);
h=x(2)-x(1);
il=(1:1:500);
ir=(2:1:501);
xm=(x(il)+x(ir))/2;
y=exp(-xm.^2).*cos(3*xm);
cumint=h*cumsum(y);
plot(xm,cumint)
64
Elementary Formulae
in one dimension
Try now to evaluate and then plot the following
integral function:
x
F  x =∫0
sint 
dt
t
in the intervals [0:2π] ; [0:4π] ; [0:6π] etc.... Doing this,
would you be able to guess what is the value of the
integral:
∞
I =∫0
sin t 
dt ?
t
65
%Evaluate cumulatively int_0^2*pi sin(x)/x dx
%
clear all;
x=linspace(0,2*pi,501);
h=x(2)-x(1);
y=sin(x)./x;
y(1)=1;
%plotting the integrand
figure(1);
plot(x,y)
%rectangle method
il=(1:1:500);
ir=(2:1:501);
xm=0.5*(x(il)+x(ir));
ym=sin(xm)./xm;
cumint=h*cumsum(ym);
figure(2);
plot(xm,(cumint-pi/2),'k-')
axis([0 2*pi -pi/2 pi])
hold on
%trapezoid method
a=y(1);
cumintrap=h*(cumsum(y)-0.5*(a+y));
figure(2);
plot(x,(cumintrap-pi/2),'r-')
hold off
%difference between rectangle and trapezoid methods
figure(3);
cumintnew(ir)=cumint;
cumintnew(1)=0.;
plot(x,(cumintnew-cumintrap),'k-','linewidth',3)
66
Physical application:
Trajectories of a particle
Example 1:
Suppose you have a particle that in the time interval T
= [0:10] travels along the x and y axis according to
these equations:
x t =x 0exp −kt 
y t =A cos t 
After having chosen a value for the parameters x0,
k, A and ω, draw a graph of its trajectory in the
plane (x,y) and calculate (numerically though) first
derivatives (the velocities) and second derivatives
(the accelerations).
67
Physical application:
Trajectories of a particle
Example 2:
Suppose you have a particle whose velocity, in the
time interval T = [0:10], is given by:
v x t=v 0 kt
v y t =A cos t
After having chosen a value for the parameters v0,
k, A and ω, draw a graph of its trajectory in the
plane (x,y).
68