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

Slides:



Advertisements
Similar presentations
Introduction to Macromedia Director 8.5 – Lingo
Advertisements

Introduction to Matlab
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Flow Charts, Loop Structures
Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction.
Introduction to Computer Programming in C
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Section 2.3 Gauss-Jordan Method for General Systems of Equations
Scripts and Flow Control. Scripts So far we have been entering commands directly into the command line But there is a better way Script files (and functions)
By Hrishikesh Gadre Session II Department of Mechanical Engineering Louisiana State University Engineering Equation Solver Tutorials.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Introduction to SPSS Short Courses Last created (Feb, 2008) Kentaka Aruga.
Matrix Mathematics in MATLAB and Excel
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
James Matte Nicole Calbi SUNY Fredonia AMTNYS October 28 th, 2011.
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
Financial Information System Running Reports in FIS.
An Introduction to Scilab Tsing Nam Kiu 丁南僑 Department of Mathematics The University of Hong Kong 2009 January 7.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Mathcad Variable Names A string of characters (including numbers and some “special” characters (e.g. #, %, _, and a few more) Cannot start with a number.
Chapter 10 Review: Matrix Algebra
An Introduction to Textual Programming
Matlab tutorial course Lesson 2: Arrays and data types
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Needs Work Need to add –HW Quizzes Chapter 13 Matrices and Determinants.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
Math 15 Lecture 12 University of California, Merced Scilab Programming – No. 3.
Math 15 Introduction to Scientific Data Analysis Lecture 5 Association Statistics & Regression Analysis University of California, Merced.
Sample size vs. Error A tutorial By Bill Thomas, Colby-Sawyer College.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Objectives Understand what MATLAB is and why it is widely used in engineering and science Start the MATLAB program and solve simple problems in the command.
Math 15 Lecture 7 University of California, Merced Scilab A “Very” Short Introduction.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
Lesson 13-1: Matrices & Systems Objective: Students will: State the dimensions of a matrix Solve systems using matrices.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Chapter 1: Getting Started with MATLAB MATLAB for Scientist and Engineers Using Symbolic Toolbox.
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.
Introduction to MATLAB 7 Engineering 161 Engineering Practices II Joe Mixsell Spring 2010.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Math 15 Introduction to Scientific Data Analysis Lecture 3 Working With Charts and Graphics.
Chapter 2: General Problem Solving Concepts
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Math 15 Lecture 9 University of California, Merced Scilab A Short Introduction – No. 3 Today – Quiz #4.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
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.
Introduction to Matlab. What is Matlab? A software environment for interactive numerical computations Examples:  Matrix computations and linear algebra.
Introduction to MATLAB 7 MATLAB Programming for Engineer Hassan Migdadi Spring 2013.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Introduction to MATLAB 7 Engineering 161 Engineering Practices II Joe Mixsell Spring 2012.
Trees Example More than one variable. The residual plot suggests that the linear model is satisfactory. The R squared value seems quite low though,
CSE 455 : Computer Vision MATLAB 101 Getting Started with MATLAB.
Tutorial 11 1 JavaScript Operators and Expressions.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Chapter 4 MATLAB Programming MATLAB Troubleshooting Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
INTRODUCTION TO COMPUTER PROGRAMMING(IT-303) Basics.
Computer Application in Engineering Design
Problems With Assistance Module 1 – Problem 2
Matlab Training Session 4: Control, Flow and Functions
L – Modeling and Simulating Social Systems with MATLAB
Matlab Workshop 9/22/2018.
ECONOMETRICS ii – spring 2018
StatLab Matlab Workshop
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Problems With Assistance Module 1 – Problem 2
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.
Problems With Assistance Module 1 – Problem 2
Presentation transcript:

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

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)

How do you like Scilab so far? Grea t Tool!

Outline 1. Introduction to Computer Programming 2. More programming 4

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!

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.

October, 2007 Bis 180

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

We can find A and B for next generation readily. given that A = 10 and B = 5 at t = 0. 9

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?

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

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

UC Merced Any Questions?

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

Here is the example: If you know how to program in Scilab, only 9 lines to do the job. 15 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')

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 = # of rows # of columns -->ones(1,3) ans = >eye(2,2) ans =

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 =

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 = >A=[A B] A = >A=[A B] A = This doesn’t mean A is equal to [A B]. In the programming, this means [A B] is assigned to A.

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 = >A=[A;B] // Expand A A = >A=[A;B] A = >A=[A C] A =

UC Merced Any Questions?

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

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.

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

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

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

SCILAB Programming: Malthusian Model – cont. For the values =1.07, and P 0 = 500, our entire model is now 26 DayPopulation How to program this sequence of events in Scilab.

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?

SCILAB Programming: 28 DayPopulation 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.

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

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 Numbers between 1 and 5 with increment of 1 loopiP 11 1 * * * * * 10 for i = 1:5 P = i * 10 end Default increment is 1, so

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 Numbers between 1 and 10 with increment of 3

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 311*51*5 455*55* * *5 The variable, i, will be assigned from the array t.

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 = Pt = Pt = Pt = Pt = Pt = Pt = Pt = Pt = Let’s calculate populations for Day 1 and Day 10

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 = T=[ ; ]; P0=[10;5]; for t=0:9 Pt=T^t*P0 end

Populations of A and B species. – cont > Pt = Pt = Pt = Pt = Pt = Pt = Pt = Pt = Pt = Pt = T=[ ; ]; P0=[10;5]; for t=0:9 Pt=T^t*P0 end

UC Merced Any Questions?

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.

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') Now we understand parts of the earlier program: 38 We don’t know what operations in these green circles yet.

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

T =[ ; ]; // 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 = Pt =

Making arrays and Expanding arrays – cont. 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 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

What is happening? 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 42 t = 0. Pt = loopntPt 110, , 1, , 1, 2, , 1, 2, 3, Pop. A Pop. B loopntPt 110, , 1, , 1, 2, , 1, 2, 3,

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') One more line to understand this program: 43 To understand this, let’s review some matrix expressions.

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 = ans = 8 0 ans = 4

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') Now we understand this program: 45 This means that plotting all elements of 1 st row against t with the option, ‘ -O ’.

UC Merced 46 Any Questions?

Next Lecture More Programming in Scialb Logical expressions if statement Solving some more difference equations, such as 47

Well. Can I make Scilab outputs nicer? Answer is Yes! 48 --> Pt = Pt = Pt = Pt = Pt = Pt = Pt = Pt = Pt = Pt = Day = 0 Pop A= 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