Download presentation
Presentation is loading. Please wait.
Published byAshlyn Griffin Modified over 9 years ago
1
Today we will learn MATLAB Click Start All programm Class Software Matlab This command window will be seen with a prompt sign >> 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!!
2
Type: >> 2 + 2 and hit the return key. See the answer? Now type >> a = 2 + 2 and hit the return key. The variable a now is assigned the value 2. Type >> a and hit the return key. Now type >> b = 2 + 1; and hit the return key. Semi-colon suppresses display of the output (later, you’ll see how helpful that is in case the output is a huge array of numbers). Computer now remembers what b is. Type >> b and hit the return key. See the answer? Now type >> clear and hit the return key. This wipes out computer memory, which is useful once in a while. Now type >> b and see the error message. (These error messages are very useful.)
3
Arithmetic Operations :. The operations + and - are as usual addition and subtraction. Try >> 2 -3 Operation * is multiplication. Try >> 3*2 Operation / is division. Try >> 3/2 a to the power b is a^ b. Try >> 3^(-2) To make sure that operations are done in proper sequence, use brackets. For example, >> 3/2+1 means, while >> 3/(2+1) means.
4
Type „clc“ to clear the page.Note „clc“ is not same as „clear“
5
for-end Loop If you want to compute the sum the easiest way to do it is: >> Sum = 0; for j = 1:10 Sum = Sum + 1/j; end 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 Note: >> 1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8 + 1/9 + 1/10 Type >>Sum
6
Use 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: >> x = [1,2,3,4,5] The same vector can be defined much more elegantly if you type: >> x = (1:5) 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!
7
Opoooos !!!!
8
Very 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: >> x = (1:0.1:1.5) If x is a vector, then command >> x(k) where k is any number from 1 to n, gives the number equal to the k th element of vector x. Try: >> x = (1:50); 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): >> x=(1:4); y=x^2 Now try (correctly): >> x=(1:4); y=x.^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, try:>> x=(1:4); y=(5:8); a=y.*x, b=y./x You can multiply or divide the vector by a number as usual; try >> x=(1:4); y=x/2, z=2*x
10
Special vectors: >> y=zeros(size(1:4)) or >> x=(1:4); y=zeros(size(x)) introduces the vector [0 0 0 0] >> y=ones(size(1:4)) or >> x=(1:4); y=ones(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.
11
Try: >> sin(pi/2) >> exp(1) >> log(exp(2)) >> abs(-2) >> sign(-2) >> sqrt(4)
12
Plots: To plot a graph function b(t), where both b and t are vectors of the same size, use command plot(t,b) Try >> t = (1:10); b = sin(t); plot(t,b) If you do not want the points on the graph to be connected by lines, try: >> t = (1:10); b = sin(t); plot(t,b,'*') To make a graph of different color (for example, red), try >> t = (1:10); b = sin(t); plot(t,b,'r*') Now, try: >> t = (1 :0.1:10); b = sin(t); plot(t,b) See the difference ?
13
>> t = (1 :0.1:10); b = sin(t); plot(t,b) >> t = (1:10); b = sin(t); plot(t,b,'r*')
14
If statement. Try to type- >> a = 1; if (a = = 2 || a > 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 < -1 d = 1; else if c < 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.
15
If you have any doubt : You can use command >> help if you are in trouble or if you need to find out more. For example, try >> help plot
16
Now we can study a few models Logistic Growth Model: >> tend=20; r=0.1; t=(1:tend); x=0.5*ones(size(t)); >> for j=1:tend-1 if x(j)<1 x(j+1)=r*x(j)*(1-x(j)); else x(j+1)=0; end, end, plot(t,x) To simulate just type: r=0.1;x0=0.5
17
R=2.1 R=3.5 R=0.8 Just change the value of r, with x=0.5
18
Now try for for all r=0.1 to r=1 with increment 0.2 for r=.1:0.2:1 tend=100; t=(1:tend); x=0.5*ones(size(t)); for j=1:tend-1 if x(j)<1 x(j+1)=r*x(j)*(1-x(j)); else x(j+1)=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. We have used a loop
19
What happens when 1 < r < 2 ? >> for r=1:0.2:2 tend=100; t=(1:tend); x=0.5*ones(size(t)); for j=1:tend-1 if x(j)<1 x(j+1)=r*x(j)*(1-x(j)); else x(j+1)=0; end, end, plot(t,x),hold on end
20
What happens when 2 < r < 3 ? >> for r=2:0.2:3 tend=100; t=(1:tend); x=0.5*ones(size(t)); for j=1:tend-1 if x(j)<1 x(j+1)=r*x(j)*(1-x(j)); else x(j+1)=0; end, end, plot(t,x),hold on end
21
What happens when 3< r < 4 ? X=0.5
22
r=3.95; x1(1)=0.3; x2(1)=0.33 r=3.4; x1(1)=0.3; x2(1)=0.33; Now try different initial conditions
23
Ricker Model of Fish population >> tend=50; r=1; h=0; t=(1:tend); x=0.5*ones(size(t)); >> for j=1:tend-1 x(j+1)=r*x(j)*exp(-x(j))-h; end, plot(t,x) The Ricker model is a classic discrete population model which gives the expected number x t+1 (or density) of individuals in generation t + 1 as a function of the number of individuals in the previous generation, x(j+1)=r*x(j)*exp(-x(j))-h; Type this to simulate
24
R=1 R=0.5 R=7 R=10 All the plot for h=0
25
R=14 R=20 R=30 All the plot for h=0
26
Now start Fishing: increase h from 0 to.15 tend=50; r=2; h=0; t=(1:tend); x=0.5*ones(size(t)); for j=1:tend-1 x(j+1)=r*x(j)*exp(-x(j))-h; if x(j+1)>0 x(j+1)=x(j+1); else x(j+1)=0; end, end, plot(t,x) r=2, h=0 r=2, h=0.15
27
r=7; h=1.6r=7; h=1.7 r=10; h=2.4r=10; h=2.0 Increase r and h
28
Different initial condition x=1.5 r=10; h=2 r=10; h=2.4 r=10; h=2.7 r=10; h=2.72
29
tend=100; h=0.1;r=20; t=(0:tend); x=2*ones(size(t)); for j=1:tend-1 x(j+1)=r*x(j)*(exp(-x(j)))-h; end plot(t,x) Here r=20, h=0.1 Even tiny harvest is ruinous if population in some years is fragile.
30
Smart strategy: skip dangerous years when population is low tend=100; h=2.6;r=10; t=(0:tend); x=1.65*ones(size(t)); for j=1:tend-1 if x(j+1)>0 x(j+1)=x(j+1); else x(j+1)=x(j+1)+h; end x(j+1)=r*x(j)*(exp(-x(j)))-h; end plot(t,x)
31
1)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 : Heart Model
32
> tend=20; c=0.3; t=(1:tend); x=0.5*ones(size(t)); >> for j=1:tend-1 if c*x(j)>1 x(j+1)=c*x(j); else x(j+1)=c*x(j)+1; end, end, plot(t,x) Type these 2 lines in command window to simulate: Which means, x increases if it is less than 1/c and decreases if it is more than 1/c
33
Now increase c from 0.3 till 1.1 with increment 0.1. C=0.3 C=0.4 C=0.5 C=0.6 Interpret the results for c=0.6 in words.
34
x increases if it is less than 1/c and decreases if it is more than 1/c Explanation of the result for c=0.6 1/c
35
C=0.7 C=0.8 C=0.9 C=1.0
36
C=1.1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.