Panduan Praktikum 4 Octave: Memprogram di OCTAVE

OPERATOR ARITMATIKA
Operator aritmatika berikut tersedia dan bekerja/berfungsi pada skalar dan matriks.
x + y Penjumlahan. Jika kedua operan (sesuatu yang dioperasikan) adalah matriks, jumlah baris dan kolom kedua matriks harus sesuai. Jika satu operan adalah/berupa skalar, nilainya ditambahkan pada semua elemen operan yang lain.
x .+ y Element by element addition. Operator ini setara dengan +.
x – y Pengurangan. Jika kedua operan adalah matriks, jumlah baris dan kolom kedua matriks harus sesuai.
x .- y Element by element subtraction. Operator ini setara dengan -.
x * y Perkalian matriks. jumlah kolom matriks x harus sama dengan jumlah baris matriks y.
x .* y Element by element multiplication. If both operands are matrices, the number of rows and columns must both agree.
x / y Right division. This is conceptually equivalent to the expression
(inverse (y’) * x’)’ but it is computed without forming the inverse of y’.
If the system is not square, or if the coefficient matrix is singular, a minimum norm solution is computed.
x ./ y Element by element right division.
x \ y Left division. This is conceptually equivalent to the expression
inverse (x) * y but it is computed without forming the inverse of x.
If the system is not square, or if the coefficient matrix is singular, a minimum norm solution is computed.
x .\ y Element by element left division. Each element of y is divided by each corresponding element of x.
x ^ y
x ** y Power operator. If x and y are both scalars, this operator returns x raised to the power y. If x is a scalar and y is a square matrix, the result is computed
using an eigenvalue expansion. If x is a square matrix, the result is computed
by repeated multiplication if y is an integer, and by an eigenvalue expansion if y is not an integer. An error results if both x and y are matrices.
The implementation of this operator needs to be improved.
x .^ y
x .** y Element by element power operator. If both operands are matrices, the number
of rows and columns must both agree.
-x Negation.
+x Unary plus. This operator has no effect on the operand.
x’ Complex conjugate transpose. For real arguments, this operator is the same as
the transpose operator. For complex arguments, this operator is equivalent to
the expression
conj (x.’)
x.’ Transpose.
Operator Perbandingan
x < y Benar jika x kurang dari y.
x = y Benar jika x lebih dari atau sama dengan y.
x > y Benar jika x lebih dari y.
x != y
x ~= y
x y Benar jika x tidak sama dengan y.
Meningkat
a = a + 2;
dapat dipersingkat menggunakan operator +=
a += 2;

statement separators
‘;’, ‘,’.
assignment
‘=’, ‘+=’, ‘-=’, ‘=’,‘/=’. This operator groups right to left.
logical “or” and “and”
‘||’, ‘&&’.
element-wise “or” and “and”
‘|’, ‘&’.
relational
‘<’, ‘=’, ‘>’, ‘!=’, ‘~=’, ‘’.
colon ‘:’.
add, subtract
‘+’, ‘-’.
multiply, divide
’, ‘/’, ‘\’, ‘.\’, ‘.*’, ‘./’.
transpose
‘’’, ‘.’’
unary plus, minus, increment, decrement, and ‘‘not’’
‘+’, ‘-’, ‘++’, ‘–’, ‘!’, ‘~’.
exponentiation
‘^’, ‘’, ‘.^’, ‘.’.
Menulis Fungsi
Pernyataan if
if (condition)
then-body
endif

if (condition)
then-body
else
else-body
endif

if (rem (x, 2) == 0)
printf (“x is even\n”);
else
printf (“x is odd\n”);
endif

if (condition)
then-body
elseif (condition)
elseif-body
else
else-body
endif

// fibonacci
fib = ones (1, 10);
i = 2;
do
i++;
fib (i) = fib (i-1) + fib (i-2);
until (i == 10)
Statemen for
for var = expression
body
endfor

// — fibonacci dengan statemen for —
fib = ones (1, 10);
for i = 3:10
fib (i) = fib (i-1) + fib (i-2);
endfor

Fungsi
function name (arg-list)
body
endfunction

function bangun (pesan)
printf (“\a%s\n”, pesan);
endfunction
Panggil fungsi menggunakan statemen
bangun (“bangun!”);

function hello (who = “World”)
printf (“Hello, %s!\n”, who);
endfunction
When called without an input argument the function prints the following
hello ();
a Hello, World!
printf (“Hello, world!\n”);

myfile = fopen (“splat.dat”, “r”, “ieee-le”);
The possible values ‘mode’ may have are
‘r’ Membuka file untuk dibaca.
‘w’ Membuka file untuk ditulis. Konten sebelumnya akan diabaikan.
‘a’ Membuka atau membuat file untuk ditulis di akhir file.
‘r+’ Membuka file yang telah ada untuk dibaca dan ditulis.
‘w+’ Membuka file untuk dibaca dan ditulis. Konten sebelumnya akan diabaikan.
‘a+’ Membuka atau membuat file untuk ditulis di akhir file.
filename = “free.txt”;
fid = fopen (filename, “w”);
fputs (fid, “Free Software is needed for Free Science”);
fclose (fid);

Tinggalkan komentar

Situs ini menggunakan Akismet untuk mengurangi spam. Pelajari bagaimana data komentar Anda diproses.