명령 프롬프트> octave -qi
octave:1> x = [0, 1, 1, 0, 0];
octave:2> y = [0, 0, 1, 1, 0];
octave:3> plot(x, y), axis([-1,2, -1,2])
octave:4> axis equal
octave:5> close
octave:6> plot(x, y), axis equal, axis([-1,2, -1,2])
octave:7> close


 

 * 위의 3번 줄 명령으로 정사각형을 그렸지만, aspect ratio 가 맞지 않아 직사각형으로 보인다,

 

 

  * 그래프 창을 그대로 둔 채 위의 4번 줄 명령울 내리면, 그래프 창이 다시 열리면서 정사각형이 그려진다. (위의 3번과 4번 줄 대신 6번 줄 하나만 내려도 된다. close 는 그래프 창을 닫는 명령이다,)

 

 

Posted by Scripter
,


프롬프트> octave -qi
octave:1> # Let's find roots of the equation x^2 - 5 = 0.
octave:1> c = [1, 0, -5];       # 다항식의 계수들
octave:2> roots(c)
ans =

  -2.2361
   2.2361

octave:3> x = roots(c);
octave:4> x(1)
ans = -2.2361
octave:5> x(2)
ans =  2.2361
octave:6> sqrt(5)
ans =  2.2361
octave:7> format long
octave:8> x
x =

  -2.23606797749979
   2.23606797749979


octave:9> x(1), x(2)
ans = -2.23606797749979
ans =  2.23606797749979
octave:10> sqrt(5)
ans =  2.23606797749979

 

Posted by Scripter
,

프롬프트> octave -qi
octave:1> a = [1, 2, 3];
octave:2> b = [5, 3, 4, 3];
octave:3> union(a, b)      # 합집합
ans =

   1   2   3   4   5

octave:4> intersect(a, b)      # 공통집합
ans =  3
octave:5> setdiff(a, b)        # 차집합
ans =

   1   2

octave:6> setdiff(b, a)        # 차집합
ans =

   4   5

octave:7> setxor(a, b)        # 대칭차(symmetric difference)
ans =

   1   2   4   5

octave:8> unique(b)         # 중복 허용 않는 집합
ans =

   3   4   5

octave:9> ismember(a, b)       # 원소 확인
ans =

   0   0   1

octave:10> ismember(b, a)       # 원소 확인
ans =

   0   1   0   1

octave:11> ismember(unique(b), a)       # 원소 확인
ans =

   1   0   0

 

 

Posted by Scripter
,

Cygwin 의 터미널에서

$ xinit

하여 X 터미널을 열고, X 터미널의 쉘 프롬프트에서

$ octave -qi

하여 Octave 를 시작하여 다음 Octave 명령들을 입력한다. (saveas() 명령으로 그림을 파일로 저장할 수 있다.)

 

octave:1> t = linspace(0, 2*pi, 500);
octave:2> x = cos(t).**3;
octave:3> y = sin(t).**3;
octave:4> plot(x, y);     # => error!
octave:5> setenv("GNUTERM", "X11");     # 윈도우즈에서는 graphics_toolkit gnuplot;
octave:6> plot(x, y);
octave:7> saveas(1, "figure_asterisk.jpg", "jpg");
octave:8> close;

 

* X 터미널에서 Octave 를 실행 중인 모습

 

 * 위의 saveas() 명령으로 저장된 jpg 파일

 

 

이제 위의 작업을 Octave 대신 Gnuplot 으로 해보자.

먼저 Cygwin 의 쉘 프롬프트에서 

xinit
gnuplot> plot [0:2*pi] (cos(t))**3, (sin9t))**3

한 후, X 터미널이 뜨면 X 터미널의 쉘 프롬프트에서

gnuplot

하여 Gbuplot 을 실행시킨다. 그리고 다음 Gnuplot 명령들을 입력한다.

 

gnuplot> set term 11
gnuplot> set parametric
gnuplot> set size square
gnuplot> set size ratio -1
gnuplot> set grid
gnuplot> plot [0:2*pi] (cos(t))**3, (sin(t))**3

 

그러면 아래와 같이 매개변수 평면곡선이 X 터미널에 그려진다.

 

 

그림 창을 닫을려면 Guplot 프롬프트에서 다음 영령을 입역한다.


gnuplot> set term x11 close

 

이제 장금 그려졌던 그림을 jpg 파일로 저장해보자.


