School of Mechanical Engineering Pusan National University dongwoonkim@pusan.ac.kr
Teaching Assistant 김동운 dongwoonkim@pusan.ac.kr Lab office: 기계관 3301 ( 510-3921) 방사선영상연구실홈페이지 http://bml.pusan.ac.kr 2
Numerical analysis 복잡한공학적문제를해석하는일종의툴 Analytic analysis 2 3 5 Numerical analysis 0.1 20 0.215 5 Statistical analysis 2 3 5 3
Why numerical analysis? 4
MATLAB?? MATrix + LABoratory 수치해석, 행렬연산, 신호처리, 간단한그래픽기능등을통합 고성능의수치계산및결과의가시화제공툴박스 MATLAB 이용범위 복잡한공학적문제의모델링및해도출 수학적인계산 ( 행렬연산에특화 ) 알고리즘개발 (text coding, graphical coding) 상황모델링과데이터분석 여러가지과학과공학적인그래프표현 GUI를채택한 application 개발 5
Layout (version 7.6.0 R2008a) Current directory Current directory & Workspace Command window Command history Start and status 6
Help menu Hotkey: F1 7
Matrices Column space, comma Raw semicolon, enter >>A=[1, 2, 3; 4 5 6 7 8, 8] sum: the sum of matrix in one direction >>sum(a) 12 15 17 >>sum(sum(a)) 44 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 Transpose (A T =A ) >>A 1 4 7 2 5 8 3 6 8 diag: picks off the diagonal of A >>diag(a) 1 5 8 8
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??? 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 i j a k l. c m n e b d f ia kc me jb ld nf 9
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 실수 10
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 11
Structure statements (if, switch, while, for) if if relation statements elseif relation statements else relation statements end switch switch reference case value1 statements case value2 statements otherwise statements end while while relation statements end for (computing cost) for index=start:increment:end statements end 12
Scalar & Vector functions sin cos tan asin acos atan sinh cosh tanh exp log (natural log) rem (remainder) abs sqrt sign round floor max min sum median any prod mean all sort std 13
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 14
M-file MATLAB 언어로쓰여진파일 Script mode: 연속된 MATLAB 명령어 (command window 동일 ) Function mode: 입 / 출력매개변수사용 (m-file 자체가하나의함수로사용 ) M-file generation M-file editor 15
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]) 16
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) function & file name % STAT Mean and standard deviation output % 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); mean = 4.0000 5.0000 5.6667 stdev = 2.4495 2.4495 2.0548 17
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 18
Output format format short A=1.7374 fixed point with 4 decimal places (default) format long A=1.737635236326245 fixed point with 14 decimal places format short e A=1.6543e+008 scientific notation with 4 decimal places format long e A=1.654313216164868e+008 scientific notation with 15 decimal places 19
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; Color default Grid 20
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); 21
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);? 22
Tip clear all; close all; fclose all; clc Ctrl+C on command window 좌측하단 Start Preference m-file 저장이름첫자리숫자불가, 기존내장함수와같은이름불가 m-file 실행시 current folder 위치 (version 에따라다름 ) 23
Assignment m-file 제출 (dongwoonkim@pusan.ac.kr) 파일명 : 이름이니셜 + 학번 ( 예시 : KDW200000000.m) 제출기한 : 3 월 22 일오후 6:00 1) 학번행렬정의 학번첫자리두번째세번째 2 0 1 네번째다섯번째여섯번째 4 2 1 일곱번째여덟번째마지막자리 3 3 5 2) 날짜행렬정의 년 월 일 2014 3 13 3) 날짜행렬의합, 학번행렬의제곱, 학번행렬과날짜행렬의곱, 학번행렬의역행렬 4) 두번째매트랩강의를위한 mfile, 연구실홈페이지에서다운로드받아올것 24
MATLAB application examples 25
Thanks for your kind attention!!! 26