Professional Documents
Culture Documents
MATLAB 일반
- 사칙연산 : +,-,*,/
예) 먼저 MATLAB을 실행하면 MATLAB Command Window가 뜬다. 그리고 다음을
타이핑 하고 리턴을 치면 다음과 같은 결과를 얻는다. 나머지 연산도 연습을 해보자.
>>1.5+3.5
ans =
5
>> help +
Operators and special characters.
Arithmetic operators.
plus - Plus +
uplus - Unary plus +
minus - Minus -
uminus - Unary minus -
mtimes - Matrix multiply *
1
times - Array multiply .*
mpower - Matrix power ^
power - Array power .^
mldivide - Backslash or left matrix divide \
mrdivide - Slash or right matrix divide /
ldivide - Left array divide .\
rdivide - Right array divide ./
kron - Kronecker tensor product kron
Relational operators.
eq - Equal ==
ne - Not equal ~=
lt - Less than <
gt - Greater than >
le - Less than or equal <=
ge - Greater than or equal >=
Logical operators.
Short-circuit logical AND &&
Short-circuit logical OR ||
and - Element-wise logical AND &
or - Element-wise logical OR |
not - Logical NOT ~
xor - Logical EXCLUSIVE OR
any - True if any element of vector is nonzero
all - True if all elements of vector are nonzero
2
>>a+b
ans =
5
;을 붙이지 않았을 때
>>c=1.0
c=
1
;을 붙였을 때
>>c=1.0;
>>
- 다양한 수학 함수 (exp, log, log10, sin, asin (역사인함수), cos, acos, tan, atan, atan2,
^ (승), sind, asind, cosd, tand, abs 등)
>> 5.0^3
ans =
125
>>exp(5)
ans =
148.4132
1.2 행렬 지정과 행렬 연산
- 행벡터 지정과 값 불러오기
>>A=[1 2 3 4 5]
A=
1 2 3 4 5
>>A(3)
ans =
3
>>A(5)
ans =
5
3
- 열벡터 지정
>> B=[1; 2; 3; 4; 5]
B=
1
2
3
4
5
- 행렬 지정과 값 불러오기
>>C=[1 2 3 ; 4 5 6]
C=
1 2 3
4 5 6
>>C(2,3)
ans =
6
- 행렬의 transpose
>> D=[1 3 5 6 ; 6 3 0 -3]
D=
1 3 5 6
6 3 0 -3
>>E=D'
E=
1 6
3 3
5 0
6 -3
- 행렬의 더하기(+)
>> A=[1 2 3 ; 6 3 0 ]
A=
1 2 3
6 3 0
>> B=[ 0 2 1 ; 9 5 1]
4
B=
0 2 1
9 5 1
>>C=A+B
C=
1 4 4
15 8 1
- 행렬 곱하기 (*)
>>A=[1 2 3 ; 6 3 0 ]
A=
1 2 3
6 3 0
>>B=[-1 -3 ; -6 -8; -1 -2]
B=
-1 -3
-6 -8
-1 -2
>>C=A*B
C=
-16 -25
-24 -42
- 역 행렬 구하기 (inv)
>> A=[1 2 3; 8 4 0; -6 -5 -3]
A=
1 2 3
8 4 0
-6 -5 -3
>> C=inv(A)
C=
1.0000 0.7500 1.0000
-2.0000 -1.2500 -2.0000
1.3333 0.5833 1.0000
5
1.3 그래프
- 그래프를 그리는데 plot이라는 명령을 사용할 것이다. 먼저 다음과 같이 도움말의
도움을 받아 보자.
Various line types, plot symbols and colors may be obtained with
PLOT(X,Y,S) where S is a character string made from one element
from any or all the following 3 columns:
b blue . point - solid
g green o circle : dotted
r red x x-mark -. dashdot
c cyan + plus -- dashed
m magenta * star (none) no line
y yellow s square
k black d diamond
v triangle (down)
^ triangle (up)
< triangle (left)
> triangle (right)
p pentagram
h hexagram
For example, PLOT(X,Y,'c+:') plots a cyan dotted line with a plus
at each data point; PLOT(X,Y,'bd') plots blue diamond at each data
point but does not draw any line.
6
PLOT(X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,...) combines the plots defined by
the (X,Y,S) triples, where the X's and Y's are vectors or matrices
and the S's are strings.
12
10
0
1 2 3 4 5 6 7 8 9 10
7
12
10
6
y
0
1 2 3 4 5 6 7 8 9 10
x
12
10
6
y
0
1 2 3 4 5 6 7 8 9 10
x
8
12
process output
10
6
y
0
1 2 3 4 5 6 7 8 9 10
x
11 °øÁ¤ µ¥ÀÌŸ
10.5
10
y
9.5
8.5
8
2 3 4 5 6 7 8 9
x
9
>> x=[1 2 3 4 5 6 7 8 9 10];
>> y=[0 5 8 10 11 11 10 8 5 0];
>> plot(x,y,'k-+','LineWidth',2)
>> xlabel('x')
>> ylabel('y')
>> legend('process data')
>> title('test graph')
>> axis([0 11 -1 12])
>>
test graph
12
process data
10
6
y
0 1 2 3 4 5 6 7 8 9 10 11
x
10
PLOT3(X,Y,Z), where X, Y and Z are three matrices of the same size,
plots several lines obtained from the columns of X, Y and Z.
Various line types, plot symbols and colors may be obtained with
PLOT3(X,Y,Z,s) where s is a 1, 2 or 3 character string made from
the characters listed under the PLOT command.
Example: A helix:
t = 0:pi/50:10*pi;
plot3(sin(t),cos(t),t);
11
0
-50
-100
-150
-200
-250
5
5
0
0
-5 -5
12
geometrically rectangular grid.
test 3D graph
50
40
30
z
20
10
0
5
5
0
0
y -5 -5
x
13
>> view([0,20])
>> view([10,20])
>> view([20,20])
>> view([30,20])
>> view([40,20])
>> view([40,30])
>> view([40,40])
>> view([40,50])
test 3D graph
60
40
z
20
0 5
-5
0
0
5 -5 y
x
14
SURF returns a handle to a surface plot object.
AXIS, CAXIS, COLORMAP, HOLD, SHADING and VIEW set figure, axes, and
surface properties which affect the display of the surface.
>> surf(x,y,z)
>> view([40,40])
>> colorbar
>>
45
40
50 35
40
30
30
25
20
20
10
15
0 5
-5 10
0 5
0
5 -5
15
pairs to specify additional properties of the contour object.
Example:
[c,h] = contour(peaks); clabel(c,h), colorbar
4 45
3 40
2
35
1
30
0
25
-1
20
-2
-3 15
-4 10
-5
-5 0 5
>> [c,h]=contour(x,y,z);
>> clabel(c,h)
16
5 30 25 30 45
45 40
40 20 25
4 35
15
35
25 20 15 20
3 10
2 5
10
25
15
25
20
1
15
10
5
10
0
20
-1
-2 5
15
10
25
10 15
25
20
-3
35 15 20
-4 35
40 20
45 30 25 25
30 40 45
-5
-5 -4 -3 -2 -1 0 1 2 3 4 5
17
>> x=[1 2 3 4 5];
>> y=[5.5 43.1 128 290.7 498.4];
>> p=polyfit(x,y,3)
p=
-0.1917 31.5821 -60.3262 35.3400
>> x2=1:0.1:5
x2 =
Columns 1 through 8
18
1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000
Columns 9 through 16
1.8000 1.9000 2.0000 2.1000 2.2000 2.3000 2.4000 2.5000
Columns 17 through 24
2.6000 2.7000 2.8000 2.9000 3.0000 3.1000 3.2000 3.3000
Columns 25 through 32
3.4000 3.5000 3.6000 3.7000 3.8000 3.9000 4.0000 4.1000
Columns 33 through 40
4.2000 4.3000 4.4000 4.5000 4.6000 4.7000 4.8000 4.9000
Column 41
5.0000
>> y2=polyval(p,x2);
>> plot(x,y,'o',x2,y2)
>> grid on
>>
500
450
400
350
300
250
200
150
100
50
0
1 1.5 2 2.5 3 3.5 4 4.5 5
19
ans =
C:\MATLAB7\work
- 원하는 디렉토리로 바꾸고 싶어면 cd를 사용하면 present working directory 가 바뀐다.
>> cd d:\swsung
>> pwd
ans =
d:\swsung
- m파일 만들기
MATLAB Command Window에서 File메뉴 New m-file을 누르면 m-file을
작성할 수 있다.
sum=0.0;
for i=1:3
i=i
sum=sum+i
end
20
3
sum =
6
- if문 사용 예제 (두 값 중에 큰 값을 출력하기)
다음을 타이핑하고 나서 파일 이름을 example2.m으로 원하는 directory에 저장을
하자.
a=1.0;
b=2.0;
if (a>b)
c=a
else
c=b
end
>> a
a=
1
>> b
b=
2
>> c
c=
2
>>
21
다음을 타이핑하고 나서 파일 이름을 example3.m으로 원하는 directory에 저장을
하자.
function [c]=example3(a,b)
if (a>b)
c=a
else
c=b
end
return
>> d=example3(1,2)
c=
2
d=
2
22
3.0 3.1
4.0 3.4
5.0 4.0
이것을 다음과 같이 바꾸고 저장을 example4.m으로 저장하자.
data=[
1.0 2.0
2.0 2.5
3.0 3.1
4.0 3.4
5.0 4.0];
그리고 나서 MATLAB Command Window에서 example4를 실행하면 다음과 같은
결과를 얻게 된다. 이때 pwd가 m-file과 같은 directory이어야 한다.
>> example4
>> data
data =
1.0000 2.0000
2.0000 2.5000
3.0000 3.1000
4.0000 3.4000
5.0000 4.0000
- fprintf를 이용한 파일 출력
다음과 같이 된 example5.m을 만들자.
x=[1 2 3 4 5 6 7 8 9 10];
y=[1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 10.0];
23
2.00 2.20000000
3.00 3.30000000
4.00 4.40000000
5.00 5.50000000
6.00 6.60000000
7.00 7.70000000
8.00 8.80000000
9.00 9.90000000
10.00 10.00000000
ODE45 can solve problems M(t,y)*y' = f(t,y) with mass matrix M that is
nonsingular. Use ODESET to set the 'Mass' property to a function MASS if
MASS(T,Y) returns the value of the mass matrix. If the mass matrix is
24
constant, the matrix can be used as the value of the 'Mass' option. If
the mass matrix does not depend on the state variable Y and the function
MASS is to be called with one input argument T, set 'MStateDependence' to
'none'. ODE15S and ODE23T can solve problems with singular mass matrices.
Example
[t,y]=ode45(@vdp1,[0 20],[2 0]);
plot(t,y(:,1));
solves the system y' = vdp1(t,y), using the default relative error
tolerance 1e-3 and the default absolute tolerance of 1e-6 for each
component, and plots the first component of the solution.
See also
other ode solvers: ode23, ode113, ode15s, ode23s, ode23t, ode23tb
options handling: odeset, odeget
output functions: odeplot, odephas2, odephas3, odeprint
evaluating solution: deval
ode examples: rigidode, ballode, orbitode
25
dy(1,1)=-y(1)+1;
dy(2,1)=0.2*y(1)-y(2);
return
이것은 수식으로는 다음의 미분 방정식을 의미한다.
0.9
0.8
y1
0.7 y2
0.6
0.5
0.4
0.3
0.2
0.1
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
26
>> [t,y]=ode45(@test_func,[0 2 3 5],[0 0]);
>> t
t=
0
2
3
5
>> y
y=
0 0
0.8647 0.1188
0.9502 0.1602
0.9933 0.1919
27
0 0
0.8647 0.1188
0.9502 0.1602
0.9933 0.1919
28