25
Radiological Imaging Sciences School of Mechanical Engineering Pusan National University [email protected] MATLAB and Numerical Analysis

MATLAB and Numerical Analysis - Pusan National Universitybml.pusan.ac.kr/.../Undergraduates/NumAnalysis/MATLAB_3.pdf · 2017-03-06 · MATLAB and Numerical Analysis. Radiological

  • Upload
    others

  • View
    28

  • Download
    0

Embed Size (px)

Citation preview

Radiological Imaging SciencesSchool of Mechanical Engineering

Pusan National University

[email protected]

MATLAB and Numerical Analysis

MATLAB and Numerical Analysis

Radiological Imaging Sciences

Teaching Assistant

김동운[email protected]

윤종희[email protected]

Lab office: 통합기계관 120호 (☎ 510-3921)

방사선영상연구실홈페이지http://bml.pusan.ac.kr

2

MATLAB and Numerical Analysis

Radiological Imaging Sciences

Numerical analysis

복잡한공학적문제를해석하는일종의툴

Analytic analysis

Numerical analysis

Statistical analysis

3

532 =+

5152.0201.0 =×+×

532 ≈+

MATLAB and Numerical Analysis

Radiological Imaging Sciences

Why numerical analysis ?

4

MATLAB and Numerical Analysis

Radiological Imaging Sciences

MATLAB??

MATrix + LABoratory

수치해석, 행렬연산, 신호처리, 간단한그래픽기능등을통합

• 고성능의수치계산및결과의가시화제공툴박스

MATLAB 이용범위

• 복잡한공학적문제의모델링및해도출

• 수학적인계산 (행렬연산에 특화)

• 알고리즘개발 (text coding, graphical coding)

• 상황모델링과데이터분석

• 여러가지과학과공학적인그래프표현

• GUI를채택한 application 개발

5

MATLAB and Numerical Analysis

Radiological Imaging Sciences

Layout (version 7.6.0 R2008a)

6

Command window

Current directory

Current directory &

Workspace

Command history

Start and status

MATLAB and Numerical Analysis

Radiological Imaging Sciences

Help menu

Hotkey: F1

7

MATLAB and Numerical Analysis

Radiological Imaging Sciences

Matrices

Column – space, comma Raw – semicolon, enter

>>A=[1, 2, 3; 4 5 67 8, 8]

A=1 2 3 4 5 67 8 8

Matrix subscriptsA(i,j): the element in raw i and column j

A(1,2) = 2A(2,3) = 6

8

sum: the sum of matrix in one direction>>sum(A)12 15 17>>sum(sum(A))44

Transpose (AT=A’)>>A’1 4 72 5 83 6 8

diag: picks off the diagonal of A>>diag(A)158

MATLAB and Numerical Analysis

Radiological Imaging Sciences

Operation

+ addition - substraction * multiplication / right division \ left division ^ power ’ transpose

Colon operation (:)start : increment : end

Ex]>> 1:101 2 3 4 5 6 7 8 9 10>> 1:2:101 3 5 7 9>> A(:,2)+3 ???

9

Dot operation (.)component calculation

>>A^230 36 3966 81 9095 118 133

>>A.^21 4 916 25 3649 64 64

=

×

nfmeldkcjbia

fedcba

nmlkji

.

MATLAB and Numerical Analysis

Radiological Imaging Sciences

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

MATLAB and Numerical Analysis

Radiological Imaging Sciences

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

MATLAB and Numerical Analysis

Radiological Imaging Sciences

Structure statements (if, switch, while, for)

if

if relationstatements

elseif relationstatements

else relationstatements

end

while

while relationstatements

end

12

switch

switch referencecase value1

statementscase value2

statementsotherwise

statementsend

for (computing cost)

for index=start:increment:endstatements

end

MATLAB and Numerical Analysis

Radiological Imaging Sciences

Scalar & Vector functions

sin cos tan asin acos atan sinh cosh tanh exp log (natural log) rem (remainder) abs sqrt sign round floor

13

max min sum median any prod mean all sort std

MATLAB and Numerical Analysis

Radiological Imaging Sciences

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

MATLAB and Numerical Analysis

Radiological Imaging Sciences

M-file

MATLAB 언어로쓰여진파일• Script mode: 연속된 MATLAB 명령어 (command window 동일)• Function mode: 입/출력매개변수사용 (m-file 자체가하나의함수로사용)

15

M-file editorM-file generation

MATLAB and Numerical Analysis

Radiological Imaging Sciences

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

MATLAB and Numerical Analysis

Radiological Imaging Sciences

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=nendmean=sum(x)/m;stdev=sqrt(sum(x.^2)/m-mean.^2);

17

mean =4.0000 5.0000 5.6667

stdev = 2.4495 2.4495 2.0548

function & file name

output

MATLAB and Numerical Analysis

Radiological Imaging Sciences

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: 13iter=13

18

MATLAB and Numerical Analysis

Radiological Imaging Sciences

Output format

format short fixed point with 4 decimal places (default)A=1.7374

format long fixed point with 14 decimal placesA=1.737635236326245

format short e scientific notation with 4 decimal placesA=1.6543e+008

format long e scientific notation with 15 decimal placesA=1.654313216164868e+008

19

MATLAB and Numerical Analysis

Radiological Imaging Sciences

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;

20

Color default

Grid

MATLAB and Numerical Analysis

Radiological Imaging Sciences

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

MATLAB and Numerical Analysis

Radiological Imaging Sciences

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

MATLAB and Numerical Analysis

Radiological Imaging Sciences

Tip

clear all; close all; fclose all; clc Ctrl+C on command window

좌측하단 Start Preference

m-file 저장이름첫자리숫자불가, 기존내장함수와같은이름 불가 m-file 실행시 current folder 위치 (version에따라다름)

23

MATLAB and Numerical Analysis

Radiological Imaging Sciences

MATLAB application examples

24

MATLAB and Numerical Analysis

Radiological Imaging Sciences 25

Thanks for yourkind attention!!!