Matlab Programming a logical approach. Outline Announcements: –Homework I: due Wed. by 5, by e-mail –Remember: text & subject –Last day to add/drop or.

Slides:



Advertisements
Similar presentations
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Advertisements

R for Macroecology Aarhus University, Spring 2011.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Programming with MATLAB
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
Computer Science in Practice This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine.
Computer Science in Practice This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine.
Programming in MATLAB Week 14 – 4/28/09 Kate Musgrave
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
MATLAB FUNDAMENTALS: INPUT/OUTPUT LOGIC CONTROL STRUCTURES HP 101 – MATLAB Wednesday, 9/24/2014
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.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 06/29/2009.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Text Processing. Outline Announcements: –Homework I: due Today. by 5, by Discuss on Friday. –Homework II: on web HW I: question 7 Finish functions.
How to think through your program [ principles of good program design ] Rachel Denison MATLAB for Cognitive Neuroscience ICN, 13 December 2007.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
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.
Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
COMP 116: Introduction to Scientific Programming Lecture 28: Midterm #2 Review.
ENGR 1320 Final Review - Programming Major Topics: – Functions and Scripts – Vector and Matrix Operations in Matlab Dot product Cross product – Plotting.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
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.
Flow Control and Functions ● Script files ● If's and For's ● Basics of writing functions ● Checking input arguments ● Variable input arguments ● Output.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Conditionals CS 103 February 16, Blast from the Past: C14 Dating Problem Statement: Calculate the age of a fossil from its C-14 radioactivity Problem.
Script M-Files Group of Matlab commands placed in a text file with a text editor. Matlab can open and execute the commands exactly as if they were entered.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Week 8: Decisions Bryan Burlingame 21 October 2015.
CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions.
CSE123 Lecture 3 Files and File ManagementScripts.
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.
Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and.
A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
Introduction to Matlab
Matlab Programming a logical approach. Outline Announcements: –Homework I: due Wed. by 5, by –Last day to add/drop or change credit/audit Iteration.
Structured Programming II: If Statements By the end of this class you should be able to: implement branching in a program describe and use an “if” statement.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Conditional Logic in MATLAB By Bruce Raine. How to do a basic IF – END structure in MATLAB if MATLAB Commands end i.e. do the MATLAB commands if the conditional.
General Computer Science for Engineers CISC 106 Lecture 15 Dr. John Cavazos Computer and Information Sciences 03/16/2009.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Chapter 4 MATLAB Programming MATLAB Troubleshooting Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Click to edit Master title style Click to edit Master text styles –Second level Third level –Fourth level »Fifth level 1 Fundamentals of Programming Most.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Matlab Training Session 4: Control, Flow and Functions
Chapter 5: Loops and Files.
Scripts & Functions Scripts and functions are contained in .m-files
Expressions and Control Flow in JavaScript
Conditional Statements
When I want to execute the subroutine I just give the command Write()
Matlab review Matlab is a numerical analysis system
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
CS100J 26 April. Matlab Use help button!!! Variables, values, types
Selection Statements Chapter 3.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Programming The ideal style of programming is Structured or
Presentation transcript:

Matlab Programming a logical approach

Outline Announcements: –Homework I: due Wed. by 5, by –Remember: text & subject –Last day to add/drop or change credit/audit Iteration Conditionals and logic M-files

A Short Pep-Talk Some facts: –Computers are dumb. –No, actually, they’re really dumb –Computers can only do a few stupid things, but They can do them over & over again very quickly without tiring or complaining Some conclusions –You are way smarter than your computer--don’t be intimidated –If you have to do something several times--get the stupid computer to do it.

Iteration For loops in Matlab use index-notation: for j=st:step:en; ; end Example: inner product u’*v tot=0; for j=1:length(u); tot=tot+u(j)*v(j); end

Conditionals Conditional statements control flow of a program if(logic); ; else; ; end Matlab also has switch: switch(j); case a: ; case b: ; … otherwise: ; end

Logic Relational operators: (R x R -> B) –, =, ==, ~= –isnan(a), isinf(a) Logical operators: (B x B -> B) –&, |, ~, xor (which is just ~=) aba&ba|bxor(a,b)a~=b TTTT00 T00TTT 0T0TTT

Logic Matlab’s Boolean type is a logic array –Logical operators work with doubles, too –0=> false; anything else => true Logical operators are defined for arrays: –a=1:5; a = [ ] –b=a<3 b= [ ]

Find--searches for the truth (or not false) –b=[ ]; –I=find(b) I=[1 3] --b(I) is an array without zeros –I=find(b<1) “b [ ]; I=[ ] –I=find(b>1) “b>1”-->[ ]; I=[] Logic--searching with find

M=ones(3,1)*[ 1 2 3]; –I=find(M==2) I= treats M as M(:)! –[I,J]=find(M==2) I= J= Find for matrices * = M(:)=

Programming Matlab programs are stored in “m-files” –Text files containing Matlab commands and ending with.m –Script m-files--commands executed as if entered on command line –Function m-files--analogous to subroutines or methods, maintain their own memory (workspace)

Scripts are Evil Scripts are EXTREMELY poor programming –Application specific, so difficult to reuse –Dangerous: variables created in scripts can overwrite existing variables If you send me a script, I will send it back!

Functions First line of file must be function [outputs]=fname(inputs) –outputs and inputs can have 0,1, or many variables –Ex: [U,V]=SSHvel(SSH, lat,lon) Requires 3 inputs (referred to in SSHvel.m as SSH, lat, lon) Returns 2 outputs. Whatever U and V are when SSHvel finishes are returned to the workspace (or calling function) >> [locU,locV]=SSHvel(locSSH, loclat,loclon)

Generic Function Format 1.Function statement 2.1st comment--used by lookfor 3.Comments returned by help SSHvel--Should indicate how to call, and describe what it does 4.Error checking--check that sizes are correct/consistent. Return a helpful message with error command 5.Code

Example: Geostrophy We want to create a function to compute velocity from SSH data SSH lat lonlon SSHvel.m U V

Summary Iteration with for (while exists too) Conditionals with if and switch Logical operators and vectorized 0=false find searches for truth Extend matlab with m-file functions –[output]=fname(input)