A bit more about variables >> var1 = [ 1 2 3] var1 = 1 2 3 >> var2 = var1 var2 = 1 2 3 >> var1 = [4 5 6] var1 = 4 5 6 >> var2 var2 = 1 2 3.

Slides:



Advertisements
Similar presentations
LIS651 lecture 1 PHP basics, database introduction Thomas Krichel
Advertisements

Lecture #9 EGR 277 – Digital Logic
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Programming Environment S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: Control Flow.
General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
Lecture 3 Creating M-files Programming tools: Input/output(assign/graph-&-display) Repetition (for) Decision (if) © 2007 Daniel Valentine. All rights reserved.
1 CSE 20: Lecture 7 Boolean Algebra CK Cheng 4/21/2011.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
1 Statement-Level Control Structures Levels of flow control Control Statements 1. Sequence 2. Selection 3. Iteration Unconditional branching Guarded commands.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
CSE123 Lecture 3 Different types of Variables Logical operators, Conditional Statements and If Blocks.
Matlab Programming, part 1 M-files It is generally more convenient to program in Matlab using m-files, ascii text files containing a set of Matlab commands.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Solving biological problems with MATLAB Lecturer: Chen Keasar, Office hours: 37/102 Sunday, 14:00-16:00.
S2008Final_part1.ppt CS11 Introduction to Programming Final Exam Part 1 S A computer is a mechanical or electrical device which stores, retrieves,
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Engineering Computation with MATLAB Second Edition by David M. Smith.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Basic Control Structures
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
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.
1 CS1371 Introduction to Computing for Engineers Control Statements 9/4/2003.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
Multiplying Matrices Lesson 4.2. Definition of Multiplying Matrices The product of two matrices A and B is defined provided the number of columns in A.
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
1 for – while – switch ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
Intro to Matlab Yipei Wang & Qihui Li {yipeiw,
Logic Gates and Boolean Algebra Introduction to Logic II.
Flow control. Conditionals if condition do this stuff end if condition do this stuff else do this stuff end if condition do this stuff elseif condition.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
INTRODUCTION TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in.
Introduction to Python
13.4 Product of Two Matrices
EEE 161 Applied Electromagnetics
Computer Application in Engineering Design
INC 161 , CPE 100 Computer Programming
Control Statements in Matlab
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
MatLab Programming By Kishan Kathiriya.
Matrix Multiplication
Think What will be the output?
Scripts & Functions Scripts and functions are contained in .m-files
MATLAB DENC 2533 ECADD LAB 9.
Introduction to MATLAB
Example of programming a quantum robot
Arithmetic operations, decisions and looping
Introduction to Python
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Use of Mathematics using Technology (Maltlab)
An Introduction to Python
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Logical Operations In Matlab.
Introduction to Matlab LAB 4
Selection Statements.
Summary Two basic concepts: variables and assignments Basic types:
Lecture 4 Structure plan
INTRODUCTION TO MATLAB
Introduction to Python
Matlab Basics.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Presentation transcript:

A bit more about variables >> var1 = [ 1 2 3] var1 = >> var2 = var1 var2 = >> var1 = [4 5 6] var1 = >> var2 var2 = 1 2 3

>> var3 = 'Mostly Harmless' var3 = Mostly Harmless >> var3(2:9) ans = ostly Ha Strings – vectors of characters

Control Structures Sequence Repetition Selection

Sequence Pop1=23 birthRate=0.2 deathRate=0.1 birth=Pop1 * birthRate death=Pop1 * deathRate change=birth - death Pop2=Pop1 + change (The commands are executed one after the other)

Repetitions

Reminder function pop = popDynam(popSize1, birthRate, deathRate) birth = popSize1 * birthRate; death = popSize1 * deathRate; change = birth - death; popSize2 = popSize1 + change; pop = [popSize2 birth death]; end Please note the indentation.

>> popDynam(23,0.2,0.1) ans = >>

popDynam([ ]) An Alternative approach is to encapsulate the three parameters in a single element – a vector.

function pop=popDynam(popParam) popSize1 = popParam(1); birthRate = popParam(2); deathRate = popParam(3); birth = popSize1 * birthRate; death = popSize1 * deathRate; change = birth - death; popSize2 = popSize1 + change; pop = [popSize2 birth death]; end

>> popDynam([ ]) ans = >>

The vector [ ] represents the parameters of a population. How would we represent the parameters of three populations?

popParam = [ ; ; ] popParam = >> popParam(:,1 ) population size popParam(:,2) birth rate popParam(:,3) death rate

function pop=popDynam(popParam) for iPop = 1:3 popSize1 = popParam(iPop,1); birthRate = popParam(iPop,2); deathRate = popParam(iPop,3); birth = popSize1 * birthRate; death = popSize1 * deathRate; change = birth - death; popSize2 = popSize1 + change; pop(iPop,:) = [popSize2 birth death]; end

function pop=popDynam(popParam) for iPop = [1 2 3] popSize1 = popParam(iPop,1); birthRate = popParam(iPop,2); deathRate = popParam(iPop,3); birth = popSize1 * birthRate; death = popSize1 * deathRate; change = birth - death; popSize2 = popSize1 + change; pop(iPop,:) = [popSize2 birth death]; end What is the output of this function?

function pop=popDynam(popParam) for iPop = [1 3] popSize1 = popParam(iPop,1); birthRate = popParam(iPop,2); deathRate = popParam(iPop,3); birth = popSize1 * birthRate; death = popSize1 * deathRate; change = birth - death; popSize2 = popSize1 + change; pop(iPop,:) = [popSize2 birth death]; end What is the output of this function?

function pop=popDynam(popParam) for iPop = 1:size(popParam,1) popSize1 = popParam(iPop,1); birthRate = popParam(iPop,2); deathRate = popParam(iPop,3); birth = popSize1 * birthRate; death = popSize1 * deathRate; change = birth - death; popSize2 = popSize1 + change; pop(iPop,:) = [popSize2 birth death]; end But the input matrix may be larger….

Selection if (Pop1>60) deathRate=0.15 birthRate=0.12 end

If-else-end if (Pop1>60) deathRate=0.15 birthRate=0.12 else deathRate=0.1 birthRate=0.2 end

If-elseif-end if (Pop1>60) deathRate=0.15 birthRate=0.12 elseif ((Pop1>40) & (Pop1<61)) deathRate=0.13 birthRate=0.18 else deathRate=0.1 birthRate=0.2 end and

Relational operators < > <= >= == (equals) ~= (does not equal) Logical operators & (and) ~ (not) | (or) e.g.,: if ((A>B) | (B~=C))

Logical expressions cab be interpreted >> 10 > 1 ans = 1 >> 10 > 100 ans = 0

Logical expressions cab be applied to vectors and matrices >> popParam popParam = >> popParam(:,1) > 30 ans = 0 1 0

The “find” command >> popParam popParam = >> find( popParam(:,1) > 30) ans = 2

Repetition with logical condition >> i = 10; >> while (i > 0) disp(1/i); i = i - 1; end >> commands output

A bug >> i = 10; >> while (i < 11) disp(1/i); i = i - 1; end commands

A bug >> i = 10; >> while (i < 11) disp(1/i); i = i - 1; end Inf commands

>> i = 10; >> while (i < 11) disp(1/i); if (isinf(1/i)) error('Bad number'); end i = i - 1; end Inf ??? Bad number