matlab 개요와 응용 - 1장...
TRANSCRIPT
MATLAB
An Introduction with Applications
Chapter 1. Starting with MATLAB
05
1015
2025
0
10
20
30-10
-5
0
5
10
2012-1 Hyoungsuk Yoo
Ch1. Starting MATLAB 2/55
MATLAB Windows
MATLAB을 실행하면, 데스크탑 창이 열리며, 기본 화면에는 Command Window,
Current Directory Window, Command History Window 등 세 개의 작은 창이 포
함되어 있다.
창의 왼쪽 하단부에 있는 Start 버튼을 이용하여 MATLAB의 여러 도구와 기능에
접근할 수 있다.
Ch1. Starting MATLAB 3/55
MATLAB Windows
Command Window
MATLAB을 실행시키면 나타나는 메인 창.
Desktop 메뉴→Desktop Layout 메뉴에서 명령어 창의 모양을 선택함 .
“Command Window Only”를 선택하면, 명령어 창 하나만 보임.
Default desktop layout
Command Window Only
Ch1. Starting MATLAB 4/55
MATLAB Windows
Editor Window 프로그램 작성 및
편집
그림창 그래프 명령어가 실행되면 자동 생성됨
Toolbar
Workspace Window 사용된 변수들에 대한
정보 제공
Command History Window
명령어 창에서 입력된 명령어들이 기록되어 있음
Ch1. Starting MATLAB 5/55
MATLAB Help Windows
Ch1. Starting MATLAB 6/55
Undock and Dock
dock
undock
Ch1. Starting MATLAB 7/55
Arithmetic Operations With Scalars
MATLAB에서 사용하는 산술연산자들의 기호 :
Operation Symbol Example
Addition + 5+3
Subtraction - 5-3
Multiplication * 5*3
Right division / 5/3
Left division \(\) 5\3(=3/5)
Exponentiation ^ 5^3( Means 53)
주) 한글Windows에서는 ‘\’가
‘\’(키보드의 \)로 표시됨
왼쪽 나눗셈을 제외한 나머지 기호들은 대부분의 계산기에서와 같다. 스칼
라의 경우, 왼쪽 나눗셈(left division)은 오른쪽 나눗셈(right division)의 역
수이지만, 배열에 대한 연산(3장 참조)에 대해서는 왼쪽 나눗셈이 주로 사용
된다.
Ch1. Starting MATLAB 8/55
Order of Precedence
MATLAB에서의 산술 연산 우선 순위
여러 연산이 포함된 식에서, 우선 순위가 더 높은 연산이 더 낮은 연산보다
먼저 수행된다.
둘 이상의 연산이 같은 우선순위를 가지면, 왼쪽에서 오른쪽으로 식이 수
행된다.
계산 순서를 바꾸기 위해 괄호를 사용할 수 있다.
Precedence Mathematical Operation
First Parentheses ※ For nested Parentheses, the innermost are executed first.
Second Exponentiation
Third Multiplication, division (equal precedence).
Fourth Addition and subtraction.
Ch1. Starting MATLAB 9/55
Example of Arithmetic Operation (1/3)
>> 4*50+3*100+10^2-400/2
ans =
400
>> 2^5/(2^5-1)
ans =
1.0323
>> 2*(sqrt(5)-1)/(sqrt(5)+1)^2-1
ans =
-0.7639
>> 10*acos(-1)
ans =
31.4159
>> x=2; y=6+x, x=y+7
y =
8
x =
15
>> exp(3); % e3
>> log10(10^5); % log10 105
Ch1. Starting MATLAB 10/55
MATLAB에서 산술연산은 명령어 창에 수식을 입력한 후 Enter 키를 누르
면 계산이 되며, 그다음 두 줄에 걸쳐 ‘ans=’과 결과(답)가 표시된다.
>> 7+8/2 % 8/2가 먼저 수행됨
ans =
11
>> (7+8)/2
ans =
7.5000
>> 4+5/3+2 % 5/3이 먼저 수행됨
ans =
7.6667
>> 5^3/2 % 53이 먼저 수행됨
ans =
62.5000
>> 27^(1/3)+32^0.2 % 27(1/3)+320.2
ans =
5
>> 27^1/3+32^0.2 % (271)/3 + 320.2
ans =
11
>> 0.7854-(0.7854)^3/(1*2*3)+0.785^5/…
(1*2*3*4*5)-(0.785)^7/(1*2*3*4*5*6*7)
ans =
0.7071
>> 7/2
ans =
3.5000
>> 7\2 % \대신 \가 표시됨(한글윈도)
ans =
0.2857
>> ans
ans =
0.2857
Example of Arithmetic Operation (2/3)
Ch1. Starting MATLAB 11/55
← 연산자 사이에 공백이 있어도 상관없다!
>> 2*4^3 + 5 % 243 + 5
ans =
133
>> (2*4)^3 + 5 % (24)3 + 5
ans =
517
>> (2* 4) ^3 / 5 % (24)3/5
ans =
102.4
>> 9^(1/2) % 91/2
ans =
3
>> 9^1/2 % 91/2
ans =
4.5000
오류를 피하거나 식을 읽기 쉽도록 괄호를 적절하게 사용하는 것이 좋다.
Example of Arithmetic Operation (3/3)
Ch1. Starting MATLAB 12/55
Command Description Example
format short e Scientific notation with 4 decimal digits.
>> 290/7 ans = 4.1429e+001
format long e Scientific notation with 15 decimal digits.
>> 290/7 ans = 4.142857142857143e+001
format short g Best of 5-digit fixed or floating point
>> 290/7 ans = 41.429
format long g Best of 15-digit fixed or floating point.
>> 290/7 ans = 41.4285714285714
format bank Two decimal digits.
>> 290/7 ans = 41.43
format compact Eliminates empty lines to allow more lines with information displayed on
the screen.
format loose Adds empty lines (opposite of compact).
Display Format (2/3)
Ch1. Starting MATLAB 13/55
Elementary Math Functions
전체 내장함수 목록은 Help Window에서 종류별로 분류된 목록을 참조한다.
Function Description Example
sqrt(x) Square root. >> sqrt(81)
ans = 9
nthroot(x, n) Real nth root of a real number x.
(If x is negative n must be an odd integer.)
>> nthroot(80, 5)
ans = 2.4022
exp(x) Exponential (ex). >> exp(5)
ans = 148.4132
abs(x) Absolute value >> abs(-24)
ans = 24
log(x) Natural logarithm.
Base e logarithm (ln)
>> log(1000)
ans = 6.9078
log10(x) Base 10 logarithm >> log10(1000)
ans = 3.0000
factorial(x) The factorial function x!
(x must be a positive integer.)
>> factorial(5)
ans = 120
Ch1. Starting MATLAB 14/55
Trigonometric Math Functions
Function Description Example
sin(x)
sind(x)
Sine of angle x (x in radians)
Sine of angle x (x in degrees)
>> sin(pi/6) ans = 0.5000
cos(x)
cosd(x)
Cosine of angle x (x in radians)
Cosine of angle x (x in degrees)
>> cosd(30) ans = 0.8660
tan(x)
tand(x)
Tangent of angle x (x in radians)
Tangent of angle x (x in degrees)
>> tan(pi/6) ans = 0.5774
cot(x)
cotd(x)
Cotangent of angle x (x in radians)
Cotangent of angle x (x in degrees)
>> cotd(30) ans = 1.7321
각이 라디안 단위일 때 삼각함수는 sin(x), cos(x), tan(x), cot(x)이고 역삼각함수는
asin(x), acos(x), atan(x), acot(x)이다.
각이 도(degree) 단위일 때 삼각함수는 sind(x), cosd(x), tand(x), cotd(x)이고 역삼
각함수는 asind(x), acosd(x), atand(x), acotd(x)이다.
쌍곡삼각함수(hyperbolic trigonometric function)는 sinh(x), cosh(x), tanh(x),
coth(x)이다. π는 pi로 입력한다.
Ch1. Starting MATLAB 15/55
Rounding function
Function Description Example
round(x) Round to the nearest integer.
>> round(3.4) ans = 3
fix(x) Round toward zero.
>> fix(2.6) ans = 2
ceil(x) Round toward infinity.
>> ceil(2.2) ans = 3
floor(x) Round toward minus infinity.
>> floor(-2.25) ans = -3
rem(x, y) Returns the remainder after x is divided by y
>> rem(13,5) ans = 3
sign(x) Signum function. Returns 1 if x > 0, -1 if x < 0 , and 0 if
x = 0.
>> sign(5) ans = 1
Ch1. Starting MATLAB 16/55
반드시 공백이 들어가야 함
명령이 길어서 한 줄에 입력할 수 없는 경우, 마침표(.) 세 개를 찍고 Enter 키
를 눌러도 커서의 위치만 다음 줄로 바뀔 뿐 명령은 실행되지 않는다. 연산자
는 분리할 수 있으나, 변수명은 분리해서 쓸 수 없다.
>> Class_1=20; Class_2=15; Class_3=14; Class_4=17; Total_Number=240;
>> ave = (Class_1 + Class_2 + Class_3 + Class_4) …
/ Total_Number * 100
ans = 27.5
>> ave=(Class_1+Class_2+Class_3+Class_4)/Total_ …
??? ave=(Class_1+Class_2+Class_3+Class_4)/Total …
Error: The input character is not valid in MATLAB statements or
expressions.
>> a=4; b=3; sum=a+b
sum = 7
>> a=7; sum
sum = 7
Variable
a의 값이 바뀌어도 Excel과 달리 sum의 값은 변동이 없다.
Ch1. Starting MATLAB 17/55
Useful Commands For Managing Variable
Command Outcome
clear Removes all variables form the memory.
clear x y z Removes only variables x, y, and z from the memory.
who Displays a list of the variables currently in the memory.
whos
Displays a list of the variables currently in the memory and
their sizes together with information about their bytes and
class.