Upload
domani
View
40
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Introduction to MATLAB. Alternative MATLAB Introduction Lecture authored by Annie Abell. Introduction to MATLAB. MATLAB is a powerful program for numerical computations, plotting and programming . Use it as a calculator. Define variables and use them in calculations. - PowerPoint PPT Presentation
Citation preview
• ENG 1181
College of EngineeringEngineering Education Innovation Center
Introduction to MATLAB
Alternative MATLAB Introduction Lecture authored by Annie Abell
• ENG 1181
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
• ENG 1181
Click on the shortcut icon or select: start / All programs / MATLAB
There will be a fairly long initialization process, then following prompt should appear in the command window:
>>
Open MATLAB now:
• ENG 1181
Command Window (where commands are entered)
Command History (what has been typed)
Workspace (variable values)
Command prompt
Current Directory (lists file, etc.)
• ENG 1181
MATLAB’s Working DirectoryWhen you first start MATLAB it is a good practice to change the working directory to your Z: drive or USB device.
Keep all of your files in the same folder so you do not have to constantly change your directory
• ENG 1181
Order of PrecedenceHigher-precedence operations are executed before lower-precedence operations (like Excel or graphing 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
• ENG 1181
Precedence of Operations
>> 7 + 8 / 2 ans =
11
…not what we wanted
>> (7+8)/2 ans =
7.500
… correct!
Task : Find the average of 7 and 8
• ENG 1181
Task: Add the cube root of 27 to the 5th root of 32
>> 27 ^ 1/3 + 32 ^ 0.2 ans =
11 … not what we wanted
>> 27 ^ (1/3) + 32 ^ 0.2 ans =
5
Precedence of Operations
• ENG 1181
>> x = 5x =
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.
Variable Definition
• ENG 1181
We can use the variables defined previously to do calculations.Define the following: a=8, my_var =12
>> a + my_varans =
20
>> a * my_var^2ans =
1152
Calculations with Variables
• ENG 1181
• 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 with names that are used by MATLAB: exp, sin, cos, sqrt, length, mean, max, min…
Rules About Variable Names
• ENG 1181
Acceptable:My_nameMyNamemyname1
Unacceptable:1Name starts with numeralMy Name spaces not allowedMy-Name special characters not allowed
Examples of Variable Names
• ENG 1181
exp(x) exponential (ex)
log(x) natural logarithm (log base 'e')
log10(x) base 10 logarithm (log)
sqrt(x) square root
abs(x) absolute value
Built-In Math Functions: Examples
• ENG 1181
In MATLAB, trigonometric functions use radians. So for these examples, x is in radians:• sin(x), cos(x), asin(x), acos(x), tan(x),
cot(x)
MATLAB also has equivalent functions where x is in degrees:• sind(x), cosd(x), tand(x), etc
Built-In Math Functions
• ENG 1181
Task: calculate sin(∏ / 2). Note: ∏ is defined by MATLAB as pi.
>> sin(pi/2)
ans =1
Built-In Math Functions: Examples
• ENG 1181
Task: calculate e2
>> e ^ 2??? Undefined function or variable 'e'
How do we fix this?
>> exp(2)ans =
7.3891
Built-In Math Functions: Examples
• ENG 1181
Built-In Math Functions: Examples
Task: calculate sin2x, where x = ∏ /4
>> x = pi/4 x =
0.7854
>>sin(2*x) ans =
1
• ENG 1181
Task: Using (cos2xsin2x + tan(x/2)) / e3x , find the value for x = 30 degrees.
>> x = 30 x =
30
>> x = x * pi / 180 x =
0.5236
Built-In Math Functions: Examples
• ENG 1181
Task: Using (cos2x sin2x + tan(x/2)) / e3x , find the value for x = 30 degrees.
>> (cos(2*x)* sin(x)^2 + tan(x/2)) / exp(3*x) ans =
0.0817
Built-In Math Functions: Examples
• ENG 1181
• Once the script file is completed, it must be saved. In our class use Save As to your Z:\ or your own USB drive.
• This should be the same as the working directory you specified when you started MATLAB.
• ALWAYS SAVE YOUR WORK. Back it up in multiple places for safety! To the cloud!!
Saving a Script File
• ENG 1181
Valid File Names• Prob1a.m• Prob1a.m• problemone.m
Invalid File Names Prob-1a.m Special Characters not
allowedProb 1a.m Spaces not allowed1aProb.m Cannot start with number
Saving a Script File
• ENG 1181
To clear the command window: clc>> clc
To clear all variables previously defined: clear>> x = 5 x =
5>> clear>> x ??? Undefined function or variable 'x'
Useful Programming Commands
• ENG 1181
This function displays a message to the command window. It can be a string of text or variable contents.
To display your message as text, use single quotes around your message:
>> disp(‘Brutus Buckeye’) Brutus Buckeye
The Display Function: disp()
• ENG 1181
To display any variable contents, do not use quotes:
>> x = 5 x =
5>> disp(x) x =
5
The Display Function: disp()
• ENG 1181
Suppressing Output
This is good practice! You don't need all of your program displayed in the command window, just the relevant info.
>> x = 6 x =
6
>> y = 7; …what happens when you type this?
• ENG 1181
Pythagoras' Theorem: Hypotenuse2 = Height2 + Base2
1. Create a script file and name it Pyth.m
2. Clear the command window
3. Clear all variables from memory
4. Display your name and seat number
5. Declare height of triangle as 3 units
6. Declare base of triangle as 4 units
7. Find the hypotenuse of the triangle
Height
Base
Hypotenuse
Script File Example
• ENG 1181
How To Turn In All Assignments
You must print two things for every MATLAB assignment:
1. Script file 2. Command window output
… if you do not turn in both, you will lose points
• ENG 1181
Example Script File Printout
Example Command Window Printout
• ENG 1181
• Arithmetic Operations with scalars: 5+3, 5*3 , 5^3
• Precedence of operations: 7+8/2 vs. (7+8)/2 • Variable assignment: a=8, b=12• Calculations with variables: a+b, a*b• Important commands: clc, clear• Use of semi colons (;) to suppress output• Display function: disp(‘Brutus Buckeye’)• Mathematical functions: sin(pi/2) , exp(2) • Calculate the value of (cos2xsinx + tan(x/2)) /
exp(3x) for x = 30 degrees
Self Check… Do you understand these concepts?
• ENG 1181
Homework Assignment
MAT-01Introduction to Matlab