demo-02
연산자, 행렬 연산자, 행렬 연산 함수
course/basic/demo-02.m
전체 코드
전체 코드를 복사해서 Octave에서 바로 실행할 수 있습니다.
# filename: demo-02.m
# writer: won sunggyu
# date: 2025-04-22
# language: octave
# description: 연산자, 행렬 연산자, 행렬 연산 함수
#------------------------------------------------------------------------------
# 초기화
#------------------------------------------------------------------------------
run("startup.m");
printf(fmt("{mfilename}\n", "#FF5733"));
#------------------------------------------------------------------------------
# 데이터 준비
#------------------------------------------------------------------------------
x = [1 2 3];
y = [4 5 6];
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];
#------------------------------------------------------------------------------
# 데이터 연산
#------------------------------------------------------------------------------
# pointwise multiplication
r_pointwise_add = x + y # (1,3) + (1,3) = (1,3)
r_pointwise_sub = x - y # (1,3) - (1,3) = (1,3)
r_pointwise_mul = x .* y # (1,3) .* (1,3) = (1,3)
r_pointwise_div = x ./ y # (1,3) ./ (1,3) = (1,3)
# implicit expansion applize automatically while operating
r_pointwise_add_w = x + y' # (1,3) + (3,1) = (3,3)
r_pointwise_sub_w = x - y' # (1,3) - (3,1) = (3,3)
# inner product
r_inner1 = x * y' # (1,3) * (3,1) = (1,1)
r_inner2 = dot(x, y)
# cross product: 2D or 3D geometry
r_cross = cross(x, y)
# outer product with (pointwise multiplication)
r_outer_w = x .* y' # (1,3) .* (3,1) = (3,3)
# dyadic product (no good for big size data)
r_dyadic_w = x' .* y # (3,1) .* (1,3) = (3,3)
# matrix operation
m_add = A + B # (2,2) + (2,2) = (2,2)
m_sub = A - B # (2,2) - (2,2) = (2,2)
m_mul = A .* B # (2,2) .* (2,2) = (2,2)
m_div = A ./ B # (2,2) ./ (2,2) = (2,2)
m_mmul = A * B # (2,2) * (2,2) = (2,2)
# matrix solve A * x = B
m_sol1 = A \ B # (2,2) * (2,2) = (2,2)
m_sol2 = inv(A) * B # (2,2) * (2,2) = (2,2)
m_sol3 = linsolve(A, B) # (2,2) * (2,2) = (2,2)
#------------------------------------------------------------------------------
# 그래프 그리기
#------------------------------------------------------------------------------
코드 해설
목적
연산자, 행렬 연산자, 행렬 연산 함수
입력
- 스크립트 상단에서 정의한 파라미터/입력 데이터를 사용합니다.
출력
- 콘솔 텍스트 출력
실행 흐름
- 초기화
- 데이터 준비
- 데이터 연산
- pointwise multiplication
- 그래프 그리기
핵심 함수/주제
crossdotfmtinvlinsolveprintfproductrun
실습 과제
- 질량/감쇠/강성 또는 전달함수 계수를 바꿔 응답 변화를 확인해보세요.
- 핵심 함수 cross의 인자를 한 가지 바꿔 결과 변화를 기록해보세요.
- "초기화 -> 데이터 준비" 흐름을 함수 단위로 분리해 리팩터링해보세요.
학습 팁
같은 카테고리의 다른 코드
- colored
course/basic/colored.m - demo-00
course/basic/demo-00.m - demo-01
course/basic/demo-01.m - demo-03a
course/basic/demo-03a.m - demo-03b
course/basic/demo-03b.m - demo-04
course/basic/demo-04.m - demo-05
course/basic/demo-05.m - demo-06
course/basic/demo-06.m - demo-07
course/basic/demo-07.m - demo-08a
course/basic/demo-08a.m