28
1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept. http://www.bmabdelaty.faculty.zu.edu.eg/ Default.aspx

1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

Embed Size (px)

Citation preview

Page 1: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

1

Lecture 1

Post-Graduate Students

Advanced Programming (Introduction to

MATLAB)Code: ENG 505

Dr. Basheer M. Nasef

Computers & Systems

Dept.http://www.bmabdelaty.faculty.zu.edu.eg/Default.aspx

Page 2: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

2

Evaluation

100 Total

70 Written

30 Term Work

26 – 11 - 2015Group Name

Page 3: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

3

Books Main textbook: MATLAB® An Introduction with Applications, Fourth Edition, Amos Gilat.

An Engineer’s Guide to MATLAB With Applications from Mechanical, Aerospace, Electrical, Civil, and Biological Systems Engineering, Third Edition

INTRODUCTION TO MATLAB® & SIMULINK 3rd Edition, O. BEUCHER and M. WEEKS

Additional readings:

Matlab help

Any useful tutorial, article or ppt on the web

Page 4: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

4

Course Outline

1. Starting with Matlab.

2. Arrays and Matrices.

3. Mathematical operations on arrays.

4. M-files and data I/O.

5. 2-D plots.

6. Programming in Matlab.

7. User defined functions.

8. Polynomials and curve fitting.

Page 5: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

5

Course Outline

9. Symbolic math.: integration and differentiation.

10. Simulink.

11. Control Systems.

12.Fluid Mechanics. Or Heat Transfer.

Page 6: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

6

Agenda

Starting with Matlab:

1)Matlab Windows.

2)Arithmetic Operations.

3)Scalar Variables.

4)Introduction to m-files.

5)Examples.

Page 7: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

7

Matlab Windows

Start Matlab from your programs list to have:

Page 8: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

8

Matlab Windows

Page 9: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

9

Command Window Typing a command, using up/down arrows.

Semicolon. ;

Comments using %

clc command.

Command history window.

Page 10: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

10

Arithmetic Operations

Page 11: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

11

Order of precedence:

Use MATLAB as a Calculator !

Arithmetic Operations

Page 12: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

12

Arithmetic Operations

Display Format:

Page 13: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

13

Math Built-in Functions

Page 14: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

14

Math Built-in Functions

Page 15: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

15

Math Built-in Functions

Page 16: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

16

Scalar Variables

Using the assignment operator =

There are rules in naming variables.

Variable_name = A numerical value, or a computable expression

Page 17: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

17

Scalar Variables

Page 18: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

18

Rules About Variable Names

A variable can be named according to the following rules:

1. Must begin with a letter.2. Can be up to 63 characters long.3. Can contain letters, digits, and the underscore

character.4. Cannot contain punctuation characters (e.g

period, comma, semicolon).

Page 19: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

19

Rules About Variable Names

5. MATLAB is case sensitive: it distinguishes between uppercase and lowercase letters. For example, AA, Aa, aA, and aa are the names of four different variables.

6. No spaces are allowed between characters (use the underscore where a space is desired).

7. Avoid using the name of a built-in function for a variable (i.e., avoid using cos, sin, exp, sqrt, etc.). Once a function name is used to define a variable, the function cannot be used.

Page 20: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

20

Predefined Variables and Keywords

Used in programming:

Others:

ans, pi, i, j, inf, Nan

Page 21: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

21

M-files

A different (better) way of executing commands

with MATLAB is first to create a file with a list of

commands (program), save it, and then run

(execute) the file.

M-file is also called script file.

Using a script file is convenient because it can be

edited (corrected or otherwise changed) and

executed many times.

Page 22: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

22

M-files

Page 23: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

23

M-files

Page 24: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

24

It is better to run your m-file from the current folder

M-files

Page 25: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

25

Example 1

Verify: at

Solution:

Page 26: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

26

Example 2

Page 27: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

27

Example 2

Solution:

Try m-file !

Page 28: 1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

28

Material & problems

We have covered Chapter 1 from Gilat.

Problems you can try:

1--10, 27,39,40