Upload
truongquynh
View
221
Download
1
Embed Size (px)
Citation preview
Radiation Imaging Laboratory School of Mechanical Engineering
Pusan National University
Computational Numerical Analysis with MATLAB
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
Teaching Assistant
윤승만
Lab office: 기계관 3301 (☎ 3921)
방사선영상연구실 홈페이지
http://bml.pusan.ac.kr
2
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
Text book
“Applied Numerical Methods with MATLAB for Engineers and Scientists (2 ed.)”
Steven C. Chapra
McGRAW-HILL international edition
3
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
Numerical analysis
복잡핚 공학적 문제를 해석하는 일종의 툴
Analytic analysis
Numerical analysis
Statistical analysis
4
532
5152.0201.0
532
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
Why numerical analysis ?
5
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
MATLAB ??
MATrix + LABoratory
수치해석, 행렬연산, 싞호처리, 간단핚 그래픽 기능 등을 통합
• 고성능의 수치계산 및 결과의 가시화 제공 툴박스
MATLAB 이용범위
• 복잡핚 공학적 문제의 모델링 및 해 도출
• 수학적인 계산 (행렬연산에 특화)
• 알고리즘 개발 (text coding, graphical coding)
• 상황 모델링과 데이터 분석
• 여러 가지 과학과 공학적인 그래프 표현
• GUI를 채택핚 application 개발
6
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
Layout (version 7.6.0 R2008a)
7
Command window
Current directory
Current directory &
Workspace
Command history
Start and status
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
Help menu
Hotkey: F1
8
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
Matrices
Column – space, comma
Raw – semicolon, enter
>>A=[1, 2, 3; 4 5 6
7 8, 8]
A=
1 2 3
4 5 6
7 8 8
Matrix subscripts
A(i,j): the element in raw i and column j
A(1,2) = 2
A(2,3) = 6
9
sum: the sum of matrix in one direction
>>sum(A)
12 15 17
>>sum(sum(A))
44
Transpose (AT=A‟)
>>A‟
1 4 7
2 5 8
3 6 8
diag: picks off the diagonal of A
>>diag(A)
1
5
8
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
Operation
+ addition
- substraction
* multiplication
/ right division
\ left division
^ power
‟ transpose
Colon operation (:)
start : increment : end
Ex]
>> 1:10
1 2 3 4 5 6 7 8 9 10
>> 1:2:10
1 3 5 7 9
>> A(:,2)+3 ???
10
Dot operation (.)
component calculation
>>A^2
30 36 39
66 81 90
95 118 133
>>A.^2
1 4 9
16 25 36
49 64 64
nfme
ldkc
jbia
fe
dc
ba
nm
lk
ji
.
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
Statements, expressions and variables
변수명 (variable) = 수식 (expression)
• 변수: int16, int32, double, char, struct, cell 등
• 수식: MATLAB이 해석핛 수 있는 numerical, nonnumerical 수식
• MATLAB은 별도의 변수형에 대핚 정의는 필요 없음
변수와 함수는 정확히 구분핛 것
• variable=function(input_variable);
• 여기서 function은 MATLAB 내장 함수 혹은 사용자 정의 함수
A= a (정수) + b (실수)
결과 A 실수
11
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
Matrix building function
eye - identity matrix
zeros - matrix of zeros (variable initialization)
ones - matrix of ones
diag - see below
triu - upper triangular part of a matrix
tril - lower triangular part of a matrix
rand - randomly generated matrix
hilb - Hilbert matrix
magic - magic square
12
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
Structure statements (if, switch, while, for)
if
if relation
statements
elseif relation
statements
else relation
statements
end
while
while relation
statements
end
13
switch
switch reference
case value1
statements
case value2
statements
otherwise
statements
end
for (computing cost)
for index=start:increment:end
statements
end
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
Scalar & Vector functions
sin
cos
tan
asin
acos
atan
sinh
cosh
tanh
exp
log (natural log)
rem (remainder)
abs
sqrt
sign
round
floor
14
max
min
sum
median
any
prod
mean
all
sort
std
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
Matrix functions
eig eigenvalues and eigenvectors
chol Cholesky factorization
svd singular value decomposition
inv inverse matrix
lu LU factorization
qr QR factorization
hess hessenberg form
schur schur decomposition
rref reduced row echelon form
expm matrix exponential
sqrtm matrix square root
poly characteristic polynomial
det determinant
size size
norm 1-norm, 2-norm, F-norm, infinity norm
cond condition number in the 2-norm
rank rank
15
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
M-file
MATLAB 언어로 쓰여진 파일
• Script mode: 연속된 MATLAB 명령어 (command window 동일)
• Function mode: 입/출력 매개변수 사용 (m-file 자체가 하나의 함수로 사용)
16
M-file editor M-file generation
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
Script M-file
A script file consists of a sequence of normal MATLAB statements
clear all;
t= -pi : pi/100 : pi;
y= sin(t);
plot(t,y)
xlabel(„t [rad]‟)
ylabel(„sin(t)‟)
axis ([ -3 3 -1.2 1.2])
axis([x_min x_max y_min y_max])
17
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
Function M-file
Function files provide extensibility to MATLAB
You can create new functions specific to your problem which will then have the same status as other MATLAB functions
function [mean, stdev]=stat(x)
% STAT Mean and standard deviation
% For a vector x, stst(x) returns the
% mean and standard deviation of x.
% For a matrix x, stst(x) returns two row
% vectors containing, respectively, the
% mean and standard deviation of each column
[m n]=size(x);
if m==1
m=n
end
mean=sum(x)/m;
stdev=sqrt(sum(x.^2)/m-mean.^2);
18
mean = 4.0000 5.0000 5.6667 stdev = 2.4495 2.4495 2.0548
function & file name
output
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
Text strings, error messages, input
>> s = „This is a test‟
s= This is a test
>> disp(„this message is hereby displayed‟) or fprintf(„this message is hereby displayed\n‟)
this message is hereby displayed
>> error(„Sorry, the matrix must be symmetric‟)
??? Sorry, the matrix must be symmetric
>> iter= input(„Enter the number of iteration: ‟)
Enter the number of iteration: 13
iter=
13
19
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
Output format
format short fixed point with 4 decimal places (default)
A=1.7374
format long fixed point with 14 decimal places
A=1.737635236326245
format short e scientific notation with 4 decimal places
A=1.6543e+008
format long e scientific notation with 15 decimal places
A=1.654313216164868e+008
20
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
Graph
plot.m 함수 사용법
>> X=[1:10; 0.7*[1:10]; 0.5*[1:10]; 0.25*[1:10]]';
>> plot(X,'LineWidth',5);
>> xlabel('x axis','fontsize',15)
>> ylabel('y axis','fontsize',15)
>> title('plot.m 함수 이용법','fontsize',15)
>> grid on;
21
Color default
Grid
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
Graph
subplot.m 함수 사용법 subplot(m, n, p)
>> t=0:1/100:20;
>> y=sin(t);
>> subplot(2,2,1);
>> plot(t,y);
>> z=cos(2*t);
>> subplot(2,2,2);
>> plot(t,z);
>> x=exp(-t);
>> subplot(2,2,3);
>> plot(t,x);
>> w=exp(-sin(t));
>> subplot(2,2,4);
>> plot(t,w);
22
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
Graph
hold on; off;
>> t=0:1/1000:4;
>> x=exp(-t);
>> plot(t,x,'LineWidth',3);
>> hold on;
>> y=sin(t);
>> plot(t,y,'r','LineWidth',3);
>> hold off;
>> figure,plot(t,y); ?
23
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
Tip
clear all;
close all;
fclose all;
clc
Ctrl+C on command window
좌측 하단 Start Preference
m-file 저장이름 첫 자리 숫자 불가, 기존 내장함수와 같은 이름 불가
m-file 실행 시 current folder 위치 (version에 따라 다름)
24
Computational Numerical Analysis with MATLAB
Radiation Imaging Laboratory
MATLAB application examples
25