Answers_of_exercises..

advertisement
Exercise 1 (control structure)
Create an M-file and write these codes down.
Clear all;
Mymatrix = [1:5; 14:2:22; 50:-7:22];
comments
for i = 1:3
for j = 1:5
if Mymatrix(i,j)< 5 | Mymatrix(i,j) > 20
newmatrix(i,j) = Mymatrix(i,j);
elseif Mymatrix(i,j) == 20
newmatrix(i,j) = 100;
else
newmatrix(i,j) = 0;
end
end
end
% use the percent sign to indicate
%semicolon means
%Matlab won’t display
Answer of Exercise 1
Mymatrix =
1
14
50
2
16
43
3
18
36
4
20
29
5
22
22
3
0
36
4
100
29
0
22
22
newmatrix =
1
0
50
2
0
43
Exercise 2 (Basic plotting)
1. Plot x versus y for y=sin(x). Let x vary from 0 to 2pi in increments of 0.1pi.
2. Add a title and labels to your plot.
3. Plot x versus y1 and y2 for y1=sin(x) and y2=cos(x). Let x vary from 0 to 2pi in
increments of 0.1pi. Add a title and labels to your plot.
4. Re-create the plot from part 3, but make the sin(x) line dashed and red. Make the cos(x)
line green and dotted.
Answer of Exercise 2
1. clear, clc
x=0:0.1*pi:2*pi;
1
y=sin(x);
plot(x,y)
2. title('Sinusoidal Curve')
xlabel('x value')
ylabel('sin(x) ')
3. figure(2)
y1=sin(x);
y2=cos(x);
plot(x,y1,x,y2)
title('Sine and Cosine Plots')
xlabel('x values')
ylabel('y values')
4. figure(3)
plot(x,y1, '--r',x,y2, ':g' )
title('Sine and Cosine Plots')
xlabel('x values')
ylabel('y values')
Exercise 3 (Subplots)
1. Subdivided a figure window into one row and two columns.
2. In the left window, plot y=tan(x) for -1.5<x<1.5. Use an increment of 0.1.
3. Add a title and axis labels to your graph.
4. In the right window, plot y=sinh(x) for the same range.
5. Add a title and axis
labels to this graph.
Answer of Exercise 3
1. subplot(1,2,1)
2. x=-1.5:0.1:1.5;
2
y=tan(x);
plot(x,y)
3. title('Tangent(x)')
xlabel('x values')
ylabel('y values')
4. subplot(1,2,2)
y=sinh(x);
plot(x,y)
5. title('Hyperbolic sine of x')
xlabel('x values')
ylabel('y values')
Exercise 4 (Histogram)
Suppose that x = [1, 2, 3, 4, 6, 7, 8, 9, 10, 12, 14, 17, 19, 23, 29, 30, 31, 32, 35, 40, 57, 66, 67, 68,
80, 90, 91, 100],
1. Subdivided a figure window into two rows and one column.
2. In the top window, plot the histogram of x with the default number of bins.
3. Add a title to your graph.
4. In the bottom window, plot the histogram of x with 20 bins.
5. Add a title to this graph.
Answer of Exercise 4
1. subplot(2,1,1)
2.x=[1,2,3,4,6,7,8,9,10,12,14,17,19,23,29,30,31
,32,35,40,57,66,67,68,80,90,91,100];
hist(x)
3. title('Histogram of x with 10 bins')
4. subplot(2,1,2)
hist(x, 20)
5. title(' Histogram of x with 20 bins ')
3
Download