gnuplot> set term jpeg size 400,400
gnuplot> set output "asterisk_by_gnuplot.jpg"
gnuplot> replot

 

위의 replot 열령 하나만 하면 반금 X 터미널에 보여졌단 그래픽을 파일로 저장할 수 있다. 그러나 아직 그림 파일은 열려 있으며 완정되지 않았다. Gnuplot 이; 여정히 파일 핸들을 붇잡고 있어서 다른 에플리케이션이 이 이미지 파일에 접근 불가능하다. 그렇다고 Gnuplot 을 그냥 종료하면 그림ㄴ이 정상정으로 저장되기 전에 종료해 버리게 되므로 그림 파일을 제대을 얻지 못한다. Gnuplot  프롬프트에서 반드시 다음 명령을 해야만 완성된 그림 파일을 얻을 수 있다.


gnuplot> set output


 

 

 

* Gnuplot 에서 저장한 jpg 그림 파일


 

윈도우용 Gnuplot 을 설치한 경우

wgnuplot 을 실행시켜서 다음을 한 줄씩 입력하면서 진행 과정을 관찰해본다.

 

gnuplot> set para
gnuplot> set size square
gnuplot> set size ratio -1
gnuplot> plot [0:2*pi] (cos(t))**3, (sin(t))**3
gnuplot> set grid
gnuplot> replot
gnuplot> set term wind close
gnuplot> set term jpeg size 500,500 
gnuplot> set output "asterisk_by_wgnuplot.jpg" 
gnuplot> replot
gnuplot> set output 

 

Posted by Scripter
,

다음 연립 미분방정식과 초기조건 x(0) = 1, y(0) = 2 를 만족하는 두 함수  x = x(t), y = y(t)  의 그래프를 그려보자.

dx/dt = r*x*(1 -x/k) -a*x*y/(1 + b*x)
dy/dt = c*a*x*y/(1 + b*x) - d*y

단, 여기서 r = 0.25
               k = 1.4
               a = 1.5
               b = 0.16
               c = 0.9

이다.

즉,

dx/dt = 0.25*x*(1 -x/1.4) - 1.5*x*y/(1 + 0.16*x)
dy/dt = 3*1.5*x*y/(1 + 0.16*x) - 0.8*y

 

[Octave 소스파일: f.m]--------------------------------
function xdot = f (x, t)

  r = 0.25;
  k = 1.4;
  a = 1.5;
  b = 0.16;
  c = 0.9;
  d = 0.8;

  xdot(1) = r*x(1)*(1 - x(1)/k) - a*x(1)*x(2)/(1 + b*x(1));
  xdot(2) = c*a*x(1)*x(2)/(1 + b*x(1)) - d*x(2);
endfunction

graphics_toolkit gnuplot
x0 = [1; 2];
t = linspace (0, 50, 200)';
x = lsode ("f", x0, t);
plot (t, x)

--------------------------------------------------------------
 

 

Posted by Scripter
,

plot 명령을 내리기 전에 graphics_toolkit gnuplot 명령을 먼저 내린다.

 

명령 프롬프트> octave -qi
octave:1> graphics_toolkit gnuplot
octave:2> x = linspace(0,1,400);
octave:3> y = x.^2;
octave:4> plot(x,y)

  

* 위의 코드로 그래프 창이 나타나지 않을 경우 다음 코드를 시도한다.

명령 프롬프트> octave -qi
octave:1> graphics_toolkit gnuplot
octave:2> setenv('GNUTERM','wx');
octave:3>x = linspace(0,1,400);
octave:4>y = x.^2;
octave:5> plot(x,y)

 

 

 

Cygwin 의 X 터미널에서 Octave 를 실행할 경우:

$ startx

또는

$ xinit

한 후 Octave 를 실행시켜서,

plot 명령을 내리기 전에 setenv("GNUTERM", "X11") 명령을 먼저 내린다.

 

쉘 프롬프트$ octave -qi
octave:1> x = linspace(0,1,400);
octave:2> y = x.^2;
octave:3> setenv("GNUTERM", "X11");
octave:4> plot(x,y);

 

 

이번에는 세 함수

    y = cos 2x,   y = sin 4x,  y = 2sin x

의 그래프를 한 좌표평면에 그려보자.

$ xinit

한 후, X 터미널에서

