lecture 7 sept 17 goals: complete chapter 4

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

Upload: mandel

Post on 02-Feb-2016

39 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

Lecture 7 Sept 17

Goals:

• Complete Chapter 4

• Chapters 5 and 6

Page 2: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

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:

Page 3: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

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:

Page 4: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

Files, path, working directory etc.

• We can load a file using the load command.

Example:

Page 5: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

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:

Page 6: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

Reading a plain text ASCII file

Page 7: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

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.

Page 8: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

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

Page 9: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

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);>> CAns = [8, 2, 1, 4, 7, 5, 6];

Etc.

Page 10: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

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;

Page 11: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

Local vs. global variables

Page 12: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

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.

Page 13: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

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.

Page 14: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

GCD function

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

Page 15: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

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).

Page 16: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

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 + 1while 1 if isprime(n) break; else n = n + 1; end;end;

Page 17: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

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

Page 18: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

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

Solution:

Page 19: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

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*

Page 20: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

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:

Page 21: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

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.

Page 22: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

Exercise 6.6

>> BooleanToTF([1 0 0 1 1 0 1 0])ans = ‘TFFTTFTF’

etc.

Page 23: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

Exercise 6.6

>> BooleanToTF([1 0 0 1 1 0 1 0])ans = ‘TFFTTFTF’

etc.

function res = BooleanToTF(bool)res = char('F' + zeros(size(bool)));res(bool) = 'T';

Page 24: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

Exercise: write a function in Matlab to convert from decimal to binary. The output should be a string.

Page 25: Lecture 7                                           Sept 17 Goals:  Complete Chapter 4

Exercise: write a function in Matlab to convert from decimal to binary. The output should be a string.

function bin = dec2bin(n)

if n == 0 bin = '0';

else

bin= '';

while ~(n==0)

if mod(n,2)==0 bit = '0'

else bit = '1';

end;

bin= strcat(bin, bit);

n = floor(n/2);

end;

end;