CLASS: 01 - Bangladesh University of Engineering and...

71
CLASS: 01 ME 262: Numerical Analysis Sessional Department of Mechanical Engineering, BUET Department of Mechanical Engineering, BUET

Transcript of CLASS: 01 - Bangladesh University of Engineering and...

CLASS: 01

ME 262: Numerical Analysis Sessional

Department of Mechanical Engineering, BUETDepartment of Mechanical Engineering, BUET

Tentative Schedule

Class TOPICS

1 Introduction: MATLAB

2 Root Finding

3 Solution of Linear and Non-linear Algebraic Equations

4 Curve Fitting and Numerical Interpolation

5 Numerical Differentiation and Integration

6 Solution of Differential Equations

7 Final Quiz

2

ME262 Numerical Analysis Sessional 4 December 2009

Reference Books

� Numerical Methods using MATLAB

- John H Mathews & Kurtis D Fink

� An Introduction to Programming and Numerical Methods in MATLAB

- S.R. Otto and J.P. Denier

3

ME262 Numerical Analysis Sessional 4 December 2009

Overview4

� Basic Matlab Operation

– Starting Matlab

– Using Matlab as a calculator

– Introduction to variables and functions

� Matrices and Vectors [All variables are matrices]

– Creating matrices and vectors

– Subscript notation

– Colon notation

ME262 Numerical Analysis Sessional 4 December 2009

Overview5

� Working with Matrices and Vectors

– Some basic linear algebra

– Vectorized operations

– Array operators

� Graphics

– Plotting

ME262 Numerical Analysis Sessional 4 December 2009

Overview6

� Scripts and Functions m-files

– Creating m-files

– Syntax of I/O parameters

– Primary and secondary functions

� Flow control

– Relational operators

– Conditional execution of blocks

– Loops

ME262 Numerical Analysis Sessional 4 December 2009

MATLAB Desktop7

ME262 Numerical Analysis Sessional 4 December 2009

MATLAB as a Calculator8

ME262 Numerical Analysis Sessional 4 December 2009

MATLAB as a Calculator9

ME262 Numerical Analysis Sessional 4 December 2009

Built-in Variables10

ME262 Numerical Analysis Sessional 4 December 2009

Built-in Functions11

ME262 Numerical Analysis Sessional 4 December 2009

Suppress output with ‘;’12

ME262 Numerical Analysis Sessional 4 December 2009

Suppress output with ‘;’13

ME262 Numerical Analysis Sessional 4 December 2009

Multiple Statements per line14

ME262 Numerical Analysis Sessional 4 December 2009

MATLAB Variables Names15

ME262 Numerical Analysis Sessional 4 December 2009

Built-in MATLAB Variables16

ME262 Numerical Analysis Sessional 4 December 2009

Matrices and Vectors17

ME262 Numerical Analysis Sessional 4 December 2009

Creating MATLAB Variables

4 December 2009ME262 Numerical Analysis Sessional

18

Creating Matrices & Vectors

4 December 2009ME262 Numerical Analysis Sessional

19

Matrix

Column vector

Row vector

Creating Matrices & Vectors

4 December 2009ME262 Numerical Analysis Sessional

20

[it will create a row vector of 100 element]

Transpose Operator

4 December 2009ME262 Numerical Analysis Sessional

21

Example:

4 December 2009ME262 Numerical Analysis Sessional

22

Colon Notation

4 December 2009ME262 Numerical Analysis Sessional

23

Colon Notation

4 December 2009ME262 Numerical Analysis Sessional

24

[J:K is the same as (J, J+1, ..., K)]

Try this25

� Create the following column vector without using manual input

5

4t

3

2

=

ME262 Numerical Analysis Sessional 4 December 2009

Functions to Create Matrices

4 December 2009ME262 Numerical Analysis Sessional

26

27

ME262 Numerical Analysis Sessional 4 December 2009

Functions to Create Matrices

28

ME262 Numerical Analysis Sessional 4 December 2009

Functions to Create Matrices

29

ME262 Numerical Analysis Sessional 4 December 2009

Subscript Notation

30

ME262 Numerical Analysis Sessional 4 December 2009

Vector Addition & Subtraction

31

ME262 Numerical Analysis Sessional 4 December 2009

Array Operators

32

ME262 Numerical Analysis Sessional 4 December 2009

Using Array Operators

33

ME262 Numerical Analysis Sessional 4 December 2009

The MATLAB Workspace

34

