demo-07

랜덤 함수와 히스토그램

course/basic/demo-07.m

코드 인덱스로 돌아가기

카테고리

Course Basic

학습 소스 그룹

코드 길이

78

lines

작성자

won sunggyu

2025-04-22

패키지

none

pkg load 기준

전체 코드

전체 코드를 복사해서 Octave에서 바로 실행할 수 있습니다.

# filename: demo-07.m
# writer: won sunggyu
# date: 2025-04-22
# language: octave
# description: 랜덤 함수와 히스토그램

#------------------------------------------------------------------------------
# 초기화
#------------------------------------------------------------------------------

run("startup.m");
printf(fmt("{mfilename}\n", "#FF5733"));

#------------------------------------------------------------------------------
# 데이터 준비
#------------------------------------------------------------------------------

dx = 0.1;
nx = 4000;
x = 0:dx:dx*(nx-1);

#------------------------------------------------------------------------------
# 데이터 연산
#------------------------------------------------------------------------------

rand ("seed", 1234)
r_rand = rand(1, nx); # Unitform Random Number (0, 1)
r_randn = randn(1, nx); # Normal Random Number (0, 1)
r_rande = rande(1, nx); # Exponentially Distributed Random Number
r_randp = randp(6, 1, nx);  # Poisson Random Number, first param is degree
r_randg = randg(1, 1, nx);  # Gamma Random Number, first param is shape

[counts, edges] = hist(r_rand, 40);
[countn, edgen] = hist(r_randn, 40);
[counte, edgee] = hist(r_rande, 40);
[countp, edgep] = hist(r_randp, 40);
[countg, edgeg] = hist(r_randg, 40);

#------------------------------------------------------------------------------
# 그래프 그리기
#------------------------------------------------------------------------------

# 그래프
figured("Size", [720, 960], "Move", [-1280, 0], "Name", mfilename);
ax = subplots(5, 2, "Xlabel", "Index", "Ylabel", "Value", "FontSize", 12);

plot(ax(1, 1), x, r_rand);
plot(ax(2, 1), x, r_randn);
plot(ax(3, 1), x, r_rande);
plot(ax(4, 1), x, r_randp);
plot(ax(5, 1), x, r_randg);

barh(ax(1, 2), edges, counts, "BarWidth", 0.5, "FaceColor", color_base(1,:), "EdgeColor", color_base(1,:));
barh(ax(2, 2), edgen, countn, "BarWidth", 0.5, "FaceColor", color_base(1,:), "EdgeColor", color_base(1,:));
barh(ax(3, 2), edgee, counte, "BarWidth", 0.5, "FaceColor", color_base(1,:), "EdgeColor", color_base(1,:));
barh(ax(4, 2), edgep, countp, "BarWidth", 0.5, "FaceColor", color_base(1,:), "EdgeColor", color_base(1,:));
barh(ax(5, 2), edgeg, countg, "BarWidth", 0.5, "FaceColor", color_base(1,:), "EdgeColor", color_base(1,:));

set(ax(1, 1), "Title", "Uniform Random Number (0, 1)");
set(ax(2, 1), "Title", "Normal Random Number (0, 1)", "Ylim", [-3, 3]);
set(ax(3, 1), "Title", "Exponentially Distributed Random Number");
set(ax(4, 1), "Title", "Poisson Distributed Random Number");
set(ax(5, 1), "Title", "Gamma Distributed Random Number");

set(ax(1, 2), "Xlabel", "", "Ylabel", "");
set(ax(2, 2), "Xlabel", "", "Ylabel", "", "Ylim", [-3, 3]);
set(ax(3, 2), "Xlabel", "", "Ylabel", "");
set(ax(4, 2), "Xlabel", "", "Ylabel", "");
set(ax(5, 2), "Xlabel", "", "Ylabel", "");

set_xticknum(ax(1, 2), 3);
set_xticknum(ax(2, 2), 3);
set_xticknum(ax(3, 2), 3);
set_xticknum(ax(4, 2), 3);
set_xticknum(ax(5, 2), 3);

set_axes_layout(ax, [1, 1, 1, 1, 1], [6, 1]);

코드 해설

목적

랜덤 함수와 히스토그램

입력

  • 스크립트 상단에서 정의한 파라미터/입력 데이터를 사용합니다.

출력

  • 그래프/figure 출력
  • 콘솔 텍스트 출력

실행 흐름

  1. 초기화
  2. 데이터 준비
  3. 데이터 연산
  4. 그래프 그리기
  5. 그래프

핵심 함수/주제

axcolor_basesetbarhhistplotset_xticknumNumber

실습 과제

  • 질량/감쇠/강성 또는 전달함수 계수를 바꿔 응답 변화를 확인해보세요.
  • 축 범위와 라벨을 바꿔 그래프 해석성이 어떻게 달라지는지 확인해보세요.
  • 핵심 함수 ax의 인자를 한 가지 바꿔 결과 변화를 기록해보세요.

학습 팁

  • 그래프 비교 시 축 범위(XLim/YLim)와 단위를 먼저 고정하면 해석 오류를 줄일 수 있습니다.

같은 카테고리의 다른 코드