Octave

## Useful links

Introduction to Octave

## Problems with mac

### displaying blank axis with aqua terminal

solution: change the terminal's type to x11

````setenv GNUTERM x11`
```

## Plotting

### plot a simple function

``````t=[0:0.01:2.00]
y=cos(2*pi*t)
legend('cos')
xlabel('time)
title('Cosine plot')
axis([-5 5 -1 1 ]); %the first two elements are x bound and the second tow are y bounds
plot (t,y)```
```

### plot 2 functions in one window

``````t=[0:0.01:2.00]
y1=cos(2*pi*t)
y2=t.**(1/2)
plot (t,y1)
hold on;
plot (t,y2)```
```

### plot in different windows

``````=[0:0.01:2.00]
y1=cos(2*pi*t)
y2=t.**(1/2)
figure(1); plot(t,y1);
figure(2); plot(t,y2);```
```

### plot 2 functions in one window side by side

``````t=[0:0.01:2.00]
y1=cos(2*pi*t)
y2=t.**(1/2)
subplot(1,2,1); %divides plot a 1X2 grids, access the first element
plot (t,y1);
subplot(1,2,2); %access the first element
plot (t,y2)```
```

### Saving plot in a directory

``````cd 'home/my_folder' ;
print -dpng 'my_plot.png'```
```

### Clear figure

````clf;`
```

### visualizing amtrix

``````A = magic(5)
imagesc(A)% draw a 5*5 grids of colors and colors are related to the values of matrix.
imagesc(A) , colorbar % add bar to the image```
```

## scripting in Octave

### adding a folder to path

````addpath('/scr/myscripts')`
```

### control statement

#### for

``````for i=1:10;
v(i) = 2^i ;
end;```
```

#### while & if

``````i=1;
while i<=5,
v(i)=20;
i=i+1;
end;
%%%%% OR
i=1;
while true,
v(i)=20;
i=i+1;
if i==6,
break;
end;
end;

%%%%%%
if v(1)==20,
disp('The first one is equal to twenty');
elseif v(2)==5,
disp('The second one is equal to five');
else,
disp('Neither v(1) ==20 or v(2)==5')```
```

### defining function

like matlab, write function into a ".m" file.

``````function [y1 , y2]= squareandcube(x)
y1=x^2;
y2=x^3;```
```

to call the function just cd to that directory and call that function no file calling in needed!!!
````[v, w] =squareandcube(5);`
```

## matrix and linear algebra

### defining matrix

``````A = [1 2 ; 4 5; 5 8]; %3*2 matrix
B= [4 2 7; 6 9 1] ; % 2*3 matrix```
```

### transpose, inverse etc

``````C=B*A; %matrix multiplication

A' %transpose
inv(C) %inverse```
```
page revision: 25, last edited: 19 May 2013 21:48
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License