Lecture 7 Sept 22 Goals: Chapters 5 and 6. Scripts Sequence of instructions that we may want to run can be stored in a file (known as script). by typing.

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

COMP 116: Introduction to Scientific Programming Lecture 37: Final Review.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
1 Quick summary: print print "Kansas" Print string plus a newline print "Kansas", Print string plus a space print "Kansas", "City" Print strings with space.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
Lecture 3 Sept 3 Complete Chapter 3 Exercises quiz.
Lecture 7 Sept 17 Goals: Complete Chapter 4 Chapters 5 and 6.
Lecture 7 Sept 29 Goals: Chapters 5 and 6. Scripts Sequence of instructions that we may want to run can be stored in a file (known as script). by typing.
6/20/2015 5:05 AMNumerical Algorithms1 x x1x
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.
Guide To UNIX Using Linux Third Edition
Lecture 7 Sept 29 Goals: Chapters 5 and 6. Chapter 5 - Exercises Exercise 5.2. Write a script swap.m that swaps the values of variables a and b. For example:
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Design and Analysis of Algorithms - Chapter 11 Algorithm An algorithm is a.
Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia.
Ch 111 Chapter 11 Advanced Batch Files. Ch 112 Overview This chapter focuses on batch file commands that allow you to:  write sophisticated batch files.
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.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
CSC 1701B Computing: Science and Creativity. Outline  Types  Variables  Operators  Control: sequence, selection, repetition  Functions (block headings.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files called M- files. M-files are.
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.
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 06/29/2009.
Introduction to Computational Linguistics Programming I.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
Introduction to Computing Using Python Straight-line code  In chapter 2, code was “straight-line”; the Python statements in a file (or entered into the.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox.
Linux Operations and Administration
Lecture 15: Control Flow (cont). 2 Lecture Contents: t Design and implementation of programs illustrating linear algorithms, branch algorithms and loop.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
You Need an Interpreter!. Closing the GAP Thus far, we’ve been struggling to speak to computers in “their” language, maybe its time we spoke to them in.
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.
Files: By the end of this class you should be able to: Prepare for EXAM 1. create an ASCII file describe the nature of an ASCII text Use and describe string.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Application: Algorithms Lecture 20 Section 3.8 Wed, Feb 21, 2007.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Higher Computing Science 2016 Prelim Revision. Topics to revise Computational Constructs parameter passing (value and reference, formal and actual) sub-programs/routines,
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.
The Hong Kong Polytechnic University Industrial Centre 1 MatLAB Lesson 7 : M-file Programming I Edward Cheung Room W311g 2008.
Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.
Program Development and Design Using C++, Third Edition
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Matlab Training Session 4: Control, Flow and Functions
MACRO Processors CSCI/CMPE 3334 David Egle.
Writing functions in MATLAB
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
funCTIONs and Data Import/Export
Fundamentals of Python: First Programs
Functions continued.
Scripts In Matlab.
Application: Algorithms
Application: Algorithms
COMPUTING.
ME 123 Computer Applications I Lecture 5: Input and Output 3/17/03
ME 123 Computer Applications I Lecture 7: Basic Functions 3/20/03
Presentation transcript:

Lecture 7 Sept 22 Goals: Chapters 5 and 6

Scripts Sequence of instructions that we may want to run can be stored in a file (known as script). by typing the name of the file, Matlab executes the sequence of operations. files can be created by any plain text editor (such as notepad) or the editor that comes with Matlab.

Files, path, working directory etc. We can save the values of the current variables using the save command. >> save(‘temp’, ‘a’, ‘b’, ‘c’); Will save variables a, b, c in a file named temp.mat default directory is named work. But this can be changed by specifying other paths. Example:

Files, path, working directory etc. pwd – gives the path to the working directory. cd – change directory dir – list all the files in the current directory load a file using the load command.

Importing and exporting data We can read from an Excel spreadsheet using the command: >> tab = xlsread(‘my_file.xls’); Now tab becomes a matrix. Example:

Reading a plain text ASCII file

Chapter 5 - Exercises Exercise 5.2. Write a script swap.m that swaps the values of variables a and b. For example: >> a = 1; >> b = 2; >> swap >> a a = 2 >> b b = 1

Exercise 5.3 Store a text file in the working directory. Write a script to create a cell array containing all the individual words of the text file. The cell array should not have any duplicate words. (i.e., keep only one copy of each word.)

Exercise 5.3 Store a text file in the working directory. Write a script to create a cell array containing all the individual words of the text file. The cell array should not have any duplicate words. (i.e., keep only one copy of each word.) x = unique(textread(‘my_file.m', '%s'));

Functions functions encapsulate computations that are repeatedly performed. input and output parameters. Example 1: Write a function to compute the hypotenuse of a right triangle given the two smaller sides a a and b.

function c = hyp(a, b) c = sqrt(a*a + b * b); This file should be stored in the current directory that is visible to Matlab. Then we can perform: >> hyp(3, 4) ans = 5

Example 2: Write a function swap that takes as input an array of integers and returns an array by swapping the max key and the key in index 1. For example: >> B = [1, 2, 8, 4, 7, 5, 6]; >> C = swap(B); >> C Ans = [8, 2, 1, 4, 7, 5, 6]; Etc.

Function swap function B = swap (A) [temp, id] = max(A); A(1) = A(1)+ A(id); A(id)= A(1) - A(id); A(1) = A(1) - A(id); B = A;

Local vs. global variables

Example 3: Write a function GCD that outputs the greatest common divisor of two positive integers n and m. Recall Euclid’s algorithm: GCD of 52, 9 compute mod(52, 9) = 7 new pair: 9, 7 mod(9, 7) = 2 7, 2 mod(7, 2) = 1 2, 1 mod(2, 1) = 0 1, 0 When we reach pair (x, 0), x is the GCD.

GCD function We need to know how to create a loop. There are two ways to do this: for loop while loop For this problem, we will use the while loop.

GCD function function m = gcd(a, b) while ~(b==0) rem = mod(a, b); a = b; b = rem; end; m = a;

Exercise: Write a function that takes as input a positive integer n and returns the first prime number greater than n. Recall the function isprime in Matlab that returns true (false) if the input is a prime (is not a prime).

Exercise: Write a function that takes as input a positive integer n and returns the first prime number greater than n. Recall the function isprime in Matlab that returns true (false) if the input is a prime (is not a prime). function n = nextPrime(m) n = m + 1 while 1 if isprime(n) break; else n = n + 1; end;

Exercise 6.1: Write a function, powersum, that computes for any specified values of z and n.

Solution:

Exercise 6.2 (d) Write a function eliminate that takes as input a character string and returns a string of the same length in which e and E have been replaced by *. Hint: Use find and the index operator. Example: >> eliminate(‘TherE’) ans = Th*r*

Exercise 6.2 (d) Write a function eliminate that takes as input a character string and returns a string of the same length in which e and E have been replaced by *. Hint: Use find and the index operator. Solution:

Exercise 6.3: function out = addToEnd(a, x) out = [a, x]; Just calling the function with a as argument does not change its value. a = addToEnd(a, x) will change a as desired.

Exercise 6.6 >> BooleanToTF([ ]) ans = ‘TFFTTFTF’ etc.

Exercise 6.6 >> BooleanToTF([ ]) ans = ‘TFFTTFTF’ etc. function res = BooleanToTF(bool) res = char('F' + zeros(size(bool))); res(bool) = 'T';