Download presentation
Presentation is loading. Please wait.
Published byLionel Osborne Modified over 9 years ago
1
Math 15 Lecture 10 University of California, Merced Scilab Programming – No. 1
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 Computer Programming - Part - I March 24Spring Recesses 10March 31Introduction to Computer Programming - Part - II(4/4) Project #1 11April 7Programming – #1Quiz #4 12April 14Programming – #2 13April 21Programming – #3Quiz #5 14April 28Programming – #4 15May 5Programming - #5Quiz #6 16May 12Movies / EvaluationsProject #2 FinalMay 19Final Examination (3-6pm COB 116)
3
How do you like Scilab so far? Grea t Tool!
4
Outline 1. Introduction to Computer Programming 2. More programming 4
5
What is a computer programming? A computer programming is a sequence of instructions that specifies how to perform a computation. The computation might be something mathematical, such as solving a system of equations or finding the roots of a polynomial, but it can also be a symbolic computation, such as searching and replacing text in a document or (strangely enough) compiling a program. Basically, all computers need someone to tell them what to do!
6
The way of programming The single most important skill for a scientist is problem solving. Problem solving means the ability to formulate problems, think creatively about solutions, and express a solution clearly and accurately. As it turns out, the process of computer programming is an excellent opportunity to practice problem-solving skills.
7
October, 2007 Bis 180
8
When does computer programming become useful? 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. 8
9
We can find A and B for next generation readily. given that A = 10 and B = 5 at t = 0. 9
10
One obvious, but not best, way to do this calculation. To find A and B at t = 156: 10 Even though this is an obvious way to do this calculation, nobody wants to do this way. Is there any other way to do this?
11
Another way to do this. Let’s use the matrix to solve this system of equations: To find A and B for t = 1, For t = 2, For t = 3, 11
12
Another way to do this. Let’s use the matrix to solve this system of equations: For t = 156, For t = n, 12 Transition Matrix Initial Conditions
13
UC Merced Any Questions?
14
Well… The matrix approach is certainly a easy way to solve the equations. But I need to plot (A vs. t ) and (B vs. t) between What is the better way to do this operation? 14 Scilab programming will make your life easy!!
15
Here is the example: If you know how to program in Scilab, only 9 lines to do the job. 15 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')
16
Before going further, Some new matrix operations. Simple Matrix Generation Some basic matrices can be generated with a single command: CommandGenerated matrix zeros All zeros ones All ones eye Identity matrix rand Random elements matrix -->zeros(3,2) ans = 0. 0. # of rows # of columns -->ones(1,3) ans = 1. 1. 1. -->eye(2,2) ans = 1. 0. 0. 1
17
Some new matrix operations. – cont. rand() random matrix generator. rand(m1,m2) is a random matrix of dimension m1 by m2. Default: Random numbers are uniformly distributed in the interval (0,1). # of rows # of columns -->A = rand(5,3) A = 0.2113249 0.6283918 0.5608486 0.7560439 0.8497452 0.6623569 0.0002211 0.6857310 0.7263507 0.3303271 0.8782165 0.1985144 0.6653811 0.0683740 0.5442573
18
Some new matrix operations. – Cont. Concatenation Concatenation is the process of joining smaller size matrices to form bigger ones. This is done by putting matrices as elements in the bigger matrix. A=[1 2 3]; B = [4 5 6]; C = [7;8;9] -->[A B] // to expand columns ans = 1. 2. 3. 4. 5. 6. -->A=[A B] A = 1. 2. 3. 4. 5. 6. -->A=[A B] A = 1. 2. 3. 4. 5. 6. 4. 5. 6. This doesn’t mean A is equal to [A B]. In the programming, this means [A B] is assigned to A.
19
Some new matrix operations. – Cont. Concatenation Numbers of rows for two matrices are different. Concatenation must be row/column consistent. A=[1 2 3]; B = [4 5 6]; C = [7;8;9] -->[A C] !--error 5 inconsistent column/row dimensions -->[A;B] // to expand rows ans = 1. 2. 3. 4. 5. 6. -->A=[A;B] // Expand A A = 1. 2. 3. 4. 5. 6. -->A=[A;B] A = 1. 2. 3. 4. 5. 6. -->A=[A C] A = 1. 2. 3. 7. 4. 5. 6. 8. 4. 5. 6. 9.
20
UC Merced Any Questions?
21
Programming – Use Scilab Editor The "Editor" menu : launches the SCILAB default editor SciPad. (Use help scipad for additional information). The SciPad editor can be used to create SCILAB script and function files. As well as to create input data files, or to edit output data files. 21
22
Editor – cont. To Execute the program from SciPad. Click “Load into Scilab” under Execute menu. Don’t forget to save your program before closing the editor. The file extensions used by scilab are sce and sci. To save a file, click for the menu File and choose Save.
23
SCILAB Programming: Simple population Model Simplest model of a population growth: Fecundity and death rates; f and d. P – Changes in population where P t = P(t) = the site of the population measured on day t. P t+1 – P t is the difference or change in population between two consecutive days. 23
24
SCILAB Programming: Simple population Model – cont. where = 1+f d. Population ecologists often refer to the constant as the finite growth rate. Generally, this type of equations is referred to as a difference equation. Formula expressing values of some quantity in terms of previous value: 24
25
SCILAB Programming: Malthusian Model – cont. For the values f =.1, d = 0.3 ( =1+ f d ), and P 0 = 500, our entire model is now First equation is a difference equation. Second equation is called its initial condition. 25
26
SCILAB Programming: Malthusian Model – cont. For the values =1.07, and P 0 = 500, our entire model is now 26 DayPopulation 0500 1 2 3 4 How to program this sequence of events in Scilab.
27
SCILAB Programming: To find a population of Day 5, Scilab Commands are: To find a table of population vs. Day 27 P0=500; // Initial Condition t=5 // Day 5 Pt=(500)^t*P0// Difference Equation P0=500; // Initial Condition t=1 // Day 1 Pt=(500)^t*P0 t=2 // Day 2 Pt=(500)^t*P0 … Is this best we can do?
28
SCILAB Programming: 28 DayPopulation 0 1 2 3 4 n Sequence iteration: Repeating identical or similar tasks Repeating identical or similar tasks without making errors is something that computers do well and people do poorly.
29
for loop statement 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. Slightly modified version: for variable = array commands end
30
for loop statement– cont. 1 st Example: 30 for i = 1:1:5 P = i * 10 end --> P = 10. P = 20. P = 30. P = 40. P = 50. loopiP 11 1 ´ 10 22 2 ´ 10 33 3 ´ 10 44 4 ´ 10 55 5 ´ 10 Numbers between 1 and 5 with increment of 1 loopiP 11 1 * 10 22 2 * 10 333*10 44 4 * 10 55 5 * 10 for i = 1:5 P = i * 10 end Default increment is 1, so
31
for loop statement– cont. 2 nd Example: 31 for i = 1:3:10 P = i * 5 end --> P = 5. P = 20. P = 35. P = 50. loopiP 11 1*51*5 24 4*54*5 37 7*57*5 410 10 * 5 Numbers between 1 and 10 with increment of 3
32
for loop statement– cont. 3 rd Example: 32 t = [0, 0.5, 1, 5, 10, 100]; for i = t P = i * 5 end --> P = 0. P = 2.5 P = 5. P = 25. P = 50. P = 500. loopiP 101*51*5 20.50.5*5 311*51*5 455*55*5 51010*5 6100100*5 The variable, i, will be assigned from the array t.
33
Let’s use “for loop” to program Simple Population Model 33 P0 =500; for t=0:10 Pt=(1.07)^t*P0 end --> Pt = 500. Pt = 535. Pt = 572.45 Pt = 612.5215 Pt = 655.39801 Pt = 701.27587 Pt = 750.36518 Pt = 802.89074 Pt = 859.09309 Pt = 919.22961 Pt = 983.57568 Let’s calculate populations for Day 1 and Day 10
34
Now let’s program the first example – Populations of A and B species. Let’s use the matrix to solve this system of equations: given that A = 10 and B = 5 at t = 0. 34 T=[0.9 0.1;0.1 0.9]; P0=[10;5]; for t=0:9 Pt=T^t*P0 end
35
Populations of A and B species. – cont. 35 --> Pt = 10. 5. Pt = 9.5 5.5 Pt = 9.1 5.9 Pt = 8.78 6.22 Pt = 8.524 6.476 Pt = 8.3192 6.6808 Pt = 8.15536 6.84464 Pt = 8.024288 6.975712 Pt = 7.9194304 7.0805696 Pt = 7.8355443 7.1644557 T=[0.9 0.1;0.1 0.9]; P0=[10;5]; for t=0:9 Pt=T^t*P0 end
36
UC Merced Any Questions?
37
Well, the original questions were … The matrix approach is certainly a easy way to solve the equations. But I need to plot (A vs. t ) and (B vs. t) between What is the better way to do this operation? 37 OK. We can use ‘for loop’ to do recursive operation. Now, I would like to know how to plot.
38
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') Now we understand parts of the earlier program: 38 We don’t know what operations in these green circles yet.
39
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') To plot, we need data in the array formats. Example: So we need to create arrays to plot theta = linspace(0,2*%pi,100); x = sin(theta); y = cos(theta); plot(theta,x,theta,y,'--')
40
T =[0.9 0.1; 0.1 0.9]; // Transition matrix P0=[10;5]; // Initial Conditions Pt = P0; Making arrays and Expanding arrays 40 Again, this doesn’t means Pt is equal to P0. But, rather, P0 is assigned to a new matrix Pt. At this point, Matrices P0 and Pt have exactly the same elements. Let’s read the previous program line by line: P0 = 10. 5. Pt = 10. 5.
41
Making arrays and Expanding arrays – cont. 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 Make a new array, t, with one element. Expanding elements in the array, t. For each loop, add an element, n, to the array t. Expanding elements in the matrix, Pt. For each loop, add an element, T n P 0, to the matrix Pt. n is assigned from 1 and 156 with increment of 1
42
What is happening? 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 42 t = 0. Pt = 10. 5. loopntPt 110, 110 9.5 5 5.5 220, 1, 210 9.5 9.1 5 5.5 5.9 330, 1, 2, 310 9.5 9.1 8.8 5 5.5 5.9 6.2 440, 1, 2, 3, 410 9.5 9.1 8.8 8.5 5 5.5 5.9 6.2 6.5 Pop. A Pop. B loopntPt 110, 110 9.5 5 5.5 220, 1, 210 9.5 9.1 5 5.5 5.9 330, 1, 2, 310 9.5 9.1 8.8 5 5.5 5.9 6.2 440, 1, 2, 3, 410 9.5 9.1 8.8 8.5 5 5.5 5.9 6.2 6.5
43
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') One more line to understand this program: 43 To understand this, let’s review some matrix expressions.
44
Matrix operations in Scilab For example, the following is a matrix: In Scilab, X=[5 8 1;4 0 2] X(2,1) // element of 2 nd row and 1 st column First subscript in a matrix refers to the row and the second subscript refers to the column. X(:,2) //All elements in 2 nd Column X(1,:) // All elements in 1 st row. ans = 5. 8. 1. ans = 8 0 ans = 4
45
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') Now we understand this program: 45 This means that plotting all elements of 1 st row against t with the option, ‘ -O ’.
46
UC Merced 46 Any Questions?
47
Next Lecture More Programming in Scialb Logical expressions if statement Solving some more difference equations, such as 47
48
Well. Can I make Scilab outputs nicer? Answer is Yes! 48 --> Pt = 10. 5. Pt = 9.5 5.5 Pt = 9.1 5.9 Pt = 8.78 6.22 Pt = 8.524 6.476 Pt = 8.3192 6.6808 Pt = 8.15536 6.84464 Pt = 8.024288 6.975712 Pt = 7.9194304 7.0805696 Pt = 7.8355443 7.1644557 Day = 0 Pop A= 10.00 Pop B= 5.00 Day = 1 Pop A= 9.50 Pop B= 5.50 Day = 2 Pop A= 9.10 Pop B= 5.90 Day = 3 Pop A= 8.78 Pop B= 6.22 Day = 4 Pop A= 8.52 Pop B= 6.48 Day = 5 Pop A= 8.32 Pop B= 6.68 Day = 6 Pop A= 8.16 Pop B= 6.84 Day = 7 Pop A= 8.02 Pop B= 6.98 Day = 8 Pop A= 7.92 Pop B= 7.08 Day = 9 Pop A= 7.84 Pop B= 7.16
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.