$ octave -qi
octave:1> x = linspace(0, 2*pi);
octave:2> a = cos(2*x);
octave:3> b = sin(4*x);
octave:4> c = 2*sin(x);
octave:5> setenv("GNUTERM", "X11");
octave:6> figure;
octave:7> hold off;
octave:8> plot(x, a, x, b, x, c);
octave:9> close all

 

 

Posted by Scripter
,

 

Octave 언어 소스:

% Filename: testHexView_02.m
%
% Execute: octave -qf testHexView_02.m [filename]
%
% Date: 2013. 8. 18.

function printUsage()
    printf("Usage: octave -qf testHexView_02.m [filename]\n");
endfunction

function y = getFileSize(fd)
    n = ftell(fd);
    fseek(fd, 0, SEEK_END);
    x = ftell(fd);
    fseek(fd, n, SEEK_SET);
    y = x;
endfunction

function y = toHex(n)
    s = "";
    x1 = bitshift(bitand(n, 0xF0), -4);
    x2 = bitand(n, 0xF);
    if (x1 < 10)
        s = strcat(s, char(toascii('0') + x1));
    else
        s = strcat(s, char(toascii('A') + (x1 - 10)));
    endif
    % printf("s = %s\n", s);
    if (x2 < 10)
        s = strcat(s, char(toascii('0') + x2));
    else
        s = strcat(s, char(toascii('A') + (x2 - 10)));
    endif
    % printf("s = %s\n", s);
    y = s;
endfunction

function y = toHex8(n)
    s = "";
    % printf("s = %s\n", s);
    x1 = bitshift(bitand(n, 0xF0000000), -28);
    x2 = bitshift(bitand(n, 0xF000000), -24);
    x3 = bitshift(bitand(n, 0xF00000), -20);
    x4 = bitshift(bitand(n, 0xF0000), -16);
    x5 = bitshift(bitand(n, 0xF000), -12);
    x6 = bitshift(bitand(n, 0xF00), -8);
    x7 = bitshift(bitand(n, 0xF0), -4);
    x8 = bitand(n, 0xF);
    if (x1 < 10)
        s = strcat(s, char(toascii('0') + x1));
    else
        s = strcat(s, char(toascii('A') + (x1 - 10)));
    endif
    if (x2 < 10)
        s = strcat(s, char(toascii('0') + x2));
    else
        s = strcat(s, char(toascii('A') + (x2 - 10)));
    endif
    if (x3 < 10)
        s = strcat(s, char(toascii('0') + x3));
    else
        s = strcat(s, char(toascii('A') + (x3 - 10)));
    endif
    if (x4 < 10)
        s = strcat(s, char(toascii('0') + x4));
    else
        s = strcat(s, char(toascii('A') + (x4 - 10)));
    endif
    %% s = sprintf("%s -", s);
    if (x5 < 10)
        % s = strcat(s, char(toascii('0') + x5));
        s = sprintf("%s %c", s, char(toascii('0') + x5));
    else
        % s = strcat(s, char(toascii('A') + (x5 - 10)));
        s = sprintf("%s %c", s, char(toascii('A') + (x5 - 10)));
    endif
    if (x6 < 10)
        s = strcat(s, char(toascii('0') + x6));
    else
        s = strcat(s, char(toascii('A') + (x6 - 10)));
    endif
    if (x7 < 10)
        s = strcat(s, char(toascii('0') + x7));
    else
        s = strcat(s, char(toascii('A') + (x7 - 10)));
    endif
    if (x8 < 10)
        s = strcat(s, char(toascii('0') + x8));
    else
        s = strcat(s, char(toascii('A') + (x8 - 10)));
    endif
    y = s;
endfunction

 

arg_list = argv ();
if nargin < 1
    printUsage();
    quit;
endif

fname = arg_list{1};

if exist(fname, "dir")
    printf("\"%s\" is a directory.\n", fname);
    quit;
endif

if ! exist(fname, "file")
    printf("The file \"%s\" does not exist.\n", fname);
    quit;
endif

f = fopen(fname, "rb");

fsize = getFileSize(f);

printf("The size of the file \"%s\" is %d.\n", fname, fsize);
printf("\n");

fseek(f, 0, SEEK_SET);
n = 0;
dum = "";
while n < fsize
    if mod(n, 16) == 0
        printf("%s: ", toHex8(n));
    endif
    [val, count] = fread (f, 1);
    if mod(n, 16) == 8
        printf("-%s", toHex(val));
    else
        printf(" %s", toHex(val));
    endif
    if toascii(val) < toascii(' ') || toascii(val) > 0x7F
        dum = sprintf("%s%c", dum, '.');
    else
        dum = sprintf("%s%c", dum, toascii(val));
    endif
    if mod(n, 16) == 15
        printf("  |%s|\n", dum);
        dum = "";
    endif
    n = n + 1;
