Matlab: Objective
Matlab: Objective
Matlab: Objective
MatLab
Objective
To introduce the student to MatLab programming, making them familiar with MatLab’s
basic commands and scripts.
Procedure.
Now, find A B and B A . Enter the results for both in the Data Sheet.
4
A X C , C
2
1 0 x1 4
2 1 x 2 This reminder may help you: A 1 A X A 1C
2
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.
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 = [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!
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]
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.
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');
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”
% 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);
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).”
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.
Change “clear all;” to “clear global;” (This prevents your argument variables
from being deleted.)
% 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)');
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.
Remove the existing title line, and insert the following code at the end of your function:
% 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']);
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:
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.
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
Part A:
A B =
B A =
X =
Verify use of MatLab, not calculator TA ________
sine(45) = ______________
sine(1506) = ______________
sine(19992) = ______________
Part B
TA ________
Part C
TA ________