ece210/hw2/notes/octave-log.txt

1379 lines
27 KiB
Plaintext

[cat@lazarus:~/classes/ece210-materials/2024-van-west/lessons/lesson02]
$ octave
GNU Octave, version 7.3.0
octave:2> x = 0:0.01:2*pi; % Create a linearly-spaced vector
y = sin(x); % sin() works element-wise on vectors!
y = abs(x); % same with abs()!
y = x .^ 4; % element-wise power
y = power(x, 4); % same as above
plot(x, y); % Plot y vs. x (line graph)
title('y vs. x');
octave:9> close all
octave:10>
octave:10>
octave:10>
octave:10>
octave:10>
y = sin(x); % sin() works element-wise on vectors!
y = abs(x); % same with abs()!
y = x .^ 4; % element-wise power
y = power(x, 4); % same as above
plot(x, y); % Plot y vs. x (line graph)
title('y vs. x');^[[201~octave:10>
octave:10>
octave:10>
octave:10>
octave:10> x = 0:0.01:2*pi; % Create a linearly-spaced vector
y = sin(x); % sin() works element-wise on vectors!
y = abs(x); % same with abs()!
y = x .^ 4; % element-wise power
y = power(x, 4); % same as above
plot(x, y); % Plot y vs. x (line graph)
title('y vs. x');
octave:17> close all
octave:18>
y = x .^ 4; % element-wise power
y = power(x, 4); % same as above
plot(x, y); % Plot y vs. x (line graph)
title('y vs. x');^[[201~octave:18>
octave:18>
octave:18>
octave:18> x = 0:0.01:2*pi; % Create a linearly-spaced vector
y = sin(x); % sin() works element-wise on vectors!
y = abs(x); % same with abs()!
y = x .^ 4; % element-wise power
y = power(x, 4); % same as above
plot(x, y); % Plot y vs. x (line graph)
title('y vs. x');
octave:25> close all
octave:26>
octave:26>
octave:26>
octave:26>
octave:26>
octave:26>
octave:26>
octave:26>
octave:26> x = 0:0.01:2*pi; % Create a linearly-spaced vector
y = sin(x); % sin() works element-wise on vectors!
y = abs(x); % same with abs()!
y = x .^ 4; % element-wise power
y = power(x, 4); % same as above
plot(x, y); % Plot y vs. x (line graph)
title('y vs. x');
octave:33>
octave:33>
octave:33>
octave:33>
octave:33> x = 0:0.01:2*pi; % Create a linearly-spaced vector
y = sin(x); % sin() works element-wise on vectors!
y = abs(x); % same with abs()!
y = x .^ 4; % element-wise power
y = power(x, 4); % same as above
plot(x, y); % Plot y vs. x (line graph)
title('y vs. x');
octave:40> close all
octave:41>
y = abs(x); % same with abs()!
y = x .^ 4; % element-wise power
y = power(x, 4); % same as above
octave:53> x = 1:5:10
x =
1 6
octave:54> x = 1:10
x =
1 2 3 4 5 6 7 8 9 10
octave:55> x(1)
ans = 1
octave:56> x(1:3)
ans =
1 2 3
octave:57> x(2:7)
ans =
2 3 4 5 6 7
octave:58> x = 0:9
x =
0 1 2 3 4 5 6 7 8 9
octave:59> x = linspace(0, 1, 10)
x =
Columns 1 through 8:
0 0.1111 0.2222 0.3333 0.4444 0.5556 0.6667 0.7778
Columns 9 and 10:
0.8889 1.0000
octave:60> x = linspace(0, 1, 10);
octave:61> x = linspace(0, 1, 10).';
octave:62> x
x =
0
0.1111
0.2222
0.3333
0.4444
0.5556
0.6667
0.7778
0.8889
1.0000
octave:63> x(2:6)
ans =
0.1111
0.2222
0.3333
0.4444
0.5556
octave:64> x([1 3 5 7])
ans =
0
0.2222
0.4444
0.6667
octave:65> x(:)
ans =
0
0.1111
0.2222
0.3333
0.4444
0.5556
0.6667
0.7778
0.8889
1.0000
octave:66> x(end)
ans = 1
octave:67> x(end:-1:3)
ans =
1.0000
0.8889
0.7778
0.6667
0.5556
0.4444
0.3333
0.2222
octave:68> x(end:-1:1)
ans =
1.0000
0.8889
0.7778
0.6667
0.5556
0.4444
0.3333
0.2222
0.1111
0
octave:69> x(1:2:end)
ans =
0
0.2222
0.4444
0.6667
0.8889
octave:70> M = 1:100;
octave:71> size(M)
ans =
1 100
octave:72> help reshape
'reshape' is a built-in function from the file libinterp/corefcn/data.cc
-- reshape (A, M, N, ...)
-- reshape (A, [M N ...])
-- reshape (A, ..., [], ...)
-- reshape (A, SIZE)
Return a matrix with the specified dimensions (M, N, ...) whose
elements are taken from the matrix A.
The elements of the matrix are accessed in column-major order (like
Fortran arrays are stored).
The following code demonstrates reshaping a 1x4 row vector into a
2x2 square matrix.
reshape ([1, 2, 3, 4], 2, 2)
=> 1 3
2 4
Note that the total number of elements in the original matrix
('prod (size (A))') must match the total number of elements in the
new matrix ('prod ([M N ...])').
A single dimension of the return matrix may be left unspecified and
Octave will determine its size automatically. An empty matrix ([])
is used to flag the unspecified dimension.
See also: resize, vec, postpad, cat, squeeze.
Additional help for built-in functions and operators is
available in the online version of the manual. Use the command
'doc <topic>' to search the manual index.
Help and information about Octave is also available on the WWW
at https://www.octave.org and via the help@octave.org
mailing list.
octave:73> N1 = reshape(M, round(sqrt(100)), [])
N1 =
1 11 21 31 41 51 61 71 81 91
2 12 22 32 42 52 62 72 82 92
3 13 23 33 43 53 63 73 83 93
4 14 24 34 44 54 64 74 84 94
5 15 25 35 45 55 65 75 85 95
6 16 26 36 46 56 66 76 86 96
7 17 27 37 47 57 67 77 87 97
8 18 28 38 48 58 68 78 88 98
9 19 29 39 49 59 69 79 89 99
10 20 30 40 50 60 70 80 90 100
octave:74> N1
N1 =
1 11 21 31 41 51 61 71 81 91
2 12 22 32 42 52 62 72 82 92
3 13 23 33 43 53 63 73 83 93
4 14 24 34 44 54 64 74 84 94
5 15 25 35 45 55 65 75 85 95
6 16 26 36 46 56 66 76 86 96
7 17 27 37 47 57 67 77 87 97
8 18 28 38 48 58 68 78 88 98
9 19 29 39 49 59 69 79 89 99
10 20 30 40 50 60 70 80 90 100
octave:75> N1(:, :)
ans =
1 11 21 31 41 51 61 71 81 91
2 12 22 32 42 52 62 72 82 92
3 13 23 33 43 53 63 73 83 93
4 14 24 34 44 54 64 74 84 94
5 15 25 35 45 55 65 75 85 95
6 16 26 36 46 56 66 76 86 96
7 17 27 37 47 57 67 77 87 97
8 18 28 38 48 58 68 78 88 98
9 19 29 39 49 59 69 79 89 99
10 20 30 40 50 60 70 80 90 100
octave:76> N1(1:3, :)
ans =
1 11 21 31 41 51 61 71 81 91
2 12 22 32 42 52 62 72 82 92
3 13 23 33 43 53 63 73 83 93
octave:77> N1(1:3, 2:6)
ans =
11 21 31 41 51
12 22 32 42 52
13 23 33 43 53
octave:78> N1(1:3, 1:3)
ans =
1 11 21
2 12 22
3 13 23
octave:79> N1(end-3:end)
ans =
97 98 99 100
octave:83>
Display all 1790 possibilities? (y or n)
abs
accumarray
__accumarray_max__
__accumarray_min__
__accumarray_sum__
accumdim
octave:84> x = 3
x = 3
octave:85> x = [1 2 3];
octave:86> x = 0:15
x =
Columns 1 through 15:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Column 16:
15
octave:87> y = x.^2
y =
Columns 1 through 13:
0 1 4 9 16 25 36 49 64 81 100 121 144
Columns 14 through 16:
169 196 225
octave:88> x
x =
Columns 1 through 15:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Column 16:
15
octave:89> xt = x.'
xt =
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
octave:90> x = (1:9).'
x =
1
2
3
4
5
6
7
8
9
octave:91> sin(x)
ans =
0.8415
0.9093
0.1411
-0.7568
-0.9589
-0.2794
0.6570
0.9894
0.4121
octave:92> rad2deg(pi/2)
ans = 90
octave:93> length(x)
ans = 9
octave:94> mean(x)
ans = 5
octave:95> x
x =
1
2
3
4
5
6
7
8
9
octave:96> mean(sin(x))
ans = 0.2172
octave:97>
^[[200~T = 1e-6; % Sampling period (s)
t = 0:T:2e-3; % Time (domain/x-axis)
f0 = 50; % Initial frequency (Hz)
b = 10e6; % Chirp rate (Hz/s)
A = 10; % Amplitude
y1 = A * cos(2*pi*f0*t + pi*b*t.^2);
figure;
plot(t,y1);^[[201~octave:97>
octave:97>
octave:97>
octave:97>
octave:97> T = 1e-6; % Sampling period (s)
t = 0:T:2e-3; % Time (domain/x-axis)
f0 = 50; % Initial frequency (Hz)
b = 10e6; % Chirp rate (Hz/s)
A = 10; % Amplitude
y1 = A * cos(2*pi*f0*t + pi*b*t.^2);
figure;
plot(t,y1);
octave:105> close all
octave:109> x = linspace(0, 1, 9)
x =
Columns 1 through 8:
0 0.1250 0.2500 0.3750 0.5000 0.6250 0.7500 0.8750
Column 9:
1.0000
octave:110> x = linspace(0, 1, 9).'
x =
0
0.1250
0.2500
0.3750
0.5000
0.6250
0.7500
0.8750
1.0000
octave:111> x(1)
ans = 0
octave:112> x(9)
ans = 1
octave:113> x(10)
error: x(10): out of bound 9 (dimensions are 9x1)
octave:114> x(5)
ans = 0.5000
octave:115> x([1 2 3])
ans =
0
0.1250
0.2500
octave:116> x([1 3 2])
ans =
0
0.2500
0.1250
octave:117> x(1:9)
ans =
0
0.1250
0.2500
0.3750
0.5000
0.6250
0.7500
0.8750
1.0000
octave:118> x(1:end)
ans =
0
0.1250
0.2500
0.3750
0.5000
0.6250
0.7500
0.8750
1.0000
octave:119> x(end)
ans = 1
octave:120> x(3:end)
ans =
0.2500
0.3750
0.5000
0.6250
0.7500
0.8750
1.0000
octave:121> x(end:-1:1)
ans =
1.0000
0.8750
0.7500
0.6250
0.5000
0.3750
0.2500
0.1250
0
octave:122> 9:-1:1
ans =
9 8 7 6 5 4 3 2 1
octave:123> 9:-.5:1
ans =
Columns 1 through 7:
9.0000 8.5000 8.0000 7.5000 7.0000 6.5000 6.0000
Columns 8 through 14:
5.5000 5.0000 4.5000 4.0000 3.5000 3.0000 2.5000
Columns 15 through 17:
2.0000 1.5000 1.0000
octave:124> x(:)
ans =
0
0.1250
0.2500
0.3750
0.5000
0.6250
0.7500
0.8750
1.0000
octave:125> A = [1 2; 3 4]
A =
1 2
3 4
octave:126> A(:)
ans =
1
3
2
4
octave:127> z = 4
z = 4
octave:128> z(1)
ans = 4
octave:129> inc
error: 'inc' undefined near line 1, column 1
octave:130> a++
error: in x++ or ++x, x must be defined first
octave:131> ++x
ans =
1.0000
1.1250
1.2500
1.3750
1.5000
1.6250
1.7500
1.8750
2.0000
octave:132> M = reshape(1:100, 10, 10)
M =
1 11 21 31 41 51 61 71 81 91
2 12 22 32 42 52 62 72 82 92
3 13 23 33 43 53 63 73 83 93
4 14 24 34 44 54 64 74 84 94
5 15 25 35 45 55 65 75 85 95
6 16 26 36 46 56 66 76 86 96
7 17 27 37 47 57 67 77 87 97
8 18 28 38 48 58 68 78 88 98
9 19 29 39 49 59 69 79 89 99
10 20 30 40 50 60 70 80 90 100
octave:133> M(1, 1)
ans = 1
octave:134> M(10, 10)
ans = 100
octave:135> M(end, end)
ans = 100
octave:136> M(end, 1)
ans = 10
octave:137> M(1, end)
ans = 91
octave:138> M(1:3, :)
ans =
1 11 21 31 41 51 61 71 81 91
2 12 22 32 42 52 62 72 82 92
3 13 23 33 43 53 63 73 83 93
octave:139> M(1:3, 2:5)
ans =
11 21 31 41
12 22 32 42
13 23 33 43
octave:140> M([1 3 5 7], 2:5)
ans =
11 21 31 41
13 23 33 43
15 25 35 45
17 27 37 47
octave:141> x
x =
1.0000
1.1250
1.2500
1.3750
1.5000
1.6250
1.7500
1.8750
2.0000
octave:142> x(1:5)
ans =
1.0000
1.1250
1.2500
1.3750
1.5000
octave:143> x(1:5) = 1:5
x =
1.0000
2.0000
3.0000
4.0000
5.0000
1.6250
1.7500
1.8750
2.0000
octave:144> x(6:end) = x(6:end).^2
x =
1.0000
2.0000
3.0000
4.0000
5.0000
2.6406
3.0625
3.5156
4.0000
octave:145> M
M =
1 11 21 31 41 51 61 71 81 91
2 12 22 32 42 52 62 72 82 92
3 13 23 33 43 53 63 73 83 93
4 14 24 34 44 54 64 74 84 94
5 15 25 35 45 55 65 75 85 95
6 16 26 36 46 56 66 76 86 96
7 17 27 37 47 57 67 77 87 97
8 18 28 38 48 58 68 78 88 98
9 19 29 39 49 59 69 79 89 99
10 20 30 40 50 60 70 80 90 100
octave:146> M(3:6, 3:6) = 0
M =
1 11 21 31 41 51 61 71 81 91
2 12 22 32 42 52 62 72 82 92
3 13 0 0 0 0 63 73 83 93
4 14 0 0 0 0 64 74 84 94
5 15 0 0 0 0 65 75 85 95
6 16 0 0 0 0 66 76 86 96
7 17 27 37 47 57 67 77 87 97
8 18 28 38 48 58 68 78 88 98
9 19 29 39 49 59 69 79 89 99
10 20 30 40 50 60 70 80 90 100
octave:147> x
x =
1.0000
2.0000
3.0000
4.0000
5.0000
2.6406
3.0625
3.5156
4.0000
octave:148> x = 1:9.'
x =
1 2 3 4 5 6 7 8 9
octave:149> x = (1:9).'
x =
1
2
3
4
5
6
7
8
9
octave:150> reshape(x, 3, 3)
ans =
1 4 7
2 5 8
3 6 9
octave:151> x = 1:9
x =
1 2 3 4 5 6 7 8 9
octave:152> reshape(x, 3, 3)
ans =
1 4 7
2 5 8
3 6 9
octave:153> x
x =
1 2 3 4 5 6 7 8 9
octave:154> xm = reshape(x, 3, 3)
xm =
1 4 7
2 5 8
3 6 9
octave:155> xm(1, 1)
ans = 1
octave:156> xm(1)
ans = 1
octave:157> xm(2)
ans = 2
octave:158> xm(3)
ans = 3
octave:159> xm(4)
ans = 4
octave:160> xm
xm =
1 4 7
2 5 8
3 6 9
octave:161> reshape(xm, 9, 1)
ans =
1
2
3
4
5
6
7
8
9
octave:162> mx
error: 'mx' undefined near line 1, column 1
octave:163> xm
xm =
1 4 7
2 5 8
3 6 9
octave:164> reshape(xm, 9, 1)
ans =
1
2
3
4
5
6
7
8
9
octave:165> reshape(xm, 9, [])
ans =
1
2
3
4
5
6
7
8
9
octave:166> reshape(xm, 3, [])
ans =
1 4 7
2 5 8
3 6 9
octave:167> xm
xm =
1 4 7
2 5 8
3 6 9
octave:168> xm(2, :) = []
xm =
1 4 7
3 6 9
octave:169> magic(4)
ans =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
octave:170> sum(magic(4))
ans =
34 34 34 34
octave:171> sum(magic(4), 2)
ans =
34
34
34
34
octave:172>
^[[200~lo = -2;
hi = 2;
N = 1e2;
x = linspace(lo, hi, N);
y = x.^2;
plot(x, y);
title('x^2');^[[201~octave:172>
octave:172>
octave:172>
octave:172>
octave:172> lo = -2;
hi = 2;
N = 1e2;
x = linspace(lo, hi, N);
y = x.^2;
plot(x, y);
title('x^2');
octave:179> close all
octave:180> diff(x)
ans =
Columns 1 through 7:
0.040404 0.040404 0.040404 0.040404 0.040404 0.040404 0.040404
Columns 8 through 14:
0.040404 0.040404 0.040404 0.040404 0.040404 0.040404 0.040404
Columns 15 through 21:
0.040404 0.040404 0.040404 0.040404 0.040404 0.040404 0.040404
Columns 22 through 28:
0.040404 0.040404 0.040404 0.040404 0.040404 0.040404 0.040404
Columns 29 through 35:
0.040404 0.040404 0.040404 0.040404 0.040404 0.040404 0.040404
Columns 36 through 42:
0.040404 0.040404 0.040404 0.040404 0.040404 0.040404 0.040404
Columns 43 through 49:
0.040404 0.040404 0.040404 0.040404 0.040404 0.040404 0.040404
Columns 50 through 56:
0.040404 0.040404 0.040404 0.040404 0.040404 0.040404 0.040404
Columns 57 through 63:
0.040404 0.040404 0.040404 0.040404 0.040404 0.040404 0.040404
Columns 64 through 70:
0.040404 0.040404 0.040404 0.040404 0.040404 0.040404 0.040404
Columns 71 through 77:
0.040404 0.040404 0.040404 0.040404 0.040404 0.040404 0.040404
Columns 78 through 84:
0.040404 0.040404 0.040404 0.040404 0.040404 0.040404 0.040404
Columns 85 through 91:
0.040404 0.040404 0.040404 0.040404 0.040404 0.040404 0.040404
Columns 92 through 98:
0.040404 0.040404 0.040404 0.040404 0.040404 0.040404 0.040404
Column 99:
0.040404
octave:181> diff(y)
ans =
Columns 1 through 8:
-0.1600 -0.1567 -0.1535 -0.1502 -0.1469 -0.1437 -0.1404 -0.1371
Columns 9 through 16:
-0.1339 -0.1306 -0.1273 -0.1241 -0.1208 -0.1175 -0.1143 -0.1110
Columns 17 through 24:
-0.1077 -0.1045 -0.1012 -0.0979 -0.0947 -0.0914 -0.0882 -0.0849
Columns 25 through 32:
-0.0816 -0.0784 -0.0751 -0.0718 -0.0686 -0.0653 -0.0620 -0.0588
Columns 33 through 40:
-0.0555 -0.0522 -0.0490 -0.0457 -0.0424 -0.0392 -0.0359 -0.0326
Columns 41 through 48:
-0.0294 -0.0261 -0.0229 -0.0196 -0.0163 -0.0131 -0.0098 -0.0065
Columns 49 through 56:
-0.0033 0 0.0033 0.0065 0.0098 0.0131 0.0163 0.0196
Columns 57 through 64:
0.0229 0.0261 0.0294 0.0326 0.0359 0.0392 0.0424 0.0457
Columns 65 through 72:
0.0490 0.0522 0.0555 0.0588 0.0620 0.0653 0.0686 0.0718
Columns 73 through 80:
0.0751 0.0784 0.0816 0.0849 0.0882 0.0914 0.0947 0.0979
Columns 81 through 88:
0.1012 0.1045 0.1077 0.1110 0.1143 0.1175 0.1208 0.1241
Columns 89 through 96:
0.1273 0.1306 0.1339 0.1371 0.1404 0.1437 0.1469 0.1502
Columns 97 through 99:
0.1535 0.1567 0.1600
octave:182> diff(y)./diff(x)
ans =
Columns 1 through 8:
-3.9596 -3.8788 -3.7980 -3.7172 -3.6364 -3.5556 -3.4747 -3.3939
Columns 9 through 16:
-3.3131 -3.2323 -3.1515 -3.0707 -2.9899 -2.9091 -2.8283 -2.7475
Columns 17 through 24:
-2.6667 -2.5859 -2.5051 -2.4242 -2.3434 -2.2626 -2.1818 -2.1010
Columns 25 through 32:
-2.0202 -1.9394 -1.8586 -1.7778 -1.6970 -1.6162 -1.5354 -1.4545
Columns 33 through 40:
-1.3737 -1.2929 -1.2121 -1.1313 -1.0505 -0.9697 -0.8889 -0.8081
Columns 41 through 48:
-0.7273 -0.6465 -0.5657 -0.4848 -0.4040 -0.3232 -0.2424 -0.1616
Columns 49 through 56:
-0.0808 0 0.0808 0.1616 0.2424 0.3232 0.4040 0.4848
Columns 57 through 64:
0.5657 0.6465 0.7273 0.8081 0.8889 0.9697 1.0505 1.1313
Columns 65 through 72:
1.2121 1.2929 1.3737 1.4545 1.5354 1.6162 1.6970 1.7778
Columns 73 through 80:
1.8586 1.9394 2.0202 2.1010 2.1818 2.2626 2.3434 2.4242
Columns 81 through 88:
2.5051 2.5859 2.6667 2.7475 2.8283 2.9091 2.9899 3.0707
Columns 89 through 96:
3.1515 3.2323 3.3131 3.3939 3.4747 3.5556 3.6364 3.7172
Columns 97 through 99:
3.7980 3.8788 3.9596
octave:183>
^[[200~figure();
plot(x(1:end-1), dydx);^[[201~octave:183>
octave:183>
octave:183>
octave:183>
octave:183> figure();
plot(x(1:end-1), dydx);
error: 'dydx' undefined near line 1, column 18
octave:201> xdiff = diff(x);
octave:202> dx = xdiff(1);
octave:203> dx
dx = 0.040404
octave:204> int_of_y = sum(y)*dx
int_of_y = 5.4960
octave:205> Y = cumsum(y)*dx
Y =
Columns 1 through 8:
0.1616 0.3168 0.4656 0.6082 0.7448 0.8754 1.0002 1.1193
Columns 9 through 16:
1.2329 1.3411 1.4440 1.5418 1.6345 1.7224 1.8055 1.8841
Columns 17 through 24:
1.9581 2.0277 2.0932 2.1546 2.2120 2.2655 2.3154 2.3617
Columns 25 through 32:
2.4046 2.4442 2.4806 2.5140 2.5445 2.5722 2.5973 2.6199
Columns 33 through 40:
2.6401 2.6581 2.6739 2.6878 2.6998 2.7101 2.7188 2.7261
Columns 41 through 48:
2.7320 2.7368 2.7405 2.7433 2.7453 2.7466 2.7474 2.7479
Columns 49 through 56:
2.7480 2.7480 2.7480 2.7482 2.7486 2.7494 2.7507 2.7527
Columns 57 through 64:
2.7555 2.7592 2.7640 2.7700 2.7772 2.7859 2.7963 2.8083
Columns 65 through 72:
2.8221 2.8380 2.8559 2.8761 2.8987 2.9238 2.9515 2.9820
Columns 73 through 80:
3.0154 3.0518 3.0914 3.1343 3.1806 3.2305 3.2841 3.3415
Columns 81 through 88:
3.4028 3.4683 3.5380 3.6120 3.6905 3.7736 3.8615 3.9542
Columns 89 through 96:
4.0520 4.1549 4.2631 4.3767 4.4959 4.6207 4.7513 4.8878
Columns 97 through 100:
5.0304 5.1793 5.3344 5.4960
octave:206> figure
octave:207> hold on
octave:208> plot(x, y)
octave:209> plot(x, Y)
octave:210> close all
octave:211> help cumtrapz
'cumtrapz' is a function from the file /usr/share/octave/7.3.0/m/general/cumtr
apz.m
-- Q = cumtrapz (Y)
-- Q = cumtrapz (X, Y)
-- Q = cumtrapz (..., DIM)
Cumulative numerical integration of points Y using the trapezoidal
method.
'cumtrapz (Y)' computes the cumulative integral of Y along the
first non-singleton dimension. Where 'trapz' reports only the
overall integral sum, 'cumtrapz' reports the current partial sum
value at each point of Y.
When the argument X is omitted an equally spaced X vector with unit
spacing (1) is assumed. 'cumtrapz (X, Y)' evaluates the integral
with respect to the spacing in X and the values in Y. This is
useful if the points in Y have been sampled unevenly.
If the optional DIM argument is given, operate along this
dimension.
Application Note: If X is not specified then unit spacing will be
used. To scale the integral to the correct value you must multiply
by the actual spacing value (deltaX).
See also: trapz, cumsum.
Additional help for built-in functions and operators is
available in the online version of the manual. Use the command
'doc <topic>' to search the manual index.
Help and information about Octave is also available on the WWW
at https://www.octave.org and via the help@octave.org
mailing list.
octave:212> help trapz=
error: help: 'trapz=' not found
octave:213> help trapz
'trapz' is a function from the file /usr/share/octave/7.3.0/m/general/trapz.m
-- Q = trapz (Y)
-- Q = trapz (X, Y)
-- Q = trapz (..., DIM)
Numerically evaluate the integral of points Y using the trapezoidal
method.
'trapz (Y)' computes the integral of Y along the first
non-singleton dimension. When the argument X is omitted an equally
spaced X vector with unit spacing (1) is assumed. 'trapz (X, Y)'
evaluates the integral with respect to the spacing in X and the
values in Y. This is useful if the points in Y have been sampled
unevenly.
If the optional DIM argument is given, operate along this
dimension.
Application Note: If X is not specified then unit spacing will be
used. To scale the integral to the correct value you must multiply
by the actual spacing value (deltaX). As an example, the integral
of x^3 over the range [0, 1] is x^4/4 or 0.25. The following code
uses 'trapz' to calculate the integral in three different ways.
x = 0:0.1:1;
y = x.^3;
## No scaling
q = trapz (y)
=> q = 2.5250
## Approximation to integral by scaling
q * 0.1
=> 0.25250
## Same result by specifying X
trapz (x, y)
=> 0.25250
See also: cumtrapz.
Additional help for built-in functions and operators is
available in the online version of the manual. Use the command
'doc <topic>' to search the manual index.
Help and information about Octave is also available on the WWW
at https://www.octave.org and via the help@octave.org
mailing list.
octave:214> trapz(x, y)
ans = 5.3344
octave:215> cmutrapz(x, y)
error: 'cmutrapz' undefined near line 1, column 1
octave:216> cumtrapz(x, y)
ans =
Columns 1 through 8:
0 0.1584 0.3104 0.4561 0.5957 0.7293 0.8570 0.9789
Columns 9 through 16:
1.0953 1.2062 1.3118 1.4121 1.5074 1.5977 1.6832 1.7640
Columns 17 through 24:
1.8403 1.9121 1.9797 2.0431 2.1024 2.1579 2.2097 2.2578
Columns 25 through 32:
2.3024 2.3436 2.3816 2.4165 2.4485 2.4776 2.5040 2.5278
Columns 33 through 40:
2.5492 2.5683 2.5852 2.6000 2.6130 2.6241 2.6336 2.6416
Columns 41 through 48:
2.6483 2.6536 2.6579 2.6611 2.6635 2.6652 2.6662 2.6668
Columns 49 through 56:
2.6671 2.6672 2.6672 2.6673 2.6676 2.6682 2.6693 2.6709
Columns 57 through 64:
2.6733 2.6766 2.6808 2.6862 2.6928 2.7008 2.7103 2.7215
Columns 65 through 72:
2.7344 2.7493 2.7662 2.7852 2.8066 2.8305 2.8569 2.8860
Columns 73 through 80:
2.9179 2.9528 2.9908 3.0321 3.0767 3.1248 3.1765 3.2320
Columns 81 through 88:
3.2914 3.3548 3.4223 3.4942 3.5704 3.6512 3.7367 3.8271
Columns 89 through 96:
3.9223 4.0227 4.1282 4.2391 4.3555 4.4774 4.6052 4.7387
Columns 97 through 100:
4.8783 5.0241 5.1760 5.3344
octave:217> Y = cumtrapz(x, y)
Y =
Columns 1 through 8:
0 0.1584 0.3104 0.4561 0.5957 0.7293 0.8570 0.9789
Columns 9 through 16:
1.0953 1.2062 1.3118 1.4121 1.5074 1.5977 1.6832 1.7640
Columns 17 through 24:
1.8403 1.9121 1.9797 2.0431 2.1024 2.1579 2.2097 2.2578
Columns 25 through 32:
2.3024 2.3436 2.3816 2.4165 2.4485 2.4776 2.5040 2.5278
Columns 33 through 40:
2.5492 2.5683 2.5852 2.6000 2.6130 2.6241 2.6336 2.6416
Columns 41 through 48:
2.6483 2.6536 2.6579 2.6611 2.6635 2.6652 2.6662 2.6668
Columns 49 through 56:
2.6671 2.6672 2.6672 2.6673 2.6676 2.6682 2.6693 2.6709
Columns 57 through 64:
2.6733 2.6766 2.6808 2.6862 2.6928 2.7008 2.7103 2.7215
Columns 65 through 72:
2.7344 2.7493 2.7662 2.7852 2.8066 2.8305 2.8569 2.8860
Columns 73 through 80:
2.9179 2.9528 2.9908 3.0321 3.0767 3.1248 3.1765 3.2320
Columns 81 through 88:
3.2914 3.3548 3.4223 3.4942 3.5704 3.6512 3.7367 3.8271
Columns 89 through 96:
3.9223 4.0227 4.1282 4.2391 4.3555 4.4774 4.6052 4.7387
Columns 97 through 100:
4.8783 5.0241 5.1760 5.3344
octave:218>