Beruflich Dokumente
Kultur Dokumente
1 of 10
file:///F:/Documents%20Saskia/2%20Education/2%20ML/MinIV%20...
Contents
Matlab 1:Introduction Matlab
Exercise 2.1
Exercise 2.2
Exercise 2.3
Exercise 2.4
Exercise 3.1
Exercise 3.2
Exercise 4.1
Exercise 4.1c
Exercise 4.1d
Matlab 1:Introduction Matlab
% % Aug 2011, Saskia Monsma
%
%
%
%
%
By eveluating a cel in this m-file you can see the results for every exercise
Normally in a m-file the output to the command window is suppressed by
adding a semicolon at the end. This is not done here to be able to view
the results for every step in the command window.
Exercise 2.1
clc, clear % empty command window and workspace
% a
A = [1:2:10;5:5:25]
%b
Z = sum(A)
%c
Z = Z'
%d
A(3,1) = 1
%e
A = [A; 1:5; 20:10:60]
%f
sum(diag(A))
A =
1
5
3
10
5
15
7
20
9
25
13
20
27
34
3
10
0
5
15
0
7
20
0
9
25
0
Z =
Z =
6
13
20
27
34
A =
1
5
1
30/08/11 17:12
mfile_1_Matlab
2 of 10
file:///F:/Documents%20Saskia/2%20Education/2%20ML/MinIV%20...
A =
1
5
1
1
20
3
10
0
2
30
5
15
0
3
40
7
20
0
4
50
9
25
0
5
60
ans =
75
Exercise 2.2
clc, clear
%a
disp('Content of B is random, so will differ for every execution of this command')
B = rand(3,2)
%b
B(3,2) = .5
%c
C = 10*B
% or C = 10.*B
%d
D = [B,C]
%e
E = [B;C]
%f
D(2,:) = []
0.9134
0.6324
0.0975
0.8147
0.9058
0.1270
0.9134
0.6324
0.5000
8.1472
9.0579
1.2699
9.1338
6.3236
5.0000
0.8147
0.9058
0.1270
0.9134
0.6324
0.5000
0.8147
0.9058
0.9134
0.6324
B =
C =
D =
8.1472
9.0579
1.2699
9.1338
6.3236
5.0000
E =
30/08/11 17:12
mfile_1_Matlab
3 of 10
file:///F:/Documents%20Saskia/2%20Education/2%20ML/MinIV%20...
0.1270
8.1472
9.0579
1.2699
0.5000
9.1338
6.3236
5.0000
0.8147
0.1270
0.9134
0.5000
D =
8.1472
1.2699
9.1338
5.0000
Exercise 2.3
clc, clear
%a
x = (1:6)'% using ' converts the row vector to a column vector
% or: x = [1;2;3;4;5;6]
%b
X = [x, sqrt(x), x+100, x.^3]
% do not forget the dot in x.^3
x =
1
2
3
4
5
6
X =
1.0000
2.0000
3.0000
4.0000
5.0000
6.0000
1.0000
1.4142
1.7321
2.0000
2.2361
2.4495
101.0000
102.0000
103.0000
104.0000
105.0000
106.0000
1.0000
8.0000
27.0000
64.0000
125.0000
216.0000
Exercise 2.4
type 'linspace' in the command window and press F1 te get help
clc, clear
%a
alfa = linspace(-pi, pi, 10)
%b
S = sin(4*alfa)' % using ' converts the row vector to a column vector
alfa =
Columns 1 through 8
-3.1416
-2.4435
-1.7453
-1.0472
-0.3491
0.3491
1.0472
1.7453
Columns 9 through 10
2.4435
3.1416
S =
30/08/11 17:12
mfile_1_Matlab
4 of 10
file:///F:/Documents%20Saskia/2%20Education/2%20ML/MinIV%20...
0.0000
0.3420
-0.6428
0.8660
-0.9848
0.9848
-0.8660
0.6428
-0.3420
-0.0000
Exercise 3.1
clc, clear
% a
t = 0:5:100
v = [0 4 10 14 14 18 20 22 25 25 26 25 28 23 20 10 8 8 4 1 1]
% b
plot(t,v)
% c
xlabel('time (s)')
ylabel('speed (m/s)')
% d
title('Measured speed of the first 100 seconds')
% e
legend('speed v')
% f
figure
plot(v) % Matlab takes the index values as x-values
t =
Columns 1 through 13
0
10
15
20
25
30
35
80
85
90
95
100
14
14
18
20
22
40
45
50
55
60
25
25
26
25
28
Columns 14 through 21
65
70
75
v =
Columns 1 through 13
0
10
Columns 14 through 21
23
20
10
30/08/11 17:12
mfile_1_Matlab
5 of 10
file:///F:/Documents%20Saskia/2%20Education/2%20ML/MinIV%20...
Exercise 3.2
clc, clear, close all % clear command winsow, workspace and figures
% a
t = 0:5:100
v = [0 4 10 14 14 18 20 22 25 25 26 25 28 23 20 10 8 8 4 1 1]
30/08/11 17:12
mfile_1_Matlab
6 of 10
file:///F:/Documents%20Saskia/2%20Education/2%20ML/MinIV%20...
v2 = [0 3 7 8 12 19 24 30 33 34 30 24 24 20 14 8 4 2 0 0 0]
% b
plot(t,v,'k-+', t,v2,'r:*')
% c
xlabel('time (s)')
ylabel('speed (m/s)')
title('Measured speed of the first 100 seconds')
% d
legend('speed v', 'speed v2')
t =
Columns 1 through 13
0
10
15
20
25
30
35
80
85
90
95
100
14
14
18
20
22
12
19
24
30
40
45
50
55
60
25
25
26
25
28
33
34
30
24
24
Columns 14 through 21
65
70
75
v =
Columns 1 through 13
0
10
Columns 14 through 21
23
20
10
v2 =
Columns 1 through 13
0
Columns 14 through 21
20
14
30/08/11 17:12
mfile_1_Matlab
7 of 10
file:///F:/Documents%20Saskia/2%20Education/2%20ML/MinIV%20...
Exercise 4.1
clc, clear, close all % command window en workspace leeg maken
% a
% graph of x-squared
% b
x = -10:0.01:10;
y1 = x.^2;
% do not forget the dot
y2 = 0.5*x.^2;
plot(x,y1,x,y2)
xlabel ('x')
ylabel ('y')
legend ('x^2','0.5*x^2')
30/08/11 17:12
mfile_1_Matlab
8 of 10
file:///F:/Documents%20Saskia/2%20Education/2%20ML/MinIV%20...
Exercise 4.1c
x = -15:0.01:15; % only this is adjusted
y1 = x.^2;
y2 = 0.5*x.^2;
plot(x,y1,x,y2)
xlabel ('x')
ylabel ('y')
legend ('x^2','0.5*x^2')
30/08/11 17:12
mfile_1_Matlab
9 of 10
file:///F:/Documents%20Saskia/2%20Education/2%20ML/MinIV%20...
Exercise 4.1d
x = -15:0.01:15;
y = x.^3;
y2 = 0.5*x.^3;
plot(x,y,x,y2)
xlabel ('x')
ylabel ('y')
legend ('x^3','0.5*x^3')
30/08/11 17:12
mfile_1_Matlab
10 of 10
file:///F:/Documents%20Saskia/2%20Education/2%20ML/MinIV%20...
30/08/11 17:12