math 15 lecture 12 university of california, merced scilab programming – no. 3
TRANSCRIPT
Math 15 Lecture 12Math 15 Lecture 12University of California, MercedUniversity of California, Merced
ScilabProgramming – No. 3
Course Lecture ScheduleWeek Date Concepts Project Due
1
2 January 28 Introduction to the data analysis
3 February 4 Excel #1 – General Techniques
4 February 11 Excel #2 – Plotting Graphs/Charts Quiz #1
5 February 18 Holiday
6 February 25 Excel #3 – Statistical Analysis Quiz #2
7 March 3 Excel #4 – Regression Analysis
8 March 10 Excel #5 – Interactive Programming Quiz #3
9 March 17 Introduction to SCILAB - Part - I
March 24 Spring Recesses
10 March 31 Introduction to SCILAB - Part - II (4/4) Project #1
11 April 7 Introduction to SCILAB - Part - III Quiz #4
12 April 14 Programming – #1
13 April 21 Programming – #2 Quiz #5
14 April 28 Programming – #3
15 May 5 Programming - #4 Quiz #6
16 May 12 Movies / Evaluations Project #2
Final May 19 Final Examination (3-6pm COB 116)
Project #2 – Due May 12th
Projects can be performed individually or in groups of three, with following rules:A team consists of at most 3 people—no copying between
teams! Teams turn in one project report and get the same grade.Team project report must include a title page, where a team
describe each team member’s contribution.10% bonus for labs done individually
Individual projects must not be copied from anyone else
Scilab
Materials will be available on April 30th.
Math 15 Final – May 19 (COB 116)
100 pts. total2 hours (3pm – 5pm)50 questions (2 pts. each)
10% from 1st lecture40% Excel Related50% Scilab Related
Mostly Multiple choices and fill-in-blanksSimilar to quizzes.One problem will ask you to make a small
programmingOpen notes (Max. 5 sheets)
But No computers will be allowed.
12%60Computer Labs
20%100In-Class Quizzes
20%100Assignments
100%500Total
20%100Final Exam
14%70Project #2
14%70Project #1
% Final Grade
PointsActivity
12%60Computer Labs
20%100In-Class Quizzes
20%100Assignments
100%500Total
20%100Final Exam
14%70Project #2
14%70Project #1
% Final Grade
PointsActivity
Grading for Math 15
Activity Points
Assignments 120
In-Class Quizzes 120
Computer Labs 70
Project #1 70
Project #2 70
Final Exam 100
Total 550
Projected points
+~45 extra points (by optional hw.)Over 275D
Over 325C
Over 375B
Over 425A
Total points achieved
Grade
Over 275D
Over 325C
Over 375B
Over 425A
Total points achieved
Grade
Any Questions?
Outline
Today – more programming
1.More for-loop
2.Logical expressions
if statement
7
Review:Let’s calculate populations of A and B species
at t = 156 if the population growths of A and B are following this system of equations:
given that A = 10 and B = 5 at t = 0.
We can describe this system of difference equations in terms of matrices.
ttt
ttt
BAB
BAA
9.01.0
1.09.0
1
1
8
Here:Let’s use the matrix to solve this system of
equations:
For t = n,
ttt
ttt
BAB
BAA
9.01.0
1.09.0
1
1
9
t
t
t
t
B
A
B
A
9.01.0
1.09.0
1
1
0
0
9.01.0
1.09.0
B
A
B
An
n
n
Transition Matrix
Initial Conditions
Now you know how to program in Scilab.
A system of two linear difference equations
10
T =[0.9 0.1; 0.1 0.9]; // Transition matrixP0=[10;5]; // Initial Conditions
Pt = P0;t = [0];for n=1:156 Pt = [Pt,T^n*P0]; t = [t,n];end
plot(t,Pt(1,:),'-o',t,Pt(2,:),'-s')
ttt
ttt
QPQ
QPP
9.01.0
1.09.0
1
1
In addition to the previous example:
Including lab 9 and HW #8
We are only dealing with linear equations. In many cases, a prediction from the linear equation cannot really be accurate for very long. So what’s next?
t
tttt
P
PdfPdfPP
)1(1 Lecture 10
Dolphin training problem Lecture 11
Now, what if a population is described by the following equation:
These is not a linear difference equation, so you cannot define the simple matrix.
How can we solve this equation?
12
K
PrPP t
tt 111
where K and r are positive.You may see this equation in Bis 1 or/and Bis 180.
Logistic model
The parameter K and r in this model have direct biological interpretations:If P<K, the population will increase.If P>K, the population will decrease.
K is called the carrying capacity of the environment, because it represents the maximum number of individuals that can be supported over a long period.
13
K
PrPP t
tt 111
Logistic model – cont.
If P<<K,
14
K
PrPP t
tt 111
0K
Pt rPK
PrPP t
ttt
1111
The model becomes a simple population model.
tt PP 1
UC Merced
Any Questions?
Let’s program this logistic model.
If P00 = 2, let’s program to graph how the population of this model change.
16
K
PrPP t
tt 111
where K = 100 and r = 0.7
Let’s program this discrete logistic model – cont.
17
2100
17.01 01
P
PPP t
tt
P=2; // Initial Condition
Pt = [P]; // Set up a new array, Pt, for populationst = [0]; // Set up a new array, t, for generations
for n=1:15 // Up to 15 generation P = P * (1 + 0.7*(1.0 – P/100)); // discrete logistic model Pt = [Pt P]; // Expanding the array, Pt. t = [t,n]; // Expanding the array, t.end
plot(t,Pt,'-o')
Let’s program this discrete logistic model – cont.
18
2100
17.01 01
P
PPP t
tt
P=2; // Initial Condition
Pt = [P]; // Set up a new array, Pt, for populationst = [0]; // Set up a new array, t, for generations
for n=1:15 // Up to 15 generation P = P * (1 + 0.7 *(1.0 – P/100)); // discrete logistic model Pt = [Pt P]; // Expanding the array, Pt. t = [t,n]; // Expanding the array, t.end
plot(t,Pt,'-o')
0 5 10 150
10
20
30
40
50
60
70
80
90
100
Generation
Po
pu
latio
ns
Review: for loop statementfor loop is used to repeat a command or a
group of commands for a fixed number of times.
The Basic Structurefor variable = starting_value: increment: ending_value
commands
end
The loop will be executed a fixed number of times specified by (starting_value: increment:
ending_value) or the number of elements in the array variable.
UC Merced
Any Questions?
Now, what if two populations are described by the following equations:
Again, these are not linear difference equations, so you cannot define the simple matrix.
How can we solve this system of nonlinear difference equations?
Well, we can do the same!
21
tttt
ttttt
QPQQ
QPPPP
6.14.0
5.013.11
1
1
where Initial Conditions, P0 = 1.1 and Q0 = 0.5
Let’s program this nonlinear population model.
22
//Initial ConditionsP_current=1.1;Q_current=0.5;
// Set up a new array, Pt, for the population, PPt = [P_current];// Set up a new array, Qt, for the population, QQt = [Q_current];
t = [0]; // Set up a new array, t, for generations
for n=1:15 // Up to 15 generation P_next = P_current * (1 + 1.3 *(1.0 - P_current))-0.5*P_current*Q_current; Q_next = 0.4*Q_current+1.6*P_current*Q_current; Pt = [Pt P_next]; // Expanding the array, Pt. Qt = [Qt Q_next]; // Expanding the array, Qt t = [t,n]; // Expanding the array, t.end
plot(t,Pt,'-o',t,Qt,'-s')legend('P','Q')
5.0
1.1
6.14.0
5.013.11
0
0
1
1
Q
P
QPQQ
QPPPP
tttt
ttttt
Let’s program this nonlinear population model – cont.
//Initial ConditionsP_current=1.1;Q_current=0.5;
// Set up a new array, Pt, for the population, PPt = [P_current];// Set up a new array, Qt, for the population, QQt = [Q_current];
t = [0]; // Set up a new array, t, for generations
for n=1:15 // Up to 15 generation P_next = P_current * (1 + 1.3 *(1.0 - P_current))-0.5*P_current*Q_current; Q_next = 0.4*Q_current+1.6*P_current*Q_current; Pt = [Pt P_next]; // Expanding the array, Pt. Qt = [Qt Q_next]; // Expanding the array, Qt t = [t,n]; // Expanding the array, t.end
plot(t,Pt,'-o',t,Qt,'-s')legend('P','Q')
0 5 10 150.5
0.6
0.7
0.8
0.9
1.0
1.1
Generation
Po
pu
latio
ns
P
Q
This program is not correct at all!
So, what is wrong on this program?
24
//Initial ConditionsP_current=1.1;Q_current=0.5;
// Set up a new array, Pt, for the population, PPt = [P_current];// Set up a new array, Qt, for the population, QQt = [Q_current];
t = [0]; // Set up a new array, t, for generations
for n=1:15 // Up to 15 generation P_next = P_current * (1 + 1.3 *(1.0 - P_current))-0.5*P_current*Q_current; Q_next = 0.4*Q_current+1.6*P_current*Q_current; Pt = [Pt P_next]; // Expanding the array, Pt. Qt = [Qt Q_next]; // Expanding the array, Qt t = [t,n]; // Expanding the array, t.end
plot(t,Pt,'-o',t,Qt,'-s')legend('P','Q')
5.0
1.1
6.14.0
5.013.11
0
0
1
1
Q
P
QPQQ
QPPPP
tttt
ttttt
So, what is wrong on this program? – cont.
25
for n=1:15 // Up to 15 generation P_next = P_current * (1 + 1.3 *(1.0 - P_current))-0.5*P_current*Q_current; Q_next = 0.4*Q_current+1.6*P_current*Q_current; Pt = [Pt P_next]; // Expanding the array, Pt. Qt = [Qt Q_next]; // Expanding the array, Qt t = [t,n]; // Expanding the array, t.end
5.0
1.1
6.14.0
5.013.11
0
0
1
1
Q
P
QPQQ
QPPPP
tttt
ttttt
Let’s look at for loop statement carefully.
26
loop n P_current Q_current P_next Q_next
initial 1.1 0.5
1 1 1.1 0.5 0.682 1.08
2 2 1.1 0.5 0.682 1.08
3 3 1.1 0.5 0.682 1.08
4 4 1.1 0.5 0.682 1.08
for n=1:15P_next = P_current * (1 + 1.3 *(1.0 - P_current)) - 0.5*P_current*Q_current;Q_next = 0.4*Q_current+1.6*P_current*Q_current;end
loop n P_current Q_current P_next Q_next
initial 1.1 0.5
1 1 1.1 0.5 0.682 1.08
2 2 1.1 0.5 0.682 1.08
3 3 1.1 0.5 0.682 1.08
4 4 1.1 0.5 0.682 1.08
Why does this happen?We have never updated P_current & Q_current
within the for-loop.
What we want to happen:
27
loop n P_current Q_current P_next Q_next
initial 1.1 0.5
1 1 1.1 0.5 0.682 1.08
2 2 0.682 1.08 0.596 1.610
3 3 0.596 1.610 0.429 2.179
4 4 0.429 2.179 0.280 2.368
5.0
1.1
6.14.0
5.013.11
0
0
1
1
Q
P
QPQQ
QPPPP
tttt
ttttt
loop n P_current Q_current P_next Q_next
initial 1.1 0.5
1 1 1.1 0.5 0.682 1.08
2 2 0.682 1.08 0.596 1.610
3 3 0.596 1.610 0.429 2.179
4 4 0.429 2.179 0.280 2.368
OK – How to program this:
28
5.0
1.1
6.14.0
5.013.11
0
0
1
1
Q
P
QPQQ
QPPPP
tttt
ttttt
loop n P_current Q_current P_next Q_next
initial 1.1 0.5
1 1 1.1 0.5 0.682 1.08
2 2 0.682 1.08 0.596 1.610
3 3 0.596 1.610 0.429 2.179
4 4 0.429 2.179 0.280 2.368
for n=1:15P_next = P_current * (1 + 1.3 *(1.0 - P_current)) - 0.5*P_current*Q_current;Q_next = 0.4*Q_current+1.6*P_current*Q_current;
P_current = P_next;Q_current = Q_next;end
Re-assign values to P_current and Q_current!
Correct Program is
29
//Initial ConditionsP_current=1.1;Q_current=0.5;
// Set up a new array, Pt, for the population, PPt = [P_current];// Set up a new array, Qt, for the population, QQt = [Q_current];
t = [0]; // Set up a new array, t, for generations
for n=1:15 // Up to 15 generation P_next = P_current * (1 + 1.3 *(1.0 - P_current))-0.5*P_current*Q_current; Q_next = 0.4*Q_current+1.6*P_current*Q_current; Pt = [Pt P_next]; // Expanding the array, Pt. Qt = [Qt Q_next]; // Expanding the array, Qt t = [t,n]; // Expanding the array, t. P_current = P_next; Q_current = Q_next;end
plot(t,Pt,'-o',t,Qt,'-s')legend('P','Q')
5.0
1.1
6.14.0
5.013.11
0
0
1
1
Q
P
QPQQ
QPPPP
tttt
ttttt
0 5 10 150.0
0.5
1.0
1.5
2.0
2.5
Generations
Po
pu
latio
ns
P
Q
Predator-Prey model
This is a simple predator-prey model.P – prey populationQ – Predator population
30
tttt
ttttt
QPQQ
QPPPP
6.13.0
5.013.11
1
1
where Initial Conditions, P0 = 1.1 and Q0 = 0.5
0 5 10 150.0
0.5
1.0
1.5
2.0
2.5
Generations
Po
pu
latio
ns
P
Q
More Generations (up to 100)
0 10 20 30 40 50 60 70 80 90 1000.0
0.5
1.0
1.5
2.0
2.5
Geneations
Po
pu
latio
ns
0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.10.4
0.6
0.8
1.0
1.2
1.4
1.6
1.8
2.0
2.2
2.4
P
Q
P
Q
Phase plot
subplot(2,1,1)plot(t,Pt,'-o',t,Qt,'-s')legend('P','Q')subplot(2,1,2)plot(Pt,Qt,'-o')
Here are Scilab commands to generate two graphs in one page
UC Merced
Do you have a question?
Now, let’s program this modified discrete logistic model.
Initial population, P00 = 2r = 0.7What if K is not constant.
i.e. K = 100 for first 10 generation, then K = 150 for rest of generations.
Let’s program to graph how the population of this model change.
33
K
PrPP t
tt 111
It means:
34
20 P
10017.011
ttt
PPP
15017.011
ttt
PPP
Initial population
First 10 generations
From 11th generation
if statementThe main statement used for selecting from alternative actions
based on test results.This construction provides a logical branching for computations
if <test1><statement1>
end
Syntax
If the <test1> is non-zero or True, then <statement1> is executed.
if statement – cont.Other syntax
if <test1><statement1>
else<statement2>
end
if <test1><statement1>
elseif <test2><statement2>
else:<statement3>
end
If-else structure. If <test1> is true, <statement1> is executed; otherwise <statement2> is executed.
If-elif-else structure. If <test1> is true, <statement1> is executed; If <test1> is false, but <test2> is true, <statement2> is executed; otherwise <statement3> is executed.
if statement examples
if x < 0printf (“it must be a negative number”)
end
if x < 0printf (“It must be a negative number”)
elseif x >0printf (“It must be a positive number”)
elseprintf (“It must be zero”)
end
Example 1
Example 2
Comparison or Test Operators
Operator Meaning Example Evaluates To
== equal to or congruent to “A” == “A” True
!= not equal to 8 != 5 True
> greater than 5 > 8 False
< less than 5 < 8 True
>= greater than or equal to 5 >= 8 False
<= less than or equal to 5 <= 5 True
Here is how:
39
10150
100100 17.01
2
1
0
tK
tK
K
PPP
P
ttt
P=2; // Initial Condition
Pt = P; // Set up a new array, Pt, for populationst = [0]; // Set up a new array, t, for generations
for n=1:20 // Up to 20 generation if n <= 10 K = 100 // if n is less than and eqaul to 10, K = 100 else K = 150 // other n’s – K = 150 end P = P * (1 + 0.7 *(1.0 – P/K)); // discrete logistic model Pt = [Pt P]; // Expanding the array, Pt. t = [t,n]; // Expanding the array, t.end
plot(t,Pt,'-o')
Here is the plot:
0 2 4 6 8 10 12 14 16 18 200
50
100
150
Generations
Po
pu
latio
ns
K = 100 K = 150
UC Merced
Any Questions?
Do you have a question?
Next WeekLecture : Last Programming in Scialb
Another loop – while loop
Quiz #6
42