Week 10

MatLAB Practice

1. Open MATLAB. Open the editor and copy paste the following code. Name your code as

FirstCode.m Save the resulting plot as a JPEG image and put it here.

clear all;
close all;
x = [1 2 3 4 5];
y = 2.^x;
plot(x, y, ‘LineWidth’, 6)
xlabel(‘Numbers’, ‘FontSize’, 12)
ylabel(‘Results’, ‘FontSize’, 12)

2. What does clear all do?

 “clear all” clears all objects in the MATLAB workspace and removes any assumptions (aka variables).

3. What does close all do?

 “close all” removes all figures that are not hidden.

4. In the command line, type x and press enter. This is a matrix. How many rows and columns are there in the matrix?


x is a 5 by 1 matrix. This means there are 5 columns and 1 row.


5. Why is there a semicolon at the end of the line of x and y?

The semicolon indicates to MATLAB that the output of x and y should not be displayed in the following lines, however the code is still implemented.

6. Remove the dot on the y = 2.^x; line and execute the code again. What does the error message mean?

The matrix x is not a square matrix; therefore the .^ operator is necessary to perform the ^ operation.

7. How does the LineWidth affect the plot? Explain.

 LineWidth effects the thickness of the graph’s line.

8. Type help plot on the command line and study the options for plot command. Provide how you would change the line for plot command to obtain the following figure (Hint: Like LineWidth, there is another property called MarkerSize)


Include ‘MarkerSize’ and a large number following it (around 10).

9. What happens if you change the line for x to x = [1; 2; 3; 4; 5];? Explain.

Changing x to x = [1; 2; 3; 4; 5]; would make the matrix a 1 by 5 rather than 5 by 1. This would be 1 column and 5 rows. The semicolons within the bracket create a new row.

11. Degree vs. radian in MATLAB:

a. Calculate sinus of 30 degrees using a calculator or internet.


sin(30) = 1/2 = 0.5

b. Type sin(30) in the command line of the MATLAB. Why is this number different? (Hint: MATLAB treats angles as radians).

This is because MatLAB uses radians, for its trigonometric functions.

c. How can you modify sin(30) so we get the correct number?

To convert from degrees to radians:
some degree value * pi / 180 = some radian value
thus sin(30 * pi / 180) = 0.5

12. Plot y = 10 sin (100 t) using Matlab with two different resolutions on the same plot: 10 points per period and 1000 points per period. The plot needs to show only two periods. Commands you might need to use are linspace, plot, hold on, legend, xlabel, and ylabel. Provide your code and resulting figure. The output figure should look like the following:





The code for the preceding graph.


13. Explain what is changed in the following plot comparing to the previous one.

q13 quesiton

 The graph is maximized at y = 5 for the fine output.

14. The command find was used to create this code. Study the use of find (help find) and try to replicate the plot above. Provide your code.

The code to get the graph from number 13.

Filters and MatLAB

1. Build a low pass filter using a resistor and capacitor in which the cut off frequency is 1 kHz. Observe the output signal using the oscilloscope. Collect several data points particularly around the cut off frequency. Provide your data in a table.

f_c = 1/2piRC; thus R = 1/2000piC , where C =  22*10^-9
thus R must be 7.234kOhms.
Screenshot 2016-03-22 at 3.13.49 PM

2. Plot your data using MATLAB. Make sure to use proper labels for the plot and make your plot line and fonts readable. Provide your code and the plot.

The low pass filter, graphed in MATLAB.
W10PartBQ2.png The code to get the preceding graph.

3. Calculate the cut off frequency using MATLAB. find command will be used. Provide your code.

Frequency = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0];

Output = [0.728, 0.718, 0.704, 0.686, 0.664, 0.64, 0.614, 0.59, 0.564, 0.54, 0.514, 0.486, 0.466, 0.446, 0.426, 0.408, 0.392, 0.376, 0.362, 0.348];

k = [0.707];

y = linspace(0,1,50);


plot(Frequency, Output, ‘r-o’)

hold on;

plot(k, y, ‘ko’, ‘MarkerSize’, 1)


xlabel(‘Time (Period)’);


title(‘Low Pass Filter’);

help plot

5. Repeat 1-3 by modifying the circuit to a high pass filter.

Screenshot 2016-03-22 at 3.14.04 PM

 High Pass Filter Final
Graph showing the curve for the high pass filter

Frequency = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0];

Output = [0.0666, 0.131, 0.1914, 0.248, 0.302, 0.348, 0.388, 0.424, 0.456, 0.484, 0.508, 0.534, 0.552, 0.568, 0.582, 0.596, 0.606, 0.616, 0.624, 0.632];

line = Frequency * 0 + 0.707;

plot(Frequency, Output, ‘k’)
hold on;
plot(Frequency, line, ‘b’, ‘LineWidth’ , 1)
hold on;
plot(xint,yint, ‘black’)

xlabel(‘Frequency (kHz)’)

title(‘High Pass Filter’);


13 thoughts on “Week 10

  1. I agree that using pictures of your code made it easier to read. However, I found reading the rest of the text on the blog to be a bit difficult to read. I think the biggest problems are the use of caps and there is some inconsistent spacing. Maybe it’s just me though. A group suggested we use different colors for our questions and answers, and that really improved the look of our blog.


  2. Very organized blog. You guys did a nice job. Like Amanda said, I would recommend using different color fonts to make it a little smoother to read, rather than just different size. Or maybe underlining one of them.


  3. I like how you explained how the semi-colons changed the code in question 5, I didn’t realized it actually created a vertical matrix until I read your explanation.

    I would suggest wrapping the text in the top line of your data tables so you don’t need to include so many empty columns in your screenshots, and can make the actual data columns a bit larger and easier to read.


    1. I’m glad that our explanation helped with your understanding of the code! Thank you for the suggestion about the data tables. You are right that there is a lot of unnecessary empty fields in those images.


  4. I like that you included screenshots of your matlab code for some of the comments instead of the regular blog text. It is very easy to read and organized.


  5. I like that you screenshotted your matlab code! Very organized and much easier to read than the regular blog text.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s