egr 115 introduction to computing for engineers introduction to matlab friday 29 august 2014 egr 115...
TRANSCRIPT
EGR 115 Introduction to Computing for Engineers
Introduction to MATLAB
Friday 29 August 2014 EGR 115 Introduction to Computing for Engineers
Lecture Outline
Friday 29 August 2014 EGR 115 Introduction to Computing for Engineers
• Introduction to MATLAB The MATLAB Environment Using MATLAB as a Calculator
Slide 2 of 13
Introduction to MATLABThe MATLAB Environment
Friday 29 August 2014 EGR 115 Introduction to Computing for Engineers
Workspace:• Currently defined
variables
Command Window:• Main interface
Command History:• Prior commands
Current Directory:• A list of files & folders
Help Browser:• Help on a variety of
topics
Slide 3 of 13
Introduction to MATLABThe MATLAB Environment
Friday 29 August 2014 EGR 115 Introduction to Computing for Engineers
• Compute the area of a circle: is a predefined variable “pi”
Assignment with semicolon suppressed output!!
Assignment without semicolon output not supressed!!
Command History:• Prior commands
Slide 4 of 13
Introduction to MATLABThe MATLAB Environment – Saving to *.m files
Friday 29 August 2014 EGR 115 Introduction to Computing for Engineers
• Saving commands to a file for reuse – “.m” files Select “New Script” ( )
Run Icon:• Select to run code
- Be sure to save files onto the “p:” drive.- Organization is very important!!!
Slide 5 of 13
Introduction to MATLABThe MATLAB Environment – How to get “help”
Friday 29 August 2014 EGR 115 Introduction to Computing for Engineers
• If you know what you are looking for>> help sqrt
• If you not exactly sure what you are looking for>> lookfor sqr
• Can use the “Tab” key to autocomplete
>> sqr
• Use the Help radio button (or >> doc sqrt)Slide 6 of 13
Introduction to MATLABThe MATLAB Env – Some important commands
Friday 29 August 2014 EGR 115 Introduction to Computing for Engineers
• Stop the currently executing task>> Ctrl + C
• To clear the command window>> clc
• Remove variables from workspace>> clear all
Slide 7 of 13
Introduction to MATLABUsing MATLAB as a Calculator
Friday 29 August 2014 EGR 115 Introduction to Computing for Engineers
• Some basic operations +, -, *, /, (add, subtract, multiply, devide) and ^ exponential (i.e., 2^3 is ) Use parentheses liberally (e.g., 2 * 3 + 4 vs 2 * (3 + 4)) Use % to precede any comments (comment profusely)
• Precedence of operatorsPrecedence OperationFirst Parentheses (innermost first)Second Exponentiation (from left to right)Third Multiplication and division (equal precedence, from left to right)Fourth Addition and subtraction (equal precedence, from left to right.)
Slide 8 of 13
Introduction to MATLABUsing MATLAB as a Calculator
Friday 29 August 2014 EGR 115 Introduction to Computing for Engineers
• Which of the following is the correct result?
MATLAB Expression Result #1 Result #2
>> 8 + 3 * 5 23 55
>> (8 + 3) * 5 23 55
>> 8 / 4 * 2 1 4
>> 8 / (4 * 2) 1 4
>> 3 * 4 ^ 2 + 5 53 149
>> 27 ^ 1 / 3 9 3
>> 27 ^ (1 / 3) 9 3
Slide 9 of 13
Introduction to MATLABValid Variable Names
Friday 29 August 2014 EGR 115 Introduction to Computing for Engineers
• Valid Names for MATLAB Variables MUST begin with a letter (a-z or A-Z). Followed by any combination of letters, numbers (0-9), and
the underscore (_) characters. o CHARACTERS such as &,^,%,@,… are not allowed
MATLAB keywords cannot be variables (e.g., for, if, end,…)o For a complete list, run the “iskeyword” command.o Valid variable names: “Area_51” , “x0”, o Invalid variable names: “_Area_51”, “0x”, “end”, “n!”,
MATLAB variables are case-sensitiveo Cost, cost and coSt are all different variable names
Use meaningful variable names!!Slide 10 of 13
Introduction to MATLABUsing MATLAB as a Calculator
Friday 29 August 2014 EGR 115 Introduction to Computing for Engineers
• In-Class Problem: From our physics classes, we know that the displacement ()
of an object, at time t, can be described as:
Given: = 10 m, = 15 m/s, and = -9.8 m/sec2, calculate the displacement at time = 5 sec.
20 0 0
1
2x x v t a t
Slide 11 of 13
Introduction to MATLABUsing MATLAB as a Calculator - Graphing Calculator
Friday 29 August 2014 EGR 115 Introduction to Computing for Engineers
• Simple Example: Plot s vs Generate an array of the discrete values of the independent
variable (i.e., )o Say, goes from 0 to 10 seconds in steps of 0.1 seconds>> t = 0:0.1:10;>> plot(t, sin(t));
0 1 2 3 4 5 6 7 8 9 10-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Slide 12 of 13
Next Lecture
Friday 29 August 2014 EGR 115 Introduction to Computing for Engineers
• MATLAB basics Variables and arrays
Slide 13 of 13