24
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

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

Embed Size (px)

Citation preview

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

• ENG 1181

College of EngineeringEngineering 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

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

• 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: >>

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

• 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)

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

• ENG 1181

MATLAB’s Working DirectoryWhen you first start MATLAB it is a good practice to changethe working directory to your Z: drive or USB device.

You can use the Browse Icon

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

• 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.

PRECEDENCE OPERATION

First Parentheses (innermost pair first)

Second Exponentiation.

Third Multiplication and division

Fourth Addition and subtraction

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

• 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 !!!

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

• ENG 1181

Topic : Precedence of Operations

>> 27 ^ 1/3 + 32 ^ 0.2ans = 11

Press up arrow key and add parenthesis around the 1/3

Question : Add the cube root of 27 to the 5th root of 32

Not what we wanted !!!

>> 27 ^ (1/3) + 32 ^ 0.2ans = 5

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

• 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

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

• 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

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

• 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.

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

• 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)

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

• ENG 1181

MATLAB BUILT-IN MATH FUNCTIONS: Examples

exp(x) exponential (ex)

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 radianssin(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

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

• 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 e2

ans = 7.3891

π is already defined inside MATLAB as ‘pi’

>> e ^ 2??? Undefined function or variable 'e'.

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

• 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

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

• ENG 1181

Topic : Built in Math functions

>> x = 30

x = 30

Given a function : (cos2xsin2x + tan(x/2)) / e3x

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

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

• 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)

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

• 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

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

• 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

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

• ENG 1181

Suppressing command window output

>> x = 5 x = 5

>> x=6; • The value of x is changed but not displayed

>> x x = 6

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

• ENG 1181

Example with Script file

Pythagoras Theorem:

Height

Base

Hypotenuse

Hypotenuse2 = Height2 + Base2

• 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

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

• ENG 1181

Print of the Script File

Print of the Output

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

• ENG 1181

ASSIGNMENT :

MAT01 – Introduction HW

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

• ENG 1181

Homework Exampleclc

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

CommandWindow

Submit a printout of both the Script File and the Command Window

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

• ENG 1181

Important examples

Arithmetic Operations with scalars : 5+3, 5*3 , 5^3Precedence of operations : 7+8/2 , (7+8)/2 , 27^ 1/3 + 32^ 0.2 , 27^ (1/3) + 32^ 0.2Variable assignment : a=8, b=12Calculations with variables : a+b, a*bImportant commands : clc, clearUse of ; after a statement to suppress the outputUse 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