endwhile

if mod(n, 16) > 0
    for i = 1 : 16 - mod(n, 16)
        printf("   ");
    endfor
    printf("  |%s", dum);
    for i = 1 : 16 - mod(n, 16)
        printf(" ");
    endfor
    printf("|\n");
    dum = "";
endif

fclose(f);

printf("\nRead %d bytes.\n", n);


 

실행 예 1> octave -qf testHexView_02.m .\temp_1.bin
The size of the file ".\temp_1.bin" is 12.

0000 0000:  48 65 6C 6C 6F 20 74 68-65 72 65 0A              |Hello there.    |

Read 12 bytes.


실행 예 2> octave -qf testHexView_02.m .\myFile.ser
The size of the file ".\myFile.ser" is 130.

0000 0000:  AC ED 00 05 73 72 00 06-50 65 72 73 6F 6E 07 31  |,m..sr..Person.1|
0000 0010:  46 DB A5 1D 44 AB 02 00-03 49 00 03 61 67 65 4C  |F[%.D+...I..ageL|
0000 0020:  00 09 66 69 72 73 74 4E-61 6D 65 74 00 12 4C 6A  |..firstNamet..Lj|
0000 0030:  61 76 61 2F 6C 61 6E 67-2F 53 74 72 69 6E 67 3B  |ava/lang/String;|
0000 0040:  4C 00 08 6C 61 73 74 4E-61 6D 65 71 00 7E 00 01  |L..lastNameq.~..|
0000 0050:  78 70 00 00 00 13 74 00-05 4A 61 6D 65 73 74 00  |xp....t..Jamest.|
0000 0060:  04 52 79 61 6E 73 71 00-7E 00 00 00 00 00 1E 74  |.Ryansq.~......t|
0000 0070:  00 07 4F 62 69 2D 77 61-6E 74 00 06 4B 65 6E 6F  |..Obi-want..Keno|
0000 0080:  62 69                                            |bi              |

Read 130 bytes.

 

 

 

 

Posted by Scripter
,

역삼각함수란 삼각함수의 역함수를 의미하고,

역쌍곡선함수란 쌍곡선함수의 역함수를 의미한다.

수학에서 sin 함수의 역함수는 arcsin 으로 표기되는데, Octave 언어에서는 asin 함수로 구현되어 있다.

또한 Octave 언어에는 쌍곡선함수 sinhcosh 의 역함수로 각각 asinhacosh 가 구현되어 있지만, 비교를 위해 arcsinharccosh 라는 이름의 함수로 아래의 소스에 구현해 보았다.

%{
% Filename: testArcSine.m
%
% Execute: octave -qf testArcSine.m
%
% Date: 2013. 1. 8.
% Copyright (c) pkim _AT_ scripts.pe.kr
%}


function y = arcsinh(x)
    y = log(x + sqrt(x*x + 1));
endfunction


function y = arccosh(x)
    y = log(x + sqrt(x*x - 1));
endfunction


x = -0.9;
y = asin(x);

printf("y = asin(%g) = %.9f\n", x,  y);
printf("sin(y) = sin(%.9f) = %g\n", y, sin(y));
printf("\n");

x = 1.1;
u = acosh(x);
printf("u = acosh(%g) = %.10f\n", x,  u);

v = asinh(x);
printf("v = asinh(%g) = %.10f\n", x, v);

printf("cosh(u) = cosh(%.10f) = %g\n", u, cosh(u));
printf("sinh(v) = sinh(%.10f) = %g\n", v, sinh(v));
printf("\n");

printf("arccosh(%g) = %.10f\n", x, arccosh(x));
printf("arcsinh(%g) = %.10f\n", x, arcsinh(x));

#{
Output:
y = asin(-0.9) = -1.119769515
sin(y) = sin(-1.119769515) = -0.9

u = acosh(1.1) = 0.4435682544
v = asinh(1.1) = 0.9503469298
cosh(u) = cosh(0.4435682544) = 1.1
sinh(v) = sinh(0.9503469298) = 1.1

arccosh(1.1) = 0.4435682544
arcsinh(1.1) = 0.9503469298

#}

 

 

 

Posted by Scripter
,