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

Slides:



Advertisements
Similar presentations
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
Advertisements

Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
259 Lecture 7 Spring 2015 Population Models in Excel.
ITEC113 Algorithms and Programming Techniques
CIS101 Introduction to Computing Week 12. Agenda Your questions Solutions to practice text Final HTML/JavaScript Project Copy and paste assignment JavaScript:
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect FXP. 1Winter Quarter Course Wrap Up and.
Welcome To Math 463: Introduction to Mathematical Biology
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
CIS 101: Computer Programming and Problem Solving Lecture 7 Usman Roshan Department of Computer Science NJIT.
true (any other value but zero) false (zero) expression Statement 2
Loops – While, Do, For Repetition Statements Introduction to Arrays
Stat 512 – Lecture 19 Wrap-Up. Announcements Review sheet online  Office hours  Review session next week?  Updated final exam signup on web Review.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Repetition Structures: For Loop Constants CSC 1401: Introduction to Programming with Java Week 5 Wanda M. Kunkle.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Today’s quiz on 8.2 B Graphing Worksheet 2 will be given at the end of class. You will have 12 minutes to complete this quiz, which will consist of one.
CIS101 Introduction to Computing Week 12 Spring 2004.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect FXP. 1Winter Quarter Course Wrap Up and.
Please open your laptops, log in to the MyMathLab course web site, and open Daily Quiz 16. IMPORTANT NOTE: If you have time left out of your five minutes.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
analysis, plug ‘n’ chug, & induction
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
Math 15 Introduction to Scientific Data Analysis Lecture 6 Interactive Excel University of California, Merced.
MATRICES AND DETERMINANTS
Math 15 Introduction to Scientific Data Analysis Lecture 5 Association Statistics & Regression Analysis University of California, Merced.
CISC474 - JavaScript 03/02/2011. Some Background… Great JavaScript Guides: –
1 Experimental Statistics - week 4 Chapter 8: 1-factor ANOVA models Using SAS.
Math 15 Lecture 7 University of California, Merced Scilab A “Very” Short Introduction.
Math 15 Lecture 10 University of California, Merced Scilab Programming – No. 1.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
MTH 161: Introduction To Statistics
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Welcome to Math 15 Introduction to Scientific Data Analysis January 25, 2008 University of California, Merced.
Math 15 Introduction to Scientific Data Analysis Lecture 3 Working With Charts and Graphics.
CONTROLLING PROGRAM FLOW
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
CS 123 Engineering Computation Lab Lab 2 Dan De Sousa and Bruce Char Department of Computer Science Drexel University April 2009 ©By the author. All rights.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Algorithm Design.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Math 15 Lecture 9 University of California, Merced Scilab A Short Introduction – No. 3 Today – Quiz #4.
OO Data Structures SE Dr. Samir Tartir 2010/2011 Second Semester.
Math 15 Introduction to Scientific Data Analysis Lecture 8 Python Programming – Part 2 University of California, Merced.
Math 15 Introduction to Scientific Data Analysis Lecture 9 Python Programming – #3 University of California, Merced.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures.
Introduction to MATLAB 7 Engineering 161 Engineering Practices II Joe Mixsell Spring 2012.
CS 122 Engineering Computation Lab Lab 4 Dan De Sousa and Bruce Char Department of Computer Science Drexel University Summer 2009 ©By the author. All rights.
Week 4 Program Control Structure
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Computational Biology, Part 14 Recursion Relations Robert F. Murphy Copyright  1996, 1999, 2000, All rights reserved.
Chapter 4 MATLAB Programming MATLAB Troubleshooting Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 Structured Programming Arab Academy for Science and Technology CC112 Dr. Sherif Mohamed Tawfik The Course.
Final Exam Information These slides and more detailed information will be posted on the webpage later…
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Population Models in Excel
Chapter 4 MATLAB Programming
Introduction to MATLAB
MATLAB Logical Expressions
Week 3 – Program Control Structure
Exam # 1 INFORMATION Scheduled for Thursday 7/20
Presentation transcript:

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

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)

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.

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.

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

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

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

Now you know how to program in Scilab. A system of two linear difference equations 10 T =[ ; ]; // 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')

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

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.

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

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

UC Merced Any Questions?

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

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 – 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 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 – P/100)); // discrete logistic model Pt = [Pt P]; // Expanding the array, Pt. t = [t,n]; // Expanding the array, t. end plot(t,Pt,'-o')

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.

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

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.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')

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

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.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')

So, what is wrong on this program? – cont. 25 for n=1:15// Up to 15 generation P_next = P_current * ( *(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

Let’s look at for loop statement carefully. 26 loopnP_currentQ_currentP_nextQ_next initial for n=1:15 P_next = P_current * ( *(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 Why does this happen? We have never updated P_current & Q_current within the for- loop.

What we want to happen: 27 loopnP_currentQ_currentP_nextQ_next initial loopnP_currentQ_currentP_nextQ_next initial

OK – How to program this: 28 loopnP_currentQ_currentP_nextQ_next initial for n=1:15 P_next = P_current * ( *(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 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.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')

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

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

UC Merced Do you have a question?

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

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

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.

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.

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

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

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 – 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: K = 100K = 150

UC Merced Any Questions? Do you have a question?

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