EE 3010

MatLab

Objective

To introduce the student to MatLab programming, making them familiar with MatLab’s basic commands and scripts.

Procedure.

Open MatLab by double-clicking on the MatLab 7.x.x icon.

Command window – this is where MatLab commands are entered.

Workspace – lists all current variables

Directory – lists the files in the current working folder

Command history – lists all previous commands


Some Basic MatLab Commands:

·  clear all / Clears workspace of all variables
·  close all / Closes all figure and plot windows
·  plot (x,y) / Plots vector “y” verses “x”
·  % / Separates comments from script
·  help / Used to find the syntax of a command

“help” is used in the following manner: help <command>, where <command> is a MatLab command.

Example:

> help close

CLOSE Close figure.

CLOSE(H) closes the window with handle H.

CLOSE, by itself, closes the current figure window.

CLOSE('name') closes the named window.

CLOSE ALL closes all the open figure windows.

CLOSE ALL HIDDEN closes hidden windows as well.

STATUS = CLOSE(...) returns 1 if the specified windows were closed

and 0 otherwise.

See also delete.

Overloaded functions or methods (ones with the same name in other directories)

help serial/close.m

help ftp/close.m

help avifile/close.m

help tlchandle/close.m

Reference page in Help browser

doc close

Basic Arithmetic Operators
+ : Arithmetic addition
- : Arithmetic subtraction
*: Arithmetic multiplication
/: Arithmetic division
^: Exponent or power (3 ^ 4 = 34)
.*: Element by element (for arrays) / Built-in Waveform Functions
·  cos(): cosine
·  sin(): sine
·  square(): generates a square wave
·  sawtooth(): generates a sawtooth wave
·  sawtooth(t,0.5): generates a triangle wave

Part A: Basic MatLab Commands (command line)

Enter the following array into MatLab

By entering the following command:

A=[1 0;2 -1]

Now, enter this array:

Record the command used to create this array in the Data Sheet.

Now, findand. Enter the results for both in the Data Sheet.

Solve the following equation for X using MatLab:

,

This reminder may help you:

Enter your results in the Data Sheet. Have your TA verify your MatLab results.

In order to plot a waveform, we need vectors representing both the independent (t) and dependent (f[t]) variables.

f[t] can easily be defined after t as shown below:

out = f[t];

Example:

out = 2*cos(t);

Now, create an array named t with values starting at 0 and ending at 2, incremented by 0.0001.

t needs to be defined first.

To DEFINE a vector, MatLab uses the following syntax:

t = [0 0.1 0.2 0.3 0.4]; which produces a vector t with five elements, 0, 0.1, 0.2, 0.3 and 0.4.

This may be simple enough for short vectors, but we need microsecond resolution to show our periodic signals clearly. Using the defining statement above would require hundreds of thousands of entries to cover only one second!

Fortunately, MatLab has a solution.

A vector can be created and filled incrementally as shown below:

array = start:increment:finish

This creates vector, filling it with values starting at “start,” ending at “finish” and incremented by “increment.”

Example:

inc = 1:3:10

inc = [1 4 7 10]

Enter this command: t=[0:0.0001:2];

Now, let’s create an associated array for a cosine wave, with a frequency of 2 Hz: Enter this command: cosine=cos(2*pi*2*t); This creates an array with a value for each index of t. This will be important later.

Now, create an array for a sine wave named “sine” with a frequency of 2 Hz, using the same array for t.

On the Data Sheet, enter the values for this array at indices 45, 1506 and 19992. “sine(45)”

Now, we will plot the sine wave. Enter “help plot” for details on the plot function.

Plot the sine wave you have created. Enter: plot(t,cosine);

A window similar to the one shown below should appear.

Figure 2: MatLab Plot

This shows your sine wave, but nothing else.

To make it look nicer, add a title, x-axis label, and y-axis label:

title('Sine wave: Frequency 2Hz');

xlabel('time (s)');

ylabel('Magintude');

Print your plot and turn it in with your report.

Part B: Sinewave Generation.

Commands can also be called as scripts. A MatLab script has the extension “.m”

To create a new “M-file” MatLab script, Click File è New è M-File.

You will now use a MatLab function to plot a sine wave. Create an M-file and save it as “sinewave.m”

Enter the following code into the M-file editor:

% EE 3010

% Generation of sine wave with given frequency, amplitude, phase, and DC offset.

clear all;

close all;

frequency = 2000; % required frequency

period = 1/frequency; % required period

amplitude = 1 ; % required amplitude

