Presentation is loading. Please wait.

Presentation is loading. Please wait.

SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 6”

Similar presentations


Presentation on theme: "SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 6”"— Presentation transcript:

1 SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 6”

2 SUNY-New Paltz objectives code determinate loops with “for” code indeterminate loops with “while” Loops

3 SUNY-New Paltz Determinate repetition with for There are 4 balls: R, B, G and Y. How many combinations of pairs of 2 balls are there? (R,B), (R,G), (R,Y), (B,G), (B,Y), (G,Y)

4 Combination of r out of n SUNY-New Paltz

5 MATLAB Code

6 SUNY-New Paltz Nested for’s N=4; M=4; L=4; for i=1:N for j=1:M for k=1:L a(i,j,k)=i+j+k; end a(:,:,1) = 3 4 5 6 4 5 6 7 5 6 7 8 6 7 8 9 a(:,:,2) = 4 5 6 7 5 6 7 8 6 7 8 9 7 8 9 10 a(:,:,3) = 5 6 7 8 6 7 8 9 7 8 9 10 8 9 10 11 a(:,:,4) = 6 7 8 9 7 8 9 10 8 9 10 11 9 10 11 12

7 SUNY-New Paltz Indeterminate repetition with while

8 SUNY-New Paltz Doubling time of an investment We would like to know how long it takes for the investment to double: 1. Initialize balance, year, interest rate 2. Display headings 3. Repeat Update balance according to interest rate Display year, balance until balance exceeds twice original balance 4. Stop a = 1000; r = 0.1; bal = a; year = 0; disp( ’Year Balance’ ) while bal < 2 * a bal = bal + r * bal; year = year + 1; disp( [year bal] ) End Year Balance 1 1100.00 2 1210.00 3 1331.00 4 1464.10 5 1610.51 6 1771.56 7 1948.7

9 SUNY-New Paltz break and continue Break : exits the while loop Continue : starts a new iteration

10 SUNY-New Paltz Menus k = 0; while k ~= 3 k = menu( ’Click on your option’, ’Do this’,... ’Do that’, ’Quit’ ); if k == 1 disp( ’Do this... press any key to continue...’ ) pause elseif k == 2 disp( ’Do that... press any key to continue...’ ) pause end

11 Exercise SUNY-New Paltz Develop code to provide

12 SUNY-New Paltz Function M-files Objective To enable you to represent your own functions with inline objects and function M-files.

13 SUNY-New Paltz Inline objects harmonic oscillators: h(t) = cos(8t) + cos(9t) h = inline( ’cos(8*t) + cos(9*t)’ ); x = 0 : pi/40 : 6*pi; plot(x, h(x)), grid

14 SUNY-New Paltz Inline Objects f = inline( ’x.^2 + y.^2’, ’x’, ’y’ ); f(1, 2) ans = 5 f = inline( ’x.^2 + y’, ’y’, ’x’ ); f(1, 2) ans = 5

15 SUNY-New Paltz Function M-file function [ outarg1, outarg2,... ] = name( inarg1, inarg2,... ) % comments to be displayed with help... outarg1 =... ; outarg2 =... ;...

16 SUNY-New Paltz Function M-file function keyword Input and output arguments Multiple output arguments Naming convention for functions Help text

17 SUNY-New Paltz Definitions: Local Variables Local variables: Any variables defined inside a function are inaccessible outside the function. Scope: range of lines over which the variable is accessible

18 SUNY-New Paltz Definitions: Global Variables Global Variables:Variables which are defined in the base workspace are not normally accessible inside functions global PLINK PLONK Persistent variables: Persistent variables, however, remain in existence between function calls. function test if isempty(count) count = 1 else count = count + 1 end persistent count

19 SUNY-New Paltz Output Arguments Functions that do not return values function stars(n) asteriks = char(abs(’*’)*ones(1,n)); disp( asteriks) Vector arguments function d = dice( n ) d = floor( 6 * rand(1, n) + 1 );

20 SUNY-New Paltz How function arguments are passed Simulated pass by reference: A function may be called with the same actual input and output argument. function y = prune(x) y = x(x ~= 0); Checking the number of function arguments: A function may be called with all, some, or none of its input arguments. function y = foo(a, b, c); disp( nargin );...

21 Exercise 1.Write a MATLAB function that returns the sum of the squares of its arguments: Sumofsquers(a, b, c, d, …) 2.Write a MATLAB function that returns the concatenate of its arguments: concat(a, b, c, d, …) Use this function in some application. SUNY-New Paltz

22 Subfunctions A function M-file may contain the code for more than one function. The first function in a file is the primary function, and is the one invoked with the M-file name. Additional functions in the file are called subfunctions, and are visible only to the primary function and to other subfunctions.

23 SUNY-New Paltz Function name resolution 1. Checks if the name is a variable. 2. Checks if the name is a subfunction of the calling function. 3. Checks if the name is a private function. 4. Checks if the name is in the directories specified by MATLAB’s search path

24 SUNY-New Paltz Debugging M-files Open test.m with the MATLAB Editor/Debugger Set breakpoints Run Step Go Until Cursor

25 SUNY-New Paltz Examine Variables 1. Position the cursor to the left of the variable in the Editor. Its current value appears in a box—this is called a datatip. 2. Type the name of the variable in the Command Window. 3. Use the Array Editor: open the Workspace browser and double-click a variable.

26 SUNY-New Paltz Debugging a function Open newtfun.m in the Editor/Debugger: Set a breakpoint In the Command Window set up function handles fhand = @f; dfhand = @df; [x f conv] = newtfun(fhand, dfhand, 10) Note that MATLAB goes into debug mode and takes you to the breakpoint in newtfun.

27 SUNY-New Paltz Recursion n! = n × (n − 1)! function y = fact(n) % FACT Recursive definition of n! if n > 1 y = n * fact(n-1); else y = 1; end;


Download ppt "SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 6”"

Similar presentations


Ads by Google