introduction to matlab for mechanical engineersme313.cankaya.edu.tr/uploads/files/lecture 1...

43
ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat Onur Introduction to MATLAB for Mechanical Engineers Cankaya University Faculty of Engineering Mechanical Engineering Department

Upload: vanquynh

Post on 10-May-2018

238 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

ME 102 Mechanical Engineering Orientation

Class Notes

by

Prof.Dr.Nevzat Onur

Introduction to MATLAB

for Mechanical Engineers

Cankaya University

Faculty of Engineering

Mechanical Engineering Department

Page 2: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

1-2

Page 3: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

Entering Commands and Expressions

• MATLAB retains your previous keystrokes.

• Use the up-arrow key to scroll back through the

commands.

• Press the key once to see the previous entry, and

so on.

• Use the down-arrow key to scroll forward.

• Edit a line using the left- and right-arrow keys

the Backspace key, and the Delete key.

• Use the down-arrow key to scroll forward

• Press the Enter key to execute the command.

Page 4: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

Scalar arithmetic operations

Page 5: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat
Page 6: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

1-6

Page 7: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

1-7

Page 8: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

Order of precedence

Page 9: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

1-9

% designates comment .Matlab does not execute it

Page 10: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat
Page 11: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

keskin

Keskin

KESKIN

Keskin_1

Keskin_2

Page 12: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat
Page 13: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat
Page 14: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat
Page 15: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat
Page 16: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat
Page 17: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

>> r=8;

>> h=15;

>> V=(pi*r^2*h)/3

V =

1.0053e+03

Page 18: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

Numeric

display

formats.

Page 19: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

Some commonly used mathematical functions

Page 20: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

1-20

Page 21: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

21

exa

abs(z) The absolute value of z=x+iy angle(z) the angle of z. This is calculated by atan2(y,x)

Example

Given z=3-5*i

>> real(z)

ans =

3

>> imag(z)

ans =

-5

>> abs(z)

ans =

5.8310

>> angle(z)

ans =

-1.0304

Page 22: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat
Page 23: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat
Page 24: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat
Page 25: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

25

Page 26: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

>> a=30;

>> b=20;

>> A=sqrt(b^2+16*a^2)/2;

>> B=(b^2/( 8*a ))*log((4*a+sqrt(b^2+16*a^2))/b);

>> s=A+B

>>s =

64.9806

Page 27: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

COMMENTS

% -the comment symbol

The comment symbol may be put anywhere in the

line. MATLAB ignores everything to the right of the

% symbol. For example,

>>% This is a comment.

>>x = 2+3 % So is this.

x =

5

Note that the portion of the line before the % sign is

executed to compute x.

Page 28: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

Some Input/output commands

Page 29: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

disp can be used to display the value of a variable.

>> x=3.0;

>> y=3^x;

>> disp(y)

27

Example

>> disp(' And now for something completely different' )

And now for something completely different

Example

>> disp('----------------------------------')

----------------------------------

29

Page 30: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

You can perform operations in MATLAB in two ways:

1. In the interactive mode: all commands are entered

directly in the Command window,

2. In the script file mode: By running a MATLAB program

stored in script file. This type of file contains MATLAB

commands, so running it is equivalent to typing all the

commands—one at a time—at the Command window

prompt. You can run the file by typing its name at the

Command window prompt.

30

Page 31: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat
Page 32: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat
Page 33: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

Example:

Write a script file for the following

problem and save it as T5

Page 34: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat
Page 35: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

>> T5

Part a

Lp =

92.0412

Part b

m =

7.9057

Page 36: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat
Page 37: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat
Page 38: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

>> sphere

Enter radius=5

Surface area of sphere

314.1593

Page 39: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

Keep in mind when using script files:

1. The name of a script file must begin with a letter,

and may include digits and the underscore

character, up to 63 characters.

2. Do not give a script file the same name as a

variable.

3. Do not give a script file the same name as a

MATLAB command or function. You can check to

see if a command, function or file name already exists by using the exist command.

39

Page 40: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

40

Page 41: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

diary

Diary command saves your input to MATLAB and most

of the output to disk. This command toggles diary on and

off. (If no file is given, it is saved to the file diary in the

current directory.)

diary on turns the diary on.

diary off turns the diary off.

41

Page 42: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

1)

Write script file.

2)

Write a script file for this problem

Page 43: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat

Write a script file