# Computational Numerical Analysis with ?· 2011-03-18 · “Applied Numerical Methods with MATLAB for…

• View
213

1

Embed Size (px)

Transcript

• Radiation Imaging Laboratory School of Mechanical Engineering

Pusan National University

smane1547@pusan.ac.kr

Computational Numerical Analysis with MATLAB

• Computational Numerical Analysis with MATLAB

Teaching Assistant

smane1547@pusan.ac.kr

Lab office: 3301 ( 3921)

http://bml.pusan.ac.kr

2

mailto:smane1547@pusan.ac.krhttp://bml.pusan.ac.kr/

• Computational Numerical Analysis with MATLAB

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

Numerical analysis

Analytic analysis

Numerical analysis

Statistical analysis

4

532

5152.0201.0

532

• Computational Numerical Analysis with MATLAB

Why numerical analysis ?

5

• Computational Numerical Analysis with MATLAB

MATLAB ??

MATrix + LABoratory

, , ,

MATLAB

( )

(text coding, graphical coding)

GUI application

6

• Computational Numerical Analysis with MATLAB

Layout (version 7.6.0 R2008a)

7

Command window

Current directory

Current directory &

Workspace

Command history

Start and status

• Computational Numerical Analysis with MATLAB

Hotkey: F1

8

• Computational Numerical Analysis with MATLAB

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

Operation

- 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

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

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

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

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

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

M-file

MATLAB

Script mode: MATLAB (command window )

Function mode: / (m-file )

16

M-file editor M-file generation

• Computational Numerical Analysis with MATLAB

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)

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

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

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

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

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

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