Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
150 views5 pages

Octave Basics for Beginners

The document introduces basic Octave commands and functions for arithmetic, variables, arrays, matrices, control structures, and plotting. It demonstrates how to perform calculations, define and manipulate variables and arrays, access matrix elements, use loops and conditional statements, and generate simple plots. Various matrix operations are also illustrated including transposition, accessing sub-matrices, creating diagonal and replicated matrices.

Uploaded by

Aamir Hasan Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views5 pages

Octave Basics for Beginners

The document introduces basic Octave commands and functions for arithmetic, variables, arrays, matrices, control structures, and plotting. It demonstrates how to perform calculations, define and manipulate variables and arrays, access matrix elements, use loops and conditional statements, and generate simple plots. Various matrix operations are also illustrated including transposition, accessing sub-matrices, creating diagonal and replicated matrices.

Uploaded by

Aamir Hasan Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

OCTAVE TUTORIAL

 
 
I  STARTING OCTAVE AND BASIC COMMANDS 
1  Calculator 
>> 3+2
ans = 5
>> sin(pi/4)
ans = 0.70711
>> exp(-0.5^2)
ans = 0.77880
2  Variables 
>> a=4
a = 4
>> b=5
b = 5
>> c=a*b
c = 20
>> c=c-4
c = 16
 
II  ARRAYS AND MATRICES 
1  Simple arrays 
>> v=[1 5 4.6 0.1]
v =

1.00000 5.00000 4.60000 0.10000

2  Interval arrays 
>> v = 1:10
v =

1 2 3 4 5 6 7 8 9 10

>> v=10:-1:1
v =

10 9 8 7 6 5 4 3 2 1

>> v=0:0.1:1
v =

0.00000 0.10000 0.20000 0.30000 0.40000 0.50000


0.60000 0.70000 0.80000 0.90000 1.00000
 
3  Transpose 
>> v=[1 2 3]
v =

1 2 3

>> v'
ans =

1
2
3
>> v=[i pi e]
v =

0.00000 + 1.00000i 3.14159 + 0.00000i 2.71828 + 0.00000i

>> v'
ans =

0.00000 - 1.00000i
3.14159 - 0.00000i
2.71828 - 0.00000i

4  Accessing elements of an array 
>> v=[0.1 0.3 0.4 -0.1 -0.2]
v =

0.10000 0.30000 0.40000 -0.10000 -0.20000

>> v(1)
ans = 0.10000
>> a=v(1)
a = 0.10000
>> v1=v(2:4)
v1 =

0.30000 0.40000 -0.10000


 
5  Simple matrix 
>> M=[3.2 -1;1 i]
M =

3.20000 + 0.00000i -1.00000 + 0.00000i


1.00000 + 0.00000i 0.00000 + 1.00000i

>> M =[1 2
3 4]
M =

1 2
3 4
 
6  Matrix for smaller matrices 
>> m1=[1 2;3 4]
m1 =

1 2
3 4

>> m2=[1 0;0 1]


m2 =

1 0
0 1

>> M=[m1 m2; m2 m1]


M =

1 2 1 0
3 4 0 1
1 0 1 2
0 1 3 4
7  Special matrices 
>> M=zeros(3)
M =

0 0 0
0 0 0
0 0 0

>> M=ones(3)
M =

1 1 1
1 1 1
1 1 1

>> M=rand(3)
M =

0.44197 0.39764 0.47648


0.70055 0.60734 0.80558
0.99505 0.26442 0.11128

>> M=randn(3)
M =

-0.59956 -1.38575 -1.39264


0.95699 -0.80763 0.38345
-0.63449 -1.12608 -0.36622

>> v=[1 2 3];M=diag(v)


M =

Diagonal Matrix

1 0 0
0 2 0
0 0 3

>> v=[1 2 3];M=diag(v,1)


M =

0 1 0 0
0 0 2 0
0 0 0 3
0 0 0 0

>> v=[1 2 3];M=diag(v,-1)


M =

0 0 0 0
1 0 0 0
0 2 0 0
0 0 3 0

>>
>> M=rand(3);v=diag(M)
v =

0.86526
0.78490
0.42045

>> v=[1 2 3];M=repmat(v,2,3)


M =

1 2 3 1 2 3 1 2 3
1 2 3 1 2 3 1 2 3

>> v=[1 2 3];M=repmat(v,3,2)


M =

1 2 3 1 2 3
1 2 3 1 2 3
1 2 3 1 2 3

8  Accessing elements of a matrix 
>> M=[1 2 3; 4 5 6; 7 8 9]
M =

1 2 3
4 5 6
7 8 9

>> M(2,3)
ans = 6
>> M(1,1)
ans = 1
>> M(1,2)
ans = 2
>> M(2,1)
ans = 4
 
III  CONTROL STRUCTURES 
1  The for loop 
>> for n=1:3
m=2^n
endfor
m = 2
m = 4
m = 8
>> v=1:2:6;for n=v
m=2^n
endfor
m = 2
m = 8
m = 32
2  The if structure 
>> m=[1:10];
>> for n=1:length(m)
if m(n)<5
printf("Number %f \n",m(n));
endif
endfor
Number 1.000000
Number 2.000000
Number 3.000000
Number 4.000000
 
IV  PLOTING 
1  Two‐dimensional 
>> theta=0:0.01:2*pi;
>> y=sin(theta);
>> plot(theta,y)
>> grid on
 
 

You might also like