Presentation is loading. Please wait.

Presentation is loading. Please wait.

ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.

Similar presentations


Presentation on theme: "ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming."— Presentation transcript:

1 ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.  Use it as a calculator.  Define variables and use them in calculations.  Use built-in functions.  Plot graphs.  Write and run computer programs.  Perform symbolic mathematics. Introduction to MATLAB Starting with MATLAB / Chapter 1

2 ENG 1181 STUDENTS PLEASE START MATLAB NOW Click on the shortcut icon  or select start / All programs / MATLAB The following prompt should appear in the command window after a fairly long initialization process: >>

3 ENG 1181 MATLAB Display (Default Setting) Command Window (where commands are entered) Command History (what has been typed) Workspace (variable values) Command prompt Current Directory (lists files, etc.) Ribbon (useful operations)

4 ENG 1181 MATLAB’s Working Directory When you first start MATLAB it is a good practice to change the working directory to your Z: drive or USB device. You can use the Browse Icon

5 ENG 1181 ORDER OF PRECEDENCE ( VERY Important to understand!) Higher-precedence operations are executed before lower-precedence operations (like Excel or programmable calculators). If two operations have the same precedence, then the expression is executed from left to right. PRECEDENCEOPERATION FirstParentheses (innermost pair first) SecondExponentiation. ThirdMultiplication and division FourthAddition and subtraction

6 ENG 1181 Topic : Precedence of Operations >> 7 + 8 / 2 ans = 11 >> (7+8)/2 ans = 7.500 Question : Find the average of 2 numbers : 7 and 8 Not what we wanted !!!

7 ENG 1181 Topic : Precedence of Operations >> 27 ^ 1/3 + 32 ^ 0.2 ans = 11 Press up arrow key and add parenthesis around the 1/3 Question : Add the cube root of 27 to the 5 th root of 32 Not what we wanted !!! >> 27 ^ (1/3) + 32 ^ 0.2 ans = 5

8 ENG 1181 Topic : Important points about variable definition >> x = 5 x = 5 >> x = x + 5 x = 10 This statement does not make any mathematical sense but in programming language it changes the value of ‘x’ to a new value

9 ENG 1181 Topic : Calculations with variables >> a + my_var ans = 20 We can use the variables defined previously to do calculations >> a * my_var ^ 2 ans = 1152 Remember a=8 and my_var =12

10 ENG 1181 RULES ABOUT VARIABLES NAMES  Can be up to 63 characters long.  Must begin with a letter.  Can contain letters, digits, and the underscore character (no spaces).  MATLAB is case sensitive; it distinguishes between UPPERCASE and lowercase letters.  Avoid naming variables names that are used by MATLAB for example, exp, sin, cos, sqrt, length, mean, max, min etc.

11 ENG 1181 EXAMPLES OF VARIABLES NAMES Correct  My_name  MyName Incorrect  1Name ( starts with numeral)  My Name ( no spaces allowed)  My-Name ( no special characters allowed)

12 ENG 1181 MATLAB BUILT-IN MATH FUNCTIONS: Examples exp(x)exponential (e x ) log(x) natural logarithm (log base “e”, written ln in math expressions) log10(x) base 10 logarithm (log in math expressions) sqrt(x) square root abs(x) absolute value NOTE: For trigonometric functions, x is in radians sin(x), cos(x), asin(x), acos(x), tan(x), cot(x) For each trig function there is an equivalent, e.g. sind(x) where x is in degrees

13 ENG 1181 Topic : Built in Math functions >> sin(pi/2) ans = 1 If you want to calculate sin(π / 2) >> exp(2) If you want to calculate e 2 ans = 7.3891 π is already defined inside MATLAB as ‘pi’ >> e ^ 2 ??? Undefined function or variable 'e'.

14 ENG 1181 Topic : Built in Math functions >> x = pi/4 x = 0.7854 If you want to calculate sin2x where x = π /4 >> sin(2*x) ans = 1

15 ENG 1181 Topic : Built in Math functions >> x = 30 x = 30 Given a function : (cos2xsin 2 x + tan(x/2)) / e 3x Find the value for x = 30 degrees >> x = x* pi / 180 x = 0.5236 Convert x in radians >> (cos(2*x)* sin(x)^2 + tan(x/2)) / exp(3*x) ans = 0.0817

16 ENG 1181 SAVING A SCRIPT FILE  Once the script file is completed, it must be saved. In our class use Save As to your Z:\ or USB drive. This should be the same as the working directory you specified when you started MATLAB.  The name of the script file follows some rules Valid File Names Invalid File Names Prob1a.m Prob 1a.m (Note a blank) Prob_1a.m 1aProb.m (Cannot start with numeric) Prob-1a.m (No special characters)

17 ENG 1181 Some useful programming commands % It clears the command window >> clc To clear the command window To clear ALL variables previously defined from the memory leave the command window untouched! First we define x=5 using the following command >> x = 5 x = 5 >> clear >> x ??? Undefined function or variable ‘x’ % It clears only the variables

18 ENG 1181 Disp function Brutus Buckeye To send a text message to the command window You can also display contents of variables with this command >> x = 5 x = 5 >> disp(x) x = 5 >> disp(‘Brutus Buckeye’) Note: single quotes Note: NO single quotes

19 ENG 1181 Suppressing command window output >> x = 5 x = 5 >> x=6; The value of x is changed but not displayed >> x x = 6

20 ENG 1181 Example with Script file Pythagoras Theorem: Height Base Hypotenuse Hypotenuse 2 = Height 2 + Base 2 Create a script file and name it Pyth.m Clear the command window Clear all variables from memory Display your name and seat number Declare height of triangle as 3 units Declare base of triangle as 4 units Find the hypotenuse of the triangle

21 ENG 1181 Print of the Script File Print of the Output

22 ENG 1181 ASSIGNMENT : MAT01 – Introduction HW

23 ENG 1181 Homework Example clc disp ('Student, Joe') disp ('EG1181, Seat 99') disp ('MAT - Introduction') disp ('Problem 4') prob4a = cos(5*pi … … Script File Student, Joe EG1181, Seat 99 MAT - Introduction Problem 4 prob4a = 0.2846 … Command Window Submit a printout of both the Script File and the Command Window

24 ENG 1181 Important examples  Arithmetic Operations with scalars : 5+3, 5*3, 5^3  Precedence of operations : 7+8/2, (7+8)/2, 27^ 1/3 + 32^ 0.2, 27^ (1/3) + 32^ 0.2  Variable assignment : a=8, b=12  Calculations with variables : a+b, a*b  Important commands : clc, clear  Use of ; after a statement to suppress the output  Use of disp : disp(‘Brutus Buckeye’)  Mathematical functions : sin(pi/2), exp(2)  Calculate the value of (cos2xsinx + tan(x/2)) / exp(3x) for x = 30 degrees


Download ppt "ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming."

Similar presentations


Ads by Google