Unformatted text preview:

Discrete Mathematics for ECE 14:332:202 Spring 2004 Recitation 1: Introduction: 1. Establish an account in the EIT Lab, and log onto a machine running Windows (if you’re here you’ve probably already done that). 2. Create a folder on your J:\ drive named J:\discmat 3. Go to the course web page: http://coewww.rutgers.edu/classes/ece/ece202 and save the two MATLAB routines (euclfor.m and euclbak.m) under J:\discmat 4. Open up MATLAB from the desktop, and change your working directory to J:\discmat a. You can do this by typing ‘cd J:\discmat’, and warning will appear. Disregard. We will be using these two routines to incorporate our understanding of Euclid’s algorithm into our usage of MATLAB. Type ‘help euclfor’. This is how you can obtain basic information about any MATLAB function. Type ‘type euclfor’. We see how a basic MATLAB function is written. Exercise:. Explore relevant MATLAB functions by typing "help name" for your functions and then for built-in Matlab functions with names: gcd, primes, isprime, and factor. Then write a calling function that calls euclfor and euclbak and returns d, m, and n, which should do much the same as Matlab's gcd function (but is easy for us to explore and, maybe, to modify). Now we will provide some background into the functions, euclfor and euclbak. Our text also provides background, particularly for its own gcd function, which is an abbreviated form of euclfor. Some tips on writing MATLAB scripts. 1. Put a semicolon after every command line, unless you wish for the result of that command to be displayed on the screen. 2. Take advantage of the matrix abilities of MATLAB, and its ability to avoid for loops – we will see this more as the course progresses. 3. Use the % at the beginning of a line to comment it out; alternatively you can use ctrl-r to comment out sections, and ctrl-t to uncomment sections Procedure: In order to create a calling function, we need to open a new MATLAB script. The name cannot begin with a number, so choose something like rec1.m or gcdtest.m – you be creative. To open, type ‘edit rec1.m’. We wish to use this script to explore our two GCD functions, so we begin by choosing two numbers. Set a = 3213 and b = 2457 by typing % Recitation 1 a = 3213; b = 2457; Now, implement the functions exactly as described in the help.[d,qvec ] = euclfor(a,b); coef = euclbak(qvec); We will use the MATLAB sprintf function, which is a way of printing formatted text to the display. (type ‘help sprintf’ for more info on this function) sprintf(‘gcd(%i, %i) = %i’,a,b,d) sprintf(‘%i = %i*%i + %i*%i’,d,coef(1),a,coef(2),b’) Now, run your script. This can be done in one of two ways. Either press the button in the editing window, or type the name of your script file from the command window: >> rec1 Or, in order to eliminate it from displaying “ans = …”, we can use the disp command: disp(sprintf(‘gcd(%i, %i) = %i’,a,b,d)) disp(sprintf(‘%i = %i*%i + %i*%i’,d,coef(1),a,coef(2),b’)) Run your script again. Additional Exercises: Implement the above calling and formatting script as a function, such that when called as ‘euclprint(a,b)’, it will display on the screen an appropriate output as shown above. This can be done by creating a new m-file by typing ‘edit euclprint.m’. Copy the required functionality from your script, except in your function you will not need to assign values to a and b. To make this work as a function, we must add as the top line: function euclprint(a,b) In general, we may want output variables so that functions can be embedded. In this case we would use the general form as the top line of our function: function [output variables] = fcn_name(input variables) for example, we could write function [d, coef] = euclprint(a,b); To call our function, we must specify the input variables and call it from the command line: >> euclprint(3213,2457) Extra Credit: Create a function that will display the information acquired from euclfor.m in a manner similar to the notes, ie the formatting will appear on the screen similar to: 3213 = 2457·1 + 756 2457 = 756·3 + 189 756 = 189·4 + 0 You may need to edit the function euclfor in order to achieve


View Full Document

Rutgers University ECE 202 - Recitation 1

Download Recitation 1
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Recitation 1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Recitation 1 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?