ME262 Numerical Analysis Sessional 4 December 2009

The MATLAB Workspace

35

ME262 Numerical Analysis Sessional 4 December 2009

Plotting [x,y] Data

36

ME262 Numerical Analysis Sessional 4 December 2009

Plotting [x,y] Data

Line & Symbol Types

4 December 2009ME262 Numerical Analysis Sessional

37

38

ME262 Numerical Analysis Sessional 4 December 2009

Plot Annotation

Example:

a = [0 1 2 3 4 5]; b = a + 2;

plot(a,b,'ro-');

xlabel('a');

ylabel('b');

title('b versus a');

legend('b'); grid;

39

ME262 Numerical Analysis Sessional 4 December 2009

Script Files

4 December 2009

40

ME262 Numerical Analysis Sessional

Script to plot tan(ϴ)

4 December 2009

41

ME262 Numerical Analysis Sessional

4 December 2009

42

ME262 Numerical Analysis Sessional

Script to plot tan(ϴ)

4 December 2009

43

ME262 Numerical Analysis Sessional

Function .m Files

4 December 2009

44

ME262 Numerical Analysis Sessional

Function .m Files

4 December 2009

45

ME262 Numerical Analysis Sessional

Function .m Files

4 December 2009

46

ME262 Numerical Analysis Sessional

Function Input & Output

4 December 2009

47

ME262 Numerical Analysis Sessional

Examples:

4 December 2009

48

ME262 Numerical Analysis Sessional

Examples:

4 December 2009

49

ME262 Numerical Analysis Sessional

Examples:

4 December 2009

50

ME262 Numerical Analysis Sessional

Text Input & Output

4 December 2009

51

ME262 Numerical Analysis Sessional

Text Output with disp

Take values of three different variables as input and

display their summation

4 December 2009

52

ME262 Numerical Analysis Sessional

Prompting for User Input

function [x, y, z] = newfunc (a, b)

x = a + b; y = a – b; z = a * b;

4 December 2009

53

ME262 Numerical Analysis Sessional

Function .m File

Do this

� Find f(x) of an real number x which is taken as input

function value should be calculated in a separate .m file

f(x)=x2-6x+15

function s=f(x)s=x^2-6*x+15;

f.m command window

>> f (input('Enter value x = '))Enter value x = 510

4 December 2009

54

ME262 Numerical Analysis Sessional

function [x, y, z] = newfunc (a, b)

x = a + b; y = a – b; z = a * b;

4 December 2009

55

ME262 Numerical Analysis Sessional

Flow Control

4 December 2009

56

ME262 Numerical Analysis Sessional

Relational Operators

4 December 2009

57

ME262 Numerical Analysis Sessional

Relational Operators

4 December 2009

58

ME262 Numerical Analysis Sessional

Logical Operators

4 December 2009

59

ME262 Numerical Analysis Sessional

Conditional Execution

4 December 2009

60

ME262 Numerical Analysis Sessional

if Constructs

4 December 2009

61

ME262 Numerical Analysis Sessional

if .. else

4 December 2009

62

ME262 Numerical Analysis Sessional

if .. elseif

4 December 2009

63

ME262 Numerical Analysis Sessional

Flow Control

4 December 2009

64

ME262 Numerical Analysis Sessional

for Loops

4 December 2009

65

ME262 Numerical Analysis Sessional

for Loop Variations

Do this

� Find Summation of an odd number series starting from 1 to n (odd number) which is taken by an input function.

function odd(n)sum = 0;for i = 1 : 2: n

sum = sum + i;enddisp(sum);

factorial.m command window

>> odd(input('Enter value n = '))Enter value n = 925

>> a=odd (input('Enter value n = '))Enter value n = 5??? Error using ==> factorialToo many output arguments.

>> odd(9)25

4 December 2009

66

ME262 Numerical Analysis Sessional

Do this

� Find Factorial of an real number n which is taken by an input function.

function sum = odd(n)sum = 0;for i = 1 : 2: n

sum = sum + i;enddisp(sum);

factorial.m command window

>> a=odd(input('Enter value n = '))Enter value n = 925

a =25

4 December 2009

67

ME262 Numerical Analysis Sessional

4 December 2009

68

ME262 Numerical Analysis Sessional

while Loops

The break Command

4 December 2009ME262 Numerical Analysis Sessional

69

The return Command

4 December 2009ME262 Numerical Analysis Sessional

70

4 December 2009

71

ME262 Numerical Analysis Sessional

Comparison of break & return