Lecture 7 Sept 17 Goals: Complete Chapter 4 Chapters 5 and 6.

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

Dr. Sharon Persinger October 30,  Recursion is a type of repetition used in mathematics and computing to create objects and to define functions.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 20P. 1Winter Quarter Propulsion Lab with MATLAB Lecture 20.
Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
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.
General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009.
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
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
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.
Lecture 4 Discrete Mathematics Harper Langston. Algorithms Algorithm is step-by-step method for performing some action Cost of statements execution –Simple.
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.
Matlab Software To Do Analyses as in Marron’s Talks Matlab Available from UNC Site License Download Software: Google “Marron Software”
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.
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
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.
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.
MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd.
Introduction to MATLAB. Windows in MATLAB Command Window – where you enter data, run MATLAB code, and display results Command History - displays a log.
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.
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
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
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
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.
Lecture 15: Control Flow (cont). 2 Lecture Contents: t Design and implementation of programs illustrating linear algorithms, branch algorithms and loop.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
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 &
CSE 311: Foundations of Computing Fall 2014 Lecture 12: Primes, GCD.
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.
Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.
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.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Debugging, Static Variables, ByRef, ByValue Chapt. 6 in Deitel, Deitel and Nieto.
17 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
5 While-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
CSE 311: Foundations of Computing Fall 2013 Lecture 12: Primes, GCD, modular inverse.
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.
Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.
CSCI 125 & 161 Lecture 12 Martin van Bommel. Prime Numbers Prime number is one whose only divisors are the number 1 and itself Therefore, number is prime.
Introduction to Programming on MATLAB Ecological Modeling Course Sep 11th, 2006.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
生查子 ~ 歐陽修 去年元夜時,花市燈如晝, 月上柳梢頭,人約黃昏後; 今年元夜時,月與燈依舊, 不見去年人,淚濕春衫袖。
Matlab Training Session 4: Control, Flow and Functions
Scripts & Functions Scripts and functions are contained in .m-files
Numerical Algorithms x x-1
Writing functions in MATLAB
Discrete Math for CS CMPSC 360 LECTURE 12 Last time: Stable matching
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
Scripts In Matlab.
Application: Algorithms
Application: Algorithms
REPETITION Why Repetition?
Intro to Programming (in JavaScript)
COMPUTING.
ME 123 Computer Applications I Lecture 5: Input and Output 3/17/03
Presentation transcript:

Lecture 7 Sept 17 Goals: Complete Chapter 4 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. Example:

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 temp. default directory is named work. But this can be changed by specifying other paths. Example:

Files, path, working directory etc. We can load a file using the load command. Example:

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

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;