Matlab Programming a logical approach. Outline Announcements: –Homework I: due Wed. by 5, by e-mail –Last day to add/drop or change credit/audit Iteration.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
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)
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.
Introduction to MATLAB
Lecture 7 Sept 17 Goals: Complete Chapter 4 Chapters 5 and 6.
Computer Science in Practice This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine.
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT.
Programming in MATLAB Week 14 – 4/28/09 Kate Musgrave
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB
Extending MATLAB Write your own scripts and/or functions Scripts and functions are plain text files with extension.m (m-files) To execute commands contained.
Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
January 24, 2005 Lecture 3 - By P. Lin 1 CPET 190 Lecture 3 Problem Solving with MATLAB
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.
Text Processing. Outline Announcements: –Homework I: due Today. by 5, by Discuss on Friday. –Homework II: on web HW I: question 7 Finish functions.
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.
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.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
October 3, 2005 Lecture 8 - By Paul Lin 1 CPET 190 Lecture 8 Problem Solving with MATLAB
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.
Flow Control and Functions ● Script files ● If's and For's ● Basics of writing functions ● Checking input arguments ● Variable input arguments ● Output.
Synthesis ENGR 1181 MATLAB 11. Topics  No new material  Covers topics that will be on the Midterm 2 Exam MATLAB 01 – Program Design MATLAB 02 – Introduction.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
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.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
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.,
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.
A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2.
Introduction to Matlab Part II 1Daniel Baur / Introduction to Matlab Part II Daniel Baur / Michael Sokolov ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften.
MATLAB Programming COMM2M Harry R. Erwin, PhD University of Sunderland.
Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
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.
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
Extending MATLAB Write your own scripts and/or functions Scripts and functions are plain text files with extension.m (m-files) To execute commands contained.
Matlab Programming a logical approach. Outline Announcements: –Homework I: due Wed. by 5, by –Remember: text & subject –Last day to add/drop or.
Introduction to Programming on MATLAB Ecological Modeling Course Sep 11th, 2006.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
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
Basics of MATLAB 2- Programming in MATLAB By DR. Wafaa Shabana
Matlab Training Session 4: Control, Flow and Functions
Chapter 4 MATLAB Programming
Scripts & Functions Scripts and functions are contained in .m-files
Expressions and Control Flow in JavaScript
Introduction to MATLAB
Conditional Statements
When I want to execute the subroutine I just give the command Write()
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
CS100J 26 April. Matlab Use help button!!! Variables, values, types
Writing functions in MATLAB
Text Processing.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Presentation transcript:

Matlab Programming a logical approach

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

Iteration For loops in Matlab use index-notation: for j=st:step:en; ; end Example: 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 double –0=> false; anything else => true Logical operators are defined for arrays: –a=1:5; –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(ones(3,1)*[ 1 2 3]==2) I= –[I,J]=find(ones(3,1)*[ 1 2 3]==2) I= J= Logic--searching with find

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)

Functions First line of file must be function [outputs]=fname(inputs) –outputs and inputs can be blank –Comments immediately following are returned by help –Functions must be in current directory or in Matlab’s search path

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

Matlab Path Matlab maintains a list of directories where it searches for files Type “path” to see Can add directories using addpath or through GUI To make permanent, place addpaths in startup.m –a special script that is executed at startup

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)