Introduction To MATLAB 7 For Engineers: An Overview of MATLAB
Introduction To MATLAB 7 For Engineers: An Overview of MATLAB
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.
The default MATLAB Desktop. Figure 1.1–1
1-3
An Example Session
>> 8/10
ans =
0.8000
>> 5*ans
ans =
4
>> r=8/10
r =
0.8000
>> r
r =
0.8000
>> s=20*r
s =
16
1-4
Scalar Arithmetic Operations Table 1.1–1
^ 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
1-5
Order of Precedence Table 1.1–2
Precedence Operation
1-6
Examples of Precedence
>> 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 …)
1-7
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 =
Command Description
clc 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 Stops MATLAB.
1-10
(continued …)
Commands for managing the work session
Table 1.1–3 (continued)
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
1-13
Numeric Display Formats Table 1.1–5
1-14
Arrays
>>u = [0:0.1:10];
>>w = 5*sin(u);
1-15
Array Index
>>u(7)
ans =
0.6000
>>w(7)
ans =
2.8232
>>m = length(w)
m =
101
>>a = [1,-7,40,-34];
>>roots(a)
ans =
3.0000 + 5.000i
3.0000 - 5.000i
1.0000
1-17
Some Commonly Used Mathematical Functions Table 1.3–1
Function MATLAB syntax1
ex exp(x)
√x sqrt(x)
ln x log(x)
log10 x log10(x)
cos x cos(x) 1The MATLAB
sin x sin(x) trigonometric functions
use radian measure.
tan x tan(x)
cos-1 x acos(x)
sin-1 x asin(x)
tan-1 x atan(x)
1-18
When you type problem1,
1-20
System, Directory, and File Commands Table 1.3–2
Command Description
addpath dirname Adds the directory
dirname to the search
path.
cd dirname Changes the current
directory to dirname.
dir Lists all files in the current
directory.
dir dirname Lists all the files in the
directory dirname.
path Displays the MATLAB
search path.
pathtool Starts the Set Path tool.
1-21 (continued …)
System, Directory, and File Commands Table 1.3–2
(continued)
Command Description
1-22
A graphics window showing a plot. Figure 1.3–1
1-23
Some MATLAB plotting commands Table 1.3–3
Command Description
[x,y] = ginput(n) Enables the mouse to get n points
from a plot, and returns the x and y
coordinates in the vectors x and y,
which have a length n.
(continued …)
1-24
Some MATLAB plotting commands Table 1.3–3
(continued)
6x + 12y + 4z = 70
7x – 2y + 3z = 5
2x + 8y – 9z = 64
>>A = [6,12,4;7,-2,3;2,8,-9];
>>B = [70;5;64];
>>Solution = A\B
Solution =
3
5
-2
1-26
More? See page 28.
You can perform operations in MATLAB in two
ways:
1-28
The MATLAB Command window with the Editor/Debugger
open. Figure 1.4–1
1-29
Keep in mind when using script files:
1-30
Debugging Script Files
1. Comments section
a. The name of the program and any key
words in the first line.
b. The date created, and the creators' names
in the second line.
c. The definitions of the variable names for
every input and output variable. Include
definitions of variables used in the calculations
and units of measurement for all input and all
output variables!
d. The name of every user-defined function
called by the program.
1-33 (continued …)
Programming Style (continued)
3. Calculation section
Problem:
(continued …)
1-36
Example of a Script File (continued)
% Program falling_speed.m:
% Plots speed of a falling object.
% Created on March 1, 2004 by W. Palm
%
% Input Variable:
% tf = final time (in seconds)
%
% Output Variables:
% t = array of times at which speed is
% computed (in seconds)
% v = array of speeds (meters/second)
%
(continued …)
1-37
Example of a Script File (continued)
% Parameter Value:
g = 9.81; % Acceleration in SI units
%
% Input section:
tf = input(’Enter final time in seconds:’);
%
(continued …)
1-38
Example of a Script File (continued)
% Calculation section:
dt = tf/500;
% Create an array of 501 time values.
t = [0:dt:tf];
% Compute speed values.
v = g*t;
%
% Output section:
Plot(t,v),xlabel(’t (s)’),ylabel(’v m/s)’)
1-39
More? See pages 37-38.
Getting Help
1-40
The Help Navigator contains four tabs:
1-41
The MATLAB Help Browser. Figure 1.5–1
1-42
Help Functions
Relational Meaning
operator
1-44
Examples of Relational Operators
>> x = [6,3,9]; y = [14,2,9];
>> z = (x < y)
z =
1 0 0
>>z = ( x > y)
z =
0 1 0
>>z = (x ~= y)
z =
1 1 0
>>z = ( x == y)
z =
0 0 1
>>z = (x > 8)
z =
1-45 0 0 1 More? See pages 44-45.
The find Function
1-46
Note the difference between the result obtained by
x(x<y) and the result obtained by find(x<y).
1-48
Suppose that we want to compute y such that
15√4x + 10 if x ≥ 9
y = 10x + 10 if 0 ≤ x < 9
10 if x < 0
1-50
A simple example of a for loop is
m = 0;
x(1) = 10;
for k = 2:3:11
m = m+1;
x(m+1) = x(m) + k^2;
end
k takes on the values 2, 5, 8, 11. The variable m
indicates the index of the array x. When the loop
is finished the array x will have the values
x(1)=14,x(2)=39,x(3)=103,x(4)=224.
1-51
A simple example of a while loop is
x = 5;k = 0;
while x < 25
k = k + 1;
y(k) = 3*x;
x = 2*x-1;
end
The loop variable x is initially assigned the value 5, and it
keeps this value until the statement x = 2*x - 1 is
encountered the first time. Its value then changes to 9.
Before each pass through the loop, x is checked to see if
its value is less than 25. If so, the pass is made. If not, the
loop is skipped.
1-52
Example of a for Loop
Write a script file to compute the sum of the first
15 terms in the series 5k2 – 2k, k = 1, 2, 3, …,
15.
total = 0;
for k = 1:15
total = 5*k^2 - 2*k + total;
end
disp(’The sum for 15 terms is:’)
disp(total)
The answer is 5960.
1-53
Example of a for Loop
total = 0;k = 0;
while total < 1e+4
k = k + 1;
total = 5*k^2 - 2*k + total;
end
disp(’The number of terms is:’)
disp(k)
disp(’The sum is:’)
disp(total)
1-56 (continued …)
Steps in engineering problem solving Table 1.7–1
(continued)
(continued …)
1-57
Steps in engineering problem solving Table 1.7–1
(continued)
1-60
The following slides contain figures from
the chapter and its homework problems.
1-61
Sketch of the dropped-package problem.
Figure 1.7–1
1-62
A piston, connecting rod, and crank for an internal combustion engine.
Figure 1.7–2
1-63
Plot of the piston motion versus crank angle.
Figure 1.7–3
1-64
Figure P28
1-65
Figure P29
1-66
Figure P30
1-67