Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming for Mechanical Engineers (ME 319) - Lecture 1 Instructor: Mr. Srikanth Madala.

Similar presentations


Presentation on theme: "Introduction to Programming for Mechanical Engineers (ME 319) - Lecture 1 Instructor: Mr. Srikanth Madala."— Presentation transcript:

1 Introduction to Programming for Mechanical Engineers (ME 319) - Lecture 1
Instructor: Mr. Srikanth Madala

2 Introduction Ph.D. Student in Mechanical Engineering, Center for Energy Research (CER), UNLV What is programming? Why? What is a programming language? Programming Experience? Variables, Datatypes, Loops etc. What is MATLAB? Source Program (written in human language) – may be open or closed Object Program (written in machine language – 0s and 1s) (.exe files) Language compiler Programming Languages: C, C++, C#, Python, Java etc Fun Fact: There is a programming language associated with almost every alphabet, viz. C,D,E,F and so on! A set of instructions/commands to a computer is called a program.

3 MATLAB – Matrix Laboratory, fourth generation programming language.
Developed by ‘MathWorks’. Available for Windows as well as Mac. Applications: Engineering, Sciences, Applied Mathematics and Economics Deals a lot with Matrices! Matrices from linear algebra, remember? Signal processing and numerical analysis etc additional tool boxes! Same logical, relational, conditional, and loop structures as C, Fortran, BASIC, or Pascal.

4

5 Horizontal Rows (Represented by ‘i’) Vertical Columns (Represented by ‘j’)

6 Computer Program Structure of a good program:
Declaration (Define new variables with their data types) Note: MATLAB usually doesn’t need declaration except in a few cases. Expression and statements Sub-programs Logical execution order

7 Embedded Programs Computer programs are embedded into hardware.
Used when the program is not changed often. The program is expected not to be lost in case of interruption of power supply.

8 MATLAB Environment – Command Window
All the MATLAB files should be in the current directory, which is by default the MATLAB folder on the hard disk drive that has MATLAB software installed. The command history is shown on the lower left corner in the picture. Shows all the commands used since the start up of the MATLAB application. Command window is the space where MATLAB commands are entered and executed.

9 Workspace and Current Folder
Also take a look at the “Command History”

10 Working on Command Window
Here X and Y are “double” type variables. We assigned the values 3 and -4 respectively. The dimensions of X and Y matrices are 1x1.

11 Figure Window

12 Editor Window as a separate text editor file known as m-file
Editor Window as a separate text editor file known as m-file. The file will have an extension ‘.m’ i.e. the filename will be ‘Untitled.m’ until it is renamed with a suitable filename.

13 Help Window Very advantageous and comprehensive feature of MATLAB is the help window.

14

15 Format workspace options
MATLAB Function Description Example format short Fixed point, 4 decimal digits 22/7 ans = 3.1429 format long Fixed point, 14 decimal digits format short e Scientific notation with 4 decimal digits 3.1429e+000 format long e Scientific notation with 14 decimal digits e+000 format short g Best of 5 digits fixed or floating format long g Best of 15 digits fixed or floating format bank Two decimal digits 3.14 format compact Eliminates empty lines from appearing in the workspace to allow for more information format loose Adds empty line between the command and the answer (opposite to compact)

16 Mathematical Expressions
Description MATLAB Function Example √𝑥 sqrt (x) sqrt (4) ans = 2 nthroot (x,n) nthroot (81,4) 3 x^2 or power (x,2) power(5,2) 25 ex exp (x) exp(2) 7.3891 log 10 x log10 (x) log10(1000) ln x log (x) log(0.1353) abs (x) abs(-3) factorial (x) factorial(4) 24

17 Mathematical Expressions
MATLAB Function Description Example sin (x) sind (x) Sine of angle x (rad) Sine of angle x (degree) sin (pi/2) ans = 1.0000 sind (30) 0.5000 cos (x) cosd (x) Cosine of angle x (rad) Cosine of angle x (degree) cos (pi/2) cosd (60) tan (x) tand (x) Tangent of angle x (rad) Tangent of angle x (degree) tan (pi/4) tand (45) round (x) Round to the nearest integer round (3.5) 4 fix (x) Round towards zero fix(-0.4) ceil (x) Round towards inf ceil (3.1) floor (x) Round towards -inf floor(-0.4) -1 rem (x,y) Returns remainder of x/y rem (16,5) 1 sign (x) Signum of x, returns 1,-1,0 if x is bigger than, less than, or equal to zero sign (7) sign (-7) sign (0)

18 Assignment Operator Variable name = Numerical Value; E.g. boys = 15;
Variable name = Computable Expression; E.g. girls = boys * 2; E.g. total = boys + girls; The command ‘whos’ or ‘who’ will display the variable in the workspace. Ho We are adding the value in the variable ‘girls’ to the value in the variable ‘boys’

19 Variable naming rules (important)
Must begin with a letter Maximum of 63 characters May contain letters, digits, and underscore. Cannot contain special characters (such as Case sensitive i.e. (AA, Aa, aA, A_A, A_a, A1, A_1 are different from each other). Avoid using built-in functions ( such as cos, floor, exp, ..etc). Use the underscore to separate between names in a variable name, i.e. student_count = 44.

20 Row Matrices and Column Matrices

21 Creating a Vector (Rows)
Row Vector In MATLAB Command Window year = [ ]; >> year = [ ] year = students = [ ]; >> students = [27800,28500,26400,27400,29300] students = NOTE: Use a ‘comma’ or a ‘space’ to separate between elements in the same row, or to create columns!

22 Creating a vector (Columns)
Column Vector In MATLAB Command Window >> year = [2005;2006;2007;2008] year = 2005 2006 2007 2008 >> number_employee = [121 133 157 118] number_employee = 121 118 NOTE: Use a ‘semicolon’ or ‘enter’ to separate between elements in the same column, or to create rows!!

23 Thank you for the attention!
Any Questions or Suggestions please…


Download ppt "Introduction to Programming for Mechanical Engineers (ME 319) - Lecture 1 Instructor: Mr. Srikanth Madala."

Similar presentations


Ads by Google