Creative Computing: Octave/Matlab
Octave is one of the major free alternatives to MATLAB, others being Scilab and FreeMat. Octave helps in solving linear and nonlinear problems numerically, and for performing other numerical experiments using a language that is mostly compatible with MATLAB. Octave has been built with MATLAB compatibility in mind, and shares many features with MATLAB: Matrices as fundamental data type. Built-in support for complex numbers. Powerful built-in math functions and extensive function libraries. Extensibility in the form of user-defined functions. Octave treats incompatibility with MATLAB as a bug.
23 flashcards · Programming Languages · by lakesare
Why is sin(pi) 1.2246e-16 in Octave instead of 0?
Trigonometric functions around pi may have errors close to machine precision, it's as precise to 0 as Octave gets :-) Note that the values for sin(pi) and tan(pi/2) are not exact: the mathematically correct values of these functions for the given arguments are 0 and Infinity (or ∞) respectively. Here, as with all mathematical operations, Octave reports the value of the function to within the floating-point numerical accuracy of the host system.
What number is 15e3?
15000 e means exponent.
What does exp(15) mean?
e15
What do these return? octave:1> x = [1, 3, 2] octave:2> x = [1; 3; 2]
Row vector: octave:1> x = [1, 3, 2] x = 1 3 2 Note that each entry is separated by an optional comma. x = [1 3 2] results in the same row vector. Column vector: octave:2> x = [1; 3; 2] x = 1 3 2
How to specify a matrix?
octave:1> A = [1 1 2; 3 5 8; 13 21 34] A = 1 1 2 3 5 8 13 21 34
What will size() return here? octave:1> x = size([1, 3, 2])
1 3 1st number: rows 2nd number: columns octave:1> x = size([1, 3, 2]) x = 1 3
What is this operation? octave:1> [1 2 3]'
Transpose! octave:1> [1 2 3]' ans = 1 2 3
What's the result? octave:1> [1:10]
octave:8> [1:10] ans = 1 2 3 4 5 6 7 8 9 10
How to create this row vector? [3 3.2 3.4 3.6 3.8 4 4.2]
octave:1> [3:0.2:4.2] ans = 3.0000 3.2000 3.4000 3.6000 3.8000 4.0000 4.2000 The first number displayed was start, the second was start + step, the third, start + (2 * step). And the last number was less than or equal to stop.
How to perform an element by element operation on 2 matrices? When doing an element by element addition, the element on place (row, col) in the resulting matrix will be the sum of the two elements at (row, col) in the operand matrices.
The regular arithmetic operators will become element-by-element operators if you place a dot in front of them. .+ .- .* ./ .^ >>> m1 m1 = 1 2 3 4 5 6 >>> m2 m2 = 0 1 1 2 2 0 >>> m1./m2 ans = Inf 2.0000 3.0000 2.0000 2.5000 Inf
You can use the standard operators to add (+), subtract (-), and multiply (*) matrices, vectors and scalars with one another.
Note that the matrices need to have matching dimensions (inner dimensions in the case of multiplication) for these operators to work.
What's a stem graph?
stem(Y) plots the data sequence, Y, as stems that extend from a baseline along the x-axis.
We can create vectors without brackets :-) Although a range constant specifies a row vector, Octave does not normally convert range constants to vectors unless it is necessary to do so. This allows you to write a constant like ‘1 : 10000’ without using 80,000 bytes of storage on a typical 32-bit workstation. A common example of when it does become necessary to convert ranges into vectors occurs when they appear within a vector (i.e., inside square brackets). For instance, whereas x = 0 : 0.1 :…
What's the result?
When applying a function on a matrix, the function is applied to each element of the matrix.
What are 2 arguments to plot(x, y)?
plot(x_vector, y_vector) When plotting in Octave you plot points having their x-values stored in one vector and the y-values in another vector. The two vectors must be the same size. >>> x=-2:2 x = -2 -1 0 1 2 >>> y=x.^2 y = 4 1 0 1 4 >>> plot(x,y)
>>> x = -2:2 >>> y = x .^ 2 >>> plot(x, y) What will be plotted?
>>> x = -2:2 x = -2 -1 0 1 2 >>> y = x .^ 2 y = 4 1 0 1 4 >>> plot(x, y)
v = [9 8 7] How to get value 9?
v(1)
function hi = hello(vector) hi = vector(1) hi = vector(2) endfunction Explain this syntax.
hello([5:10]); //=> hi = 6 Function's name is hello. Return value is a variable hi, after we execute this function we can use hi.
What does zeros() function do?
Return x,y-dimensional matrix whose elements are all 0.
What is the difference between these commands? octave:43> B = rand (3, 2); octave:44> B = rand (3, 2)
Ending a command with a semicolon tells Octave not to print the result of the command.
How to create this vector (without writing down each 0 and 1)?
What's the result? octave:1> shift([1 2 3 4 5 6], 1)
octave:1> shift([1 2 3 4 5 6], 1) ans = 6 1 2 3 4 5
What's the result? octave:1> shift([1 2 3 4 5 6], -2)
octave:1> shift([1 2 3 4 5 6], -2) ans = 3 4 5 6 1 2