MATLAB
solve 함수는 대수 방정식을 풉니다. 가장 간단한 형태는 방정식을 따옴표로 감싸 인수로 전달하는 것입니다.
solve('x-5=0')
결과: ans = 5
변수에 저장: y = solve('x-5 = 0') → y = 5
우변 생략 가능: solve('x-5') → ans = 5
solve(equation, variable)
예: v - u - 3*t^2 = 0을 v에 대해 풀기
solve('v-u-3*t^2=0', 'v')
결과: ans = 3*t^2 + u
Octave는 roots 함수를 사용합니다. 계수 벡터를 인수로 줍니다.
roots([1, -5]) % x - 5 = 0 → 계수 [1, -5]
결과: ans = 5
solve는 고차 방정식도 풀 수 있으며, 근을 배열로 반환합니다.
eq = 'x^2 -7*x + 12 = 0';
s = solve(eq);
disp('The first root is: '), disp(s(1));
disp('The second root is: '), disp(s(2));
결과:
The first root is: 3 The second root is: 4
s = roots([1, -7, 12]);
disp('The first root is: '), disp(s(1));
disp('The second root is: '), disp(s(2));
결과: 4, 3 (순서 다를 수 있음)
삼차 방정식: (x-3)^2*(x-7)=0
solve('(x-3)^2*(x-7)=0')
결과: ans = 3, 3, 7 (중근 포함)
4차 방정식: x^4 - 7x^3 + 3x^2 - 5x + 9 = 0
eq = 'x^4 - 7*x^3 + 3*x^2 - 5*x + 9 = 0';
s = solve(eq);
disp('The first root is: '), disp(s(1));
disp('The second root is: '), disp(s(2));
disp('The third root is: '), disp(s(3));
disp('The fourth root is: '), disp(s(4));
% 수치화
disp('Numeric value of first root'), disp(double(s(1)));
disp('Numeric value of second root'), disp(double(s(2)));
disp('Numeric value of third root'), disp(double(s(3)));
disp('Numeric value of fourth root'), disp(double(s(4)));
결과 (수치):
6.6304 1.0598 -0.3451 - 1.0778i -0.3451 + 1.0778i
마지막 두 근은 복소수입니다.
v = [1, -7, 3, -5, 9];
s = roots(v);
disp('Numeric value of first root'), disp(double(s(1)));
disp('Numeric value of second root'), disp(double(s(2)));
disp('Numeric value of third root'), disp(double(s(3)));
disp('Numeric value of fourth root'), disp(double(s(4)));
solve로 다변수 연립방정식도 해결 가능.
s = solve('5*x + 9*y = 5', '3*x - 6*y = 4');
s.x
s.y
결과: x = 22/19, y = -5/57
3변수 연립방정식 예:
x + 3y - 2z = 5 3x + 5y + 6z = 7 2x + 4y + 3z = 8
solve에 방정식 문자열들을 나열하면 됩니다.
행렬 형태 A*x = b로 변환 후 A \ b로 풉니다.
A = [5, 9; 3, -6]; b = [5; 4]; A \ b
결과: x ≈ 1.1579, y ≈ -0.0877
syms로 심볼릭 변수 선언 후 사용.
syms x y expand((x-5)*(x+9)) expand((x+2)*(x-3)*(x-5)*(x+7)) expand(sin(2*x)) expand(cos(x+y)) collect(x^3*(x-7)) collect(x^4*(x-3)*(x-5))
결과:
x^2 + 4*x - 45 x^4 + x^3 - 43*x^2 + 23*x + 210 2*cos(x)*sin(x) cos(x)*cos(y) - sin(x)*sin(y) x^4 - 7*x^3 x^6 - 8*x^5 + 15*x^4
pkg load symbolic
symbols
x = sym('x'); y = sym('y'); z = sym('z');
expand((x-5)*(x+9))
expand((x+2)*(x-3)*(x-5)*(x+7))
expand(Sin(2*x))
expand(Cos(x+y))
collect(x^3*(x-7), z)
collect(x^4*(x-3)*(x-5), z)
syms x y factor(x^3 - y^3) factor([x^2-y^2, x^3+y^3]) simplify((x^4-16)/(x^2-4))
결과:
(x - y)*(x^2 + x*y + y^2) [ (x - y)*(x + y), (x + y)*(x^2 - x*y + y^2) ] x^2 + 4
MATLAB
다항식 표현 MATLAB은 다항식을 내림차순 계수 행 벡터로 표현합니다. 예: P(x) = x⁴ + 7x³ - 5x + 9 → p = [1 7 0 -5 9] (x² 항 계수 0 명시). 다항식 평가 (polyval, polyvalm) polyval(p, x): 스칼라 x에서 다항식 값 계산 polyvalm(p, X): 행렬 X에 대한 행렬 다항식 계산 (X는 정방행렬) 예제: x=4에서 평가 p = [1 7 0 -5 9]; polyval(p, 4) 결과: 693 예제: 행렬 다항식 평가 p = [1 7 0 -5 9]; X
Simulink 개요 Simulink는 MATLAB과 통합된 동적/임베디드 시스템 시뮬레이션 및 모델 기반 설계 환경입니다. MathWorks 개발로, 데이터플로 그래픽 프로그래밍 언어로 다중 영역 동적 시스템 모델링, 시뮬레이션, 분석을 지원합니다. 사용자 정의 가능한 블록 라이브러리를 제공하는 그래픽 블록 다이어그램 도구입니다. MATLAB 알고리즘을 모델에 통합하고 시뮬레이션 결과를 MATLAB로 내보내 추가 분석 가능. Simulink 지원 기능 시스템 레벨 설계 시뮬레이션 자동 코드 생성 임베디드 시스템 테스트 및 검증