## 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?

### 3. What does close all do?

### 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?

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

### 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.

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

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

### 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.

### 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.

## 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.

### 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.

### 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)’);

ylabel(‘Vout/Vin’);

title(‘Low Pass Filter’);

help plot

### 5. Repeat 1-3 by modifying the circuit to a 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)’)

ylabel(‘Vout/Vin’)

title(‘High Pass Filter’);

I like how you guys put pictures of the actual codes from MATLAB, it makes it very easy to read and understand.

LikeLike

Thanks, we’ll try to keep it that way for all the code in the future.

LikeLike

Thank you! We will continue to put pictures of the code we use then for the remainder of the course.

LikeLike

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.

LikeLike

Thank you for the suggestion! We will try to update our formatting and coloring for the remaining blog entries.

LikeLike

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.

LikeLike

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.

LikeLike

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.

LikeLike

I LOVE IT!

LikeLike

WE LOVE YOU!

LikeLike

Good job, thanks.

LikeLike

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.

LikeLike

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

LikeLike