phase = 0; % required phase angle

offset = 0; % required DC offset

t = 0:0.00001:2*period;

output = amplitude * sin(2*pi*frequency*t + phase) + offset;

plot(t,output);

To execute the code, do ONE of the following:

·  Select all è Right click è Evaluate selection

·  Debug è Run

·  Press F5

·  Type the file name in the Command Window and press “Enter”

Now, add code to the M-File that sets the title to “Sine Wave Demo,” the x-axis label to “Time (s),” and the y-axis label to “Voltage (V).”

Add a title to the plot by entering: title(‘Sine Wave Demo’);

Now, add a label to the x-axis: xlabel(‘Time (t)’);

And a y-axis label: ylabel(‘Voltage (V)’); Save this plot figure to your disk.

Now, change the frequency to 1000, and save this new plot;

Now, you will produce the plots of all combinations of the following frequencies and amplitudes: Frequencies: 500,3000, 5000 Hz; Amplitudes: 1, 2.5, 4.

However, there is a way to do this without changing and resaving the script for each set of values. It would be simpler to enter the above as an argument to the script:

Sinewave(500, 1); In order to do this, the script must be configured as a function.

A MatLab function is defined in the following manner:

function output_variable function_name(argument1,argument2,…)

Add the following line to the TOP of your file:

function output = sinewave(frequency, amplitude)

Change “clear all;” to “clear global;” (This prevents your argument variables from being deleted.)

Remove the lines:

frequency = 2000; % required frequency

amplitude = 1; % required amplitude

Your code should now look as below.

function output = sinewave(frequency, amplitude)

% EE 3010

% Generation of sine wave with given frequency, amplitude, phase, and DC offset.

clear global;

close all;

period = 1/frequency; % required period

phase = 0; % required phase angle

offset = 0; % required DC offset

t = 0:0.00001:2*period;

output = amplitude * sin(2*pi*frequency*t + phase) + offset;

plot(t,output);

title('Sine Wave Demo');

xlabel('Time (s)');

ylabel('Voltage (V)');

Save the function, and try it by entering: sinewave(500,1);

It would be convenient for each plot to have a title identifying it with the give parameters. Embedded title and label statements are needed. To make a title that includes the specified frequency and amplitude, we first need to convert these numbers to strings, then concatenate (join) them with other strings to make the title.

Numbers are transformed into strings by the num2str() function.

In MatLab, Strings are concatenated in this manner:

string = [string1, string2, …];

Remove the existing title line, and insert the following code at the end of your function:

title(['SINE WAVE - Frequency ', num2str(frequency),'Hz, Amplitude: ', num2str(amplitude),'V']);

Your code should now look as below.

function output = sinewave(frequency, amplitude)

% EE 3010

% Generation of sine wave with given frequency, amplitude, phase, and DC offset.

clear global;

close all;

period = 1/frequency; % required period

phase = 0; % required phase angle

offset = 0; % required DC offset

t = 0:0.00001:2*period;

output = amplitude * sin(2*pi*frequency*t + phase) + offset;

plot(t,output);

xlabel('Time (s)');

ylabel('Voltage (V)');

title(['SINE WAVE - Frequency ', num2str(frequency),'Hz, Amplitude: ', num2str(amplitude),'V']);

Have your TA verify that your code works

Now, using this function, save the nine plots of the frequency/amplitude combinations as specified above.

Next, enter: “help sinewave;” You will get something like the below:

> help sinewave

EE 3010

Generation of sine wave with given frequency, amplitude, phase, and DC offset.

This help text is generated from the commented lines at the beginning of the script.

EDIT these comment lines to describe to the user what this function does and how to use it.

Part C. Square wave generation

Use what you have learned about MatLab programming to write a function that generates a square wave with user specified frequency and amplitude. The title should read “Square Wave Demo,” and give the frequency and amplitude entered. The x and y labels should be the same as above. Save plots with combinations of three different frequencies and three different amplitudes. Demonstrate to your TA that your function works properly.

Discussion.

Include your 11 plots from part B and your code and 9 plots from part B in your (hand in, report?) hand in to your TA.

  1. What did you find easy about this lab? What did you find hard?
  2. Discuss how you developed the code required for part C, especially giving any difficulties encountered.


Data Sheet

Name ______Section ______

Part A:

B = ______(MatLab command)

=

=

X = Verify use of MatLab, not calculator TA ______

sine(45) = ______

sine(1506) = ______

sine(19992) = ______

Part B

TA ______

Part C

TA ______