Title: Today we will learn MATLAB
1Today we will learn MATLAB
Click Start?All programm?Class Software? Matlab
This command window will be seen with a prompt
sign gtgt Any command can be written after the
prompt sign.
First let us play with matlab and see how it
works when you enter a command!!
2Type gtgt 2 2 and hit the return key. See the
answer? Now type gtgt a 2 2 and hit the return
key. The variable a now is assigned the value 2.
Type gtgt a and hit the return key. Now type gtgt
b 2 1 and hit the return key. Semi-colon
suppresses display of the output (later, youll
see how helpful that is in case the output is a
huge array of numbers). Computer now remembers
what b is. Type gtgt b and hit the return key. See
the answer? Now type gtgt clear and hit the return
key. This wipes out computer memory, which is
useful once in a while. Now type gtgt b and see
the error message. (These error messages are very
useful.)
3Arithmetic Operations
The operations and - are as usual addition and
subtraction. Try gtgt 2 -3 Operation is
multiplication. Try gtgt 32 Operation / is
division. Try gtgt 3/2 a to the power b is a b.
Try gtgt 3(-2) To make sure that operations are
done in proper sequence, use brackets. For
example, gtgt 3/21 means
, while gtgt 3/(21) means
.
.
4Type clc to clear the page.Note clc is not
same as clear
5for-end Loop
If you want to compute the sum
the easiest way to do it is gtgt Sum 0 for j
110 Sum Sum 1/j end
Note
Be careful to have spaces between commands.
Computer would understand forj as a single
unknown word.) Here, first, before summation
starts, you define Sum to be equal to 0. Then,
inside the loop, you add 1/j to old value of
Sum to get new value Sum 1/j, each time
increasing Sum by 1/j, where j changes from 1 to
10. Obviously, this is much more economical than
to type
gtgt 1 1/2 1/3 1/4 1/5 1/6 1/7 1/8
1/9 1/10
Type gtgtSum
6Use Upper cursor to type the last command use it
repeatedly to type the one before last,
etc. Vector variables Row vector (same as row
array) is a one-dimensional array of numbers. You
can define a vector by specifying its elements,
try, for example gtgt x 1,2,3,4,5 The same
vector can be defined much more elegantly if you
type gtgt x (15)
If you want just to define a vector without
displaying it on the screen, use semicolon (for
instance, x (1 5)) Imagine forgetting to
put semicolon after command x (1 5000) - try
it!
7Opoooos !!!!
?
8Very often you would want to define a linear
array with constant small increment, for example,
you want to introduce vector 1 1.1 1.2 1.3
1.4 1.5 Elegant way to do it is to type gtgt
x (10.11.5) If x is a vector
, then command gtgt x(k) where k is any number
from 1 to n, gives the number equal to the kth
element of vector x. Try gtgt x (150)
x(32) You have to be careful doing arithmetic
operations with vectors. For example, if you
want a vector, each element of which is a square
of the corresponding element of vector x, you
have to use the dot before the power
command. Try (incorrectly) gtgt x(14)
yx2 Now try (correctly) gtgt x(14)
yx.2 Similarly, to multiply or divide elements
of one vector by corresponding elements of
another vector of the same length, use . or ./
respectively. For example, trygtgt x(14)
y(58) ay.x, by./x You can multiply or
divide the vector by a number as usual try gtgt
x(14) yx/2, z2x
9(No Transcript)
10Special vectors
gtgt yzeros(size(14)) or gtgt x(14)
yzeros(size(x)) introduces the vector 0 0 0
0 gtgt yones(size(14)) or gtgt x(14)
yones(size(x)) introduces the vector 1 1 1 1
Mathematical functions Sin(x) cos(x) exp(x)
are Sin, Cos and exponential functions. log(x)
is natural logarithm function with base e.
abs(x) is absolute value of x. sign(x) is equal
to 1 if x is positive, -1 if x is negative, and
0 if x 0. sqrt(x) is square root of x.
11Try gtgt sin(pi/2) gtgt exp(1) gtgt log(exp(2)) gtgt
abs(-2) gtgt sign(-2) gtgt sqrt(4)
12Plots To plot a graph function b(t), where both
b and t are vectors of the same size, use
command plot(t,b) Try gtgt t (110) b sin(t)
plot(t,b) If you do not want the points on the
graph to be connected by lines, try gtgt t
(110) b sin(t) plot(t,b,'') To make a graph
of different color (for example, red), try gtgt t
(110) b sin(t) plot(t,b,'r') Now, try gtgt t
(1 0.110) b sin(t) plot(t,b) See the
difference ?
13gtgt t (110) b sin(t) plot(t,b,'r')
gtgt t (1 0.110) b sin(t) plot(t,b)
14If statement. Try to type- gtgt a 1 if (a
2 a gt 3) b 1 else b 2 end Then type b.
Here you first assigned value 1 to the variable
a. Then, you told to computer that if either a is
equal to 2, or a is more than 3, then the
variable b is assigned value 1, otherwise b 2.
Then you checked that b 2, indeed, because a
1. More complicated if statement c 1 if c lt
-1 d 1 else if c lt 2 d 2 else d 3
end This is self-explanatory. (Note that there
should be a space between else and if.) Type d
and see what it equals to. Pay attention how
semicolon and end are used.
15If you have any doubt You can use command gtgt
help if you are in trouble or if you need to find
out more. For example, try gtgt help plot
16Now we can study a few models
Logistic Growth Model
To simulate just type
gtgt tend20 r0.1 t(1tend) x0.5ones(size(t))
gtgt for j1tend-1 if x(j)lt1 x(j1)rx(j)(1-x(j
)) else x(j1)0 end, end, plot(t,x)
r0.1x00.5
17Just change the value of r, with x0.5
R2.1
R0.8
R3.5
18Now try for for all r0.1 to r1 with increment
0.2
We have used a loop
for r.10.21 tend100 t(1tend)
x0.5ones(size(t)) for j1tend-1 if x(j)lt1
x(j1)rx(j)(1-x(j)) else x(j1)0 end, end,
plot(t,x),hold on end
With hold on command we can plot for all values
of r. Otherwise you can change r and see the
graph.
19What happens when 1 lt r lt 2 ?
gtgt for r10.22 tend100 t(1tend)
x0.5ones(size(t)) for j1tend-1 if x(j)lt1
x(j1)rx(j)(1-x(j)) else x(j1)0 end, end,
plot(t,x),hold on end
20What happens when 2 lt r lt 3 ?
gtgt for r20.23 tend100 t(1tend)
x0.5ones(size(t)) for j1tend-1 if x(j)lt1
x(j1)rx(j)(1-x(j)) else x(j1)0 end, end,
plot(t,x),hold on end
21What happens when 3lt r lt 4 ?
X0.5
22Now try different initial conditions
r3.95 x1(1)0.3 x2(1)0.33
r3.4 x1(1)0.3 x2(1)0.33
23Ricker Model of Fish population
The Ricker model is a classic discrete population
model which gives the expected number x t1 (or
density) of individuals in generation t 1 as a
function of the number of individuals in the
previous generation,
x(j1)rx(j)exp(-x(j))-h
Type this to simulate
gtgt tend50 r1 h0 t(1tend)
x0.5ones(size(t)) gtgt for j1tend-1
x(j1)rx(j)exp(-x(j))-h end, plot(t,x)
24All the plot for h0
R0.5
R1
R7
R10
25All the plot for h0
R20
R14
R30
26Now start Fishing increase h from 0 to .15
tend50 r2 h0 t(1tend)
x0.5ones(size(t)) for j1tend-1
x(j1)rx(j)exp(-x(j))-h if x(j1)gt0
x(j1)x(j1) else x(j1)0 end, end, plot(t,x)
r2, h0.15
r2, h0
27Increase r and h
r7 h1.6
r7 h1.7
r10 h2.4
r10 h2.0
28Different initial condition x1.5
r10 h2.4
r10 h2
r10 h2.7
r10 h2.72
29tend100 h0.1r20 t(0tend)
x2ones(size(t)) for j1tend-1
x(j1)rx(j)(exp(-x(j)))-h end plot(t,x)
Here r20, h0.1
Even tiny harvest is ruinous if population in
some years is fragile.
30Smart strategy skip dangerous years when
population is low
tend100 h2.6r10 t(0tend)
x1.65ones(size(t)) for j1tend-1 if x(j1)gt0
x(j1)x(j1) else x(j1)x(j1)h
end x(j1)rx(j)(exp(-x(j)))-h end plot(t,x)
31Heart Model
- If electric potential , the heart did
not recover it skips the beat and the potential
does not change - 2) If electric potential , the heart
recovered - it beats and the potential increases
- 3) Between consecutive signals, the potential
decreases - by the factor
32Type these 2 lines in command window to simulate
gt tend20 c0.3 t(1tend) x0.5ones(size(t))
gtgt for j1tend-1 if cx(j)gt1 x(j1)cx(j)
else x(j1)cx(j)1 end, end, plot(t,x)
Which means, x increases if it is less than 1/c
and decreases if it is more than 1/c
33Now increase c from 0.3 till 1.1 with increment
0.1.
C0.3
C0.4
C0.5
C0.6
Interpret the results for c0.6 in words.
34Explanation of the result for c0.6
x increases if it is less than 1/c and decreases
if it is more than 1/c
1/c
35C0.7
C0.8
C0.9
C1.0
36C1.1