Computer Programming: Introduction To MATLAB
Computer Programming: Introduction To MATLAB
Programming
Lecture 7
Introduction to MATLAB®
An Overview of MATLAB®
The Default MATLAB Desktop
1-1
The Default MATLAB Desktop
1-1
The Default MATLAB Desktop
1-1
Entering Commands and Expressions
1-2
Scalar arithmetic operations
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
Order of precedence
1-5
Examples of Precedence
>> 8 + 3*5
ans =
23
>> 8 + (3*5)
ans =
23
>>(8 + 3)*5
ans =
55
>>4^2128/4*2
ans =
0
>>4^2128/(4*2)
ans =
3
1-6
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-7
Commands for managing the work session
1-8
Special variables and constants
1-9
Complex Number Operations, Pages 14-15
1-10
Numeric display formats. Table 1.1–5, Page 15
1-11
Arrays
>>u = 0:0.1:10;
>>w = 5*sin(u);
1-12
Array Index
>>u(7)
ans =
0.6000
>>w(7)
ans =
2.8232
>>m = length(w)
m=
101
1-13
Polynomial Roots
>>a = [1,-7,40,-34];
>>roots(a)
ans =
3.0000 + 5.000i
3.0000 - 5.000i
1.0000
1-14
Some commonly used mathematical functions
1-15
When you type problem1,
1-16
System, directory, and file commands
1-17
A graphics window showing a plot.
1-18
Some MATLAB plotting commands
1-19
Linear Algebraic Equations, Page 26
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-20
You can perform operations in MATLAB in two
ways:
1-21
COMMENTS
1-22
The MATLAB Command window with the Editor/Debugger
open. Figure 1.4–1, Page 28
1-23
Keep in mind when using script files:
1-26
Programming Style
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-27
Programming Style (continued)
3. Calculation section
1-28
Some Input/output commands
1-29
Example of a Script File
Problem:
1-30
Example of a Script File (continued)
% Program falling_speed.m:
% Plots speed of a falling object.
% Input Variable:
% tfinal = final time (in seconds)
%
% Output Variables:
% t = array of times at which speed is % computed (in
seconds)
% v = array of speeds (meters/second)
%
1-31
Example of a Script File (continued)
% Parameter Value:
g = 9.81; % Acceleration in SI units
%
% Input section:
tfinal = input(’Enter final time in seconds:’);
%
1-32
Example of a Script File (continued)
% Calculation section:
dt = tfinal/500;
% Create an array of 501 time values.
t = 0:dt:tfinal;
% Compute speed values.
v = g*t;
%
% Output section:
Plot(t,v),xlabel(’t (s)’),ylabel(’v m/s)’)
1-33
Getting Help From the Textbook
1-34
Getting Help From MATLAB:
The Function Browser after plot has been selected
1-35
The MATLAB Help Browser
1-36
The Help Navigator
1-37
MATLAB Help Functions,
1-38
Steps in engineering problem solving
1-39
Sketch of the dropped-package problem.
1-40
Steps for developing a computer solution
1-41
A piston, connecting rod, and crank for an internal
1-42
Questions ?