Presentation is loading. Please wait.

Presentation is loading. Please wait.

Math 15 Lecture 12 University of California, Merced Scilab Programming – No. 3.

Similar presentations


Presentation on theme: "Math 15 Lecture 12 University of California, Merced Scilab Programming – No. 3."— Presentation transcript:

1 Math 15 Lecture 12 University of California, Merced Scilab Programming – No. 3

2 Course Lecture Schedule WeekDateConceptsProject Due 1 2January 28Introduction to the data analysis 3February 4Excel #1 – General Techniques 4February 11Excel #2 – Plotting Graphs/ChartsQuiz #1 5February 18Holiday 6February 25Excel #3 – Statistical AnalysisQuiz #2 7March 3Excel #4 – Regression Analysis 8March 10Excel #5 – Interactive ProgrammingQuiz #3 9March 17Introduction to SCILAB - Part - I March 24Spring Recesses 10March 31Introduction to SCILAB - Part - II(4/4) Project #1 11April 7Introduction to SCILAB - Part - IIIQuiz #4 12April 14Programming – #1 13April 21Programming – #2Quiz #5 14April 28Programming – #3 15May 5Programming - #4Quiz #6 16May 12Movies / EvaluationsProject #2 FinalMay 19Final Examination (3-6pm COB 116)

3 Project #2 – Due May 12 th 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 30 th.

4 Math 15 Final – May 19 (COB 116) 100 pts. total 2 hours (3pm – 5pm) 50 questions (2 pts. each) 10% from 1 st lecture 40% Excel Related 50% Scilab Related Mostly Multiple choices and fill-in-blanks Similar to quizzes. One problem will ask you to make a small programming Open notes (Max. 5 sheets) But No computers will be allowed.

5 Grading for Math 15 ActivityPoints Assignments120 In-Class Quizzes120 Computer Labs70 Project #170 Project #270 Final Exam100 Total550 Projected points +~45 extra points (by optional hw.)

6 Any Questions?

7 Outline Today – more programming 1. More for-loop 2. Logical expressions if statement 7

8 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. 8

9 Here: Let’s use the matrix to solve this system of equations: For t = n, 9 Transition Matrix Initial Conditions

10 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 matrix P0=[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')

11 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? Lecture 10 Dolphin training problem Lecture 11

12 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 where K and r are positive. You may see this equation in Bis 1 or/and Bis 180.

13 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

14 Logistic model – cont. If P<<K, 14 The model becomes a simple population model.

15 UC Merced Any Questions?

16 Let’s program this logistic model. 0 If P 0 = 2, let’s program to graph how the population of this model change. 16 where K = 100 and r = 0.7

17 Let’s program this discrete logistic model – cont. 17 P=2; // Initial Condition Pt = [P];// Set up a new array, Pt, for populations t = [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')

18 Let’s program this discrete logistic model – cont. 18 P=2; // Initial Condition Pt = [P];// Set up a new array, Pt, for populations t = [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')

19 Review: for loop statement for loop is used to repeat a command or a group of commands for a fixed number of times. The Basic Structure for 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.

20 UC Merced Any Questions?

21 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 where Initial Conditions, P 0 = 1.1 and Q 0 = 0.5

22 Let’s program this nonlinear population model. 22 //Initial Conditions P_current=1.1; Q_current=0.5; // Set up a new array, Pt, for the population, P Pt = [P_current]; // Set up a new array, Qt, for the population, Q Qt = [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')

23 Let’s program this nonlinear population model – cont. //Initial Conditions P_current=1.1; Q_current=0.5; // Set up a new array, Pt, for the population, P Pt = [P_current]; // Set up a new array, Qt, for the population, Q Qt = [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') This program is not correct at all!

24 So, what is wrong on this program? 24 //Initial Conditions P_current=1.1; Q_current=0.5; // Set up a new array, Pt, for the population, P Pt = [P_current]; // Set up a new array, Qt, for the population, Q Qt = [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')

25 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

26 Let’s look at for loop statement carefully. 26 loopnP_currentQ_currentP_nextQ_next initial 1.10.5 11 1.10.50.6821.08 22 1.10.50.6821.08 33 1.10.50.6821.08 44 1.10.50.6821.08 for n=1:15 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; end loopnP_currentQ_currentP_nextQ_next initial 1.10.5 11 1.10.50.6821.08 22 1.10.50.6821.08 33 1.10.50.6821.08 44 1.10.50.6821.08 Why does this happen? We have never updated P_current & Q_current within the for- loop.

27 What we want to happen: 27 loopnP_currentQ_currentP_nextQ_next initial 1.10.5 11 1.10.50.6821.08 22 0.6821.080.5961.610 33 0.5961.6100.4292.179 44 0.4292.1790.2802.368 loopnP_currentQ_currentP_nextQ_next initial 1.10.5 11 1.10.50.6821.08 22 0.6821.080.5961.610 33 0.5961.6100.4292.179 44 0.4292.1790.2802.368

28 OK – How to program this: 28 loopnP_currentQ_currentP_nextQ_next initial 1.10.5 11 1.10.50.6821.08 22 0.6821.080.5961.610 33 0.5961.6100.4292.179 44 0.4292.1790.2802.368 for n=1:15 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; P_current = P_next; Q_current = Q_next; end Re-assign values to P_current and Q_current!

29 Correct Program is 29 //Initial Conditions P_current=1.1; Q_current=0.5; // Set up a new array, Pt, for the population, P Pt = [P_current]; // Set up a new array, Qt, for the population, Q Qt = [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')

30 Predator-Prey model This is a simple predator-prey model. P – prey population Q – Predator population 30 where Initial Conditions, P 0 = 1.1 and Q 0 = 0.5

31 More Generations (up to 100) 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

32 UC Merced Do you have a question?

33 Now, let’s program this modified discrete logistic model. 0 Initial population, P 0 = 2 r = 0.7 What 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

34 It means: 34 Initial population First 10 generations From 11 th generation

35 if statement The main statement used for selecting from alternative actions based on test results. This construction provides a logical branching for computations if end Syntax If the is non-zero or True, then is executed.

36 if statement – cont. Other syntax if else end if elseif else: end If-else structure. If is true, is executed; otherwise is executed. If-elif-else structure. If is true, is executed; If is false, but is true, is executed; otherwise is executed.

37 if statement examples if x < 0 printf (“it must be a negative number”) end if x < 0 printf (“It must be a negative number”) elseif x >0 printf (“It must be a positive number”) else printf (“It must be zero”) end Example 1 Example 2

38 Comparison or Test Operators OperatorMeaningExampleEvaluates To ==equal to or congruent to “ A ” == “ A ” True !=not equal to8 != 5True >greater than5 > 8False <less than5 < 8True >=greater than or equal to5 >= 8False <=less than or equal to5 <= 5True

39 Here is how: 39 P=2; // Initial Condition Pt = P;// Set up a new array, Pt, for populations t = [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')

40 Here is the plot: K = 100K = 150

41 UC Merced Any Questions? Do you have a question?

42 Next Week Lecture : Last Programming in Scialb Another loop – while loop Quiz #6 42


Download ppt "Math 15 Lecture 12 University of California, Merced Scilab Programming – No. 3."

Similar presentations


Ads by Google