Ch01 Overview

advertisement
PowerPoint to accompany
Introduction to MATLAB 7
for Engineers
William J. Palm III
Chapter 1
An Overview Of MATLAB
Copyright © 2005. The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Matlab's capabilities



Hard to describe, best shown with user
stories:
http://www.mathworks.com/company/user_sto
ries/
basically takes the tedium out of doing
complex, specialized, math calculations
some programs like MathCad help you "do
math"

Matlab is more a "Problem solving" tool.
Simulation, Verify speculative possibilities
 Combined with programming language for extensibility

The default MATLAB Desktop. Figure 1.1–1
1-2
More? See pages 6-7.
Entering Commands and Expressions





1-3
MATLAB retains your previous keystrokes.
Use the up-arrow key to scroll back back
through the commands.
Press the key once to see the previous entry,
and so on.
Use the down-arrow key to scroll forward. Edit a
line using the left- and right-arrow keys the
Backspace key, and the Delete key.
Press the Enter key to execute the command.
An Example Session
1-4
>> 8/10
ans =
0.8000
>> 5*ans
ans =
4
>> r=8/10
r =
0.8000
>> r
r =
0.8000
>> s=20*r
s =
16
More? See pages 8-9.
Scalar Arithmetic Operations Table 1.1–1
Symbol
1-5
Operation
MATLAB form
^
exponentiation: ab
a^b
*
multiplication: ab
a*b
/
right division: a/b
a/b
\
left division: b/a
a\b
+
addition: a + b
a + b
-
subtraction: a - b
a - b
Order of Precedence Table 1.1–2
Precedence
1-6
Operation
First
Parentheses, evaluated starting with the
innermost pair.
Second
Exponentiation, evaluated from left to right.
Third
Multiplication and division with equal
precedence, evaluated from left to right.
Fourth
Addition and subtraction with equal precedence,
evaluated from left to right.
Examples of Precedence
1-7
>> 8 + 3*5
ans =
23
>> 8 + (3*5)
ans =
23
>>(8 + 3)*5
ans =
55
>>4^212 8/4*2
ans =
0
>>4^212 8/(4*2)
ans =
3
(continued …)
Examples of Precedence (continued)
>> 3*4^2 + 5
ans =
53
>>(3*4)^2 + 5
ans =
149
>>27^(1/3) + 32^(0.2)
ans =
5
>>27^(1/3) + 32^0.2
ans =
5
>>27^1/3 + 32^0.2
ans =
11
1-8
The Assignment Operator =





1-9
Typing x = 3 assigns the value 3 to the variable x.
We can then type x = x + 2. This assigns the value
3 + 2 = 5 to x. But in algebra this implies that 0 = 2.
In algebra we can write x + 2 = 20, but in MATLAB we
cannot.
In MATLAB the left side of the = operator must be a
single variable.
The right side must be a computable value.
More? See pages 11-12.
Commands for managing the work session Table 1.1–3
Command
clc
Description
Clears the Command window.
clear
Removes all variables from memory.
clear v1 v2 Removes the variables v1 and v2 from
memory.
exist(‘var’)Determines if a file or variable exists
having the name ‘var’.
quit
1-10
Stops MATLAB.
(continued …)
Commands for managing the work session
Table 1.1–3 (continued)
who
whos
;
1-11
Lists the variables currently in memory.
Lists the current variables and sizes,
and indicates if they have imaginary
parts.
Semicolon; suppresses screen printing;
also denotes a new row in an array.
More? See pages 12-15.
Special Variables and Constants Table 1.1–4
Command
Description
ans
Temporary variable containing the most recent
answer.
eps
Specifies the accuracy of floating point
precision.
i,j
The imaginary unit -1.
Inf
Infinity.
NaN
Indicates an undefined numerical result.
pi
The number p.
1-12
Complex Number Operations
• The number c1 = 1 – 2i is entered as follows:
c1 = 12i.
• An asterisk is not needed between i or j and
a number, although it is required with a
variable, such as c2 = 5 i*c1.
• Be careful. The expressions
y = 7/2*i
and
x = 7/2i
give two different results: y = (7/2)i = 3.5i
and x = 7/(2i) = –3.5i.
1-13
Numeric Display Formats Table 1.1–5
Command
format short
Description and Example
Four decimal digits (the
default); 13.6745.
format long
16 digits;
17.27484029463547.
format short e
Five digits (four decimals)
plus exponent;
6.3792e+03.
format long e
16 digits (15 decimals)
plus exponent;
6.379243784781294e–04.
1-14
Some Commonly Used Mathematical Functions Table 1.3–1
1-18
Function
MATLAB syntax1
ex
exp(x)
√x
sqrt(x)
ln x
log(x)
log10 x
log10(x)
cos x
cos(x)
sin x
sin(x)
tan x
tan(x)
cos-1 x
acos(x)
sin-1 x
asin(x)
tan-1 x
atan(x)
1The
MATLAB
trigonometric functions
use radian measure.
Arrays
• The numbers 0, 0.1, 0.2, …, 10 can be assigned to the
variable u by typing u = [0:0.1:10].
• To compute w = 5 sin u for u = 0, 0.1, 0.2, …, 10, the
session is;
>>u = [0:0.1:10];
>>w = 5*sin(u);
• The single line, w = 5*sin(u), computed the formula
w = 5 sin u 101 times.
1-15
Problem Solving





The point of learning Matlab is to help us
solve problems in Engineering,Science,Math
Matlab is just a tool—it won't think for us
We still need to apply the engineering
problem solving method
Much of our thought process needs to take
place "offline" i.e. using traditional sketches
and paper based mathematics
Computer tools require special care to make
sure the answers are correct
Steps in engineering problem solving Table 1.7–1
1.
2.
3.
4.
5.
6.
7.
1-56
Understand the purpose of the problem.
Collect the known information. Realize that some of it
might later be found unnecessary.
Determine what information you must find.
Simplify the problem only enough to obtain the
required information. State any assumptions you
make.
Draw a sketch and label any necessary variables.
Determine which fundamental principles are
applicable.
Think generally about your proposed solution
approach and consider other approaches before
proceeding with the details.
(continued …)
Steps in engineering problem solving Table 1.7–1
(continued)
8. Label each step in the solution process. Understand the
purpose of the problem
9. If you solve the problem with a program, hand check the
results using a simple version of the problem.
Checking the dimensions and units and printing the
results of intermediate steps in the calculation sequence
can uncover mistakes.
1-57
(continued …)
Steps in engineering problem solving Table 1.7–1
(continued)
10. Perform a “reality check” on your answer. Does it make
sense? Estimate the range of the expected result and
compare it with your answer. Do not state the answer
with greater precision than is justified by any of the
following:
(a) The precision of the given information.
(b) The simplifying assumptions.
(c) The requirements of the problem.
Interpret the mathematics. If the mathematics produces
multiple answers, do not discard some of them without
considering what they mean. The mathematics might be
trying to tell you something, and you might miss an
opportunity to discover more about the problem.
1-58
More? See pages 52-56.
Steps for developing a computer solution Table 1.7–2
1.
2.
3.
4.
5.
6.
7.
8.
1-59
State the problem concisely.
Specify the data to be used by the program. This is the
“input.”
Specify the information to be generated by the
program. This is the “output.”
Work through the solution steps by hand or with a
calculator; use a simpler set of data if necessary.
Write and run the program.
Check the output of the program with your hand
solution.
Run the program with your input data and perform a
reality check on the output.
If you will use the program as a general tool in the
future, test it by running it for a range of reasonable
data values; perform a reality check on the results.
More? See pages 56-60.
How to Solve an
Applied Trigonometry Problem?



Step 1
Draw a sketch, and label it with the
given information. Label the quantity to
be found with a variable.
Step 2
Use the sketch to write an equation
relating the given quantities to the
variable.
Step 3
Solve the equation, and check that
your answer makes sense.
Rev.S08
23
Laws of Trig you should Master
• Pythagorean theorem: a2+b2=c2 (right triangles)
• Sum of internal angles = 180 (all triangles)
• Definition of sin, cos, tan, arcsin, arccos, arctan
(right triangles)
• Law of Cosines (all triangles)
• Law of Sines (all triangles)
Defining sin, cos, tan
• also, a = c cos(q) , b = c sin(q)
q = arccos(a/ c) = arcsin(b/c) = arctan(b/a)
Example


The length of the shadow of a tree 22.02 m tall is
28.34 m. Find the angle of elevation of the sun.
Draw a sketch.
22.02 m
B
28.34 m

The angle of elevation of the sun is 37.85.
Rev.S08
27
http://faculty.valenciacc.edu/ashaw/
Click link to download other modules.
Example 2

The length of the shadow of a tree is 100 ft, and the
angle of elevation of the sun is 60 degrees. Find the
height of the tree:

100 = hyp*cos(60), therefore
hyp = 100/cos(60) = 200
height = hyp*sin(60)
= 200*.866 = 173 ft



Rev.S08
28
http://faculty.valenciacc.edu/ashaw/
Click link to download other modules.
Law of Cosines
• Use when you know an angle and two adacent
sides to find the 3rd side
• Use when you know three sides and want to
find an angle
Find x
Matlab:
x = sqrt(14^2+10^2-2*14*10*cosd(44))
= 9.725
(use cosd for degree angles)
Find x
paper:
252 = 372 + 322 – 2*37*32*cos(x)
cos(x) = (372 + 322 – 252 ) / (2*37*32)
Matlab:
x = acosd((372 + 322 –
252 ) / (2*37*32) )
Law of Sines
• Use when you know an angle and the
opposite side and want to find another angle
or side:
Find b
(paper)
sin(b)/16 = sin(115)/123
(matlab)
b = asind( 16*sind(115)/123)
= 6.77 deg
(use sind for degrees)
U Try: Find unknown side x
a
x
(paper)
(matlab)
Download