Tải bản đầy đủ (.ppt) (17 trang)

Lecture 3 Creating M - files docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (164.79 KB, 17 trang )

Lecture 3
Lecture 3
Creating
Creating
M-files
M-files
Programming tools:
Programming tools:
Input/output
Input/output
(assign/graph-&-display)
(assign/graph-&-display)
Repetition (for)
Repetition (for)
Decision (if)
Decision (if)
© 2007 Daniel Valentine. All rights reserved. Published by
Elsevier.
Review
Review

Arrays
Arrays

List of numbers in brackets
List of numbers in brackets

A comma or space separates numbers (columns)
A comma or space separates numbers (columns)

A semicolon separates row


A semicolon separates row

Zeros and ones Matrices:
Zeros and ones Matrices:

zeros()
zeros()

ones()
ones()

Indexing
Indexing

(row,column)
(row,column)

Colon Operator:
Colon Operator:

Range of Data first:last or first:increment:last
Range of Data first:last or first:increment:last

Manipulating Arrays & Matrices
Manipulating Arrays & Matrices

Transpose
Transpose
Input
Input


Examples of input to arrays:
Examples of input to arrays:

Single number array & scalar: 1 × 1
Single number array & scalar: 1 × 1
>>
>>
a = 2
a = 2

Row array & row vector:
Row array & row vector:
1 × n
1 × n
>>
>>
b = [1 3 2 5]
b = [1 3 2 5]

Column array & column vector: n x 1
Column array & column vector: n x 1
>>
>>
b = b’
b = b’
% This an application of the transpose.
% This an application of the transpose.

Array of n rows x m columns & Matrix: n × m

Array of n rows x m columns & Matrix: n × m
>>
>>
c = [1 2 3; 4 5 6; 7 6 9]
c = [1 2 3; 4 5 6; 7 6 9]
% This example is 3 x 3.
% This example is 3 x 3.
Basic elements of a program
Basic elements of a program

Input
Input

Initialize, define or assign numerical values to variables.
Initialize, define or assign numerical values to variables.

Set of command expressions
Set of command expressions

Operations applied to input variables that lead to the desired
Operations applied to input variables that lead to the desired
result.
result.

Output
Output

Display (graphically or numerically) result.
Display (graphically or numerically) result.
An example of technical computing

An example of technical computing

Let us consider using the hyperbolic tangent to model a
Let us consider using the hyperbolic tangent to model a
down-hill section of a snowboard or snow ski facility.
down-hill section of a snowboard or snow ski facility.

Let us first examine the hyperbolic tangent function by
Let us first examine the hyperbolic tangent function by
executing the command:
executing the command:
>>
>>
ezplot(
ezplot(
‘tanh(x)’
‘tanh(x)’
)
)
We get the following graph:
We get the following graph:
Ezplot(‘tanh(x)’)
Ezplot(‘tanh(x)’)
Hyperbolic tangent: tanh(X)
Hyperbolic tangent: tanh(X)

Properties (as illustrated in the figure):
Properties (as illustrated in the figure):

As X approaches –Inf, tanh(X) approaches -1.

As X approaches –Inf, tanh(X) approaches -1.

As X approaches Inf, tanh(X) approaches +1.
As X approaches Inf, tanh(X) approaches +1.



X = 0, tanh(X) = 0.
X = 0, tanh(X) = 0.

Transition from -1 to 1 is smooth and most rapid (roughly
Transition from -1 to 1 is smooth and most rapid (roughly
speaking) in the range
speaking) in the range


-2<X<2.
-2<X<2.

REMARK:
REMARK:
With this familiarity with tanh(X), let us apply it to
With this familiarity with tanh(X), let us apply it to
solve the following problem.
solve the following problem.
Problem background
Problem background

Let us consider the design of a slope that is to be
Let us consider the design of a slope that is to be

modeled by tanh(X). Consider the range -3<X<3, and
modeled by tanh(X). Consider the range -3<X<3, and
assume the slope changes negligibly for |X|>3.
assume the slope changes negligibly for |X|>3.

Thus, for –3 < X < 3, –1 < tanh(X) < 1.
Thus, for –3 < X < 3, –1 < tanh(X) < 1.

We want to design a downhill slope such that in 10
We want to design a downhill slope such that in 10
meters the hill drops 2 meters.
meters the hill drops 2 meters.

Thus, as shown next, the following formula is needed: y
Thus, as shown next, the following formula is needed: y
= 1 – tanh(3*X/5).
= 1 – tanh(3*X/5).
Formula for snow hill shape
Formula for snow hill shape
Problem statement
Problem statement

Find the altitude of the hill at 0.5 meter intervals from -5
Find the altitude of the hill at 0.5 meter intervals from -5
meters to 5 meters using the shape described and
meters to 5 meters using the shape described and
illustrated in the previous two slides.
illustrated in the previous two slides.

Tabulate the results.

Tabulate the results.
Structure plan
Structure plan

Structure plan
Structure plan

Initialize the range of X in 0.5 meter intervals.
Initialize the range of X in 0.5 meter intervals.

Compute y, the altitude, with the formula:
Compute y, the altitude, with the formula:


y = 1 – tanh(3*X/5).
y = 1 – tanh(3*X/5).

Display the results in a table.
Display the results in a table.

Implementation of the structure plan
Implementation of the structure plan

Open the editor by typing
Open the editor by typing
edit
edit
in the command window.
in the command window.


Translate plan into the M-file language.
Translate plan into the M-file language.
This is from editor: It is
lec3.m
%
% Ski slope offsets for the
% HYPERBOLIC TANGENT DESIGN
% by D.T. Valentine January 2007
%
% Points at which the function
% is to be evaluated:
% Step 1: Input x
x = -5:0.5:5;
% Step 2: Compute the y offset,
% that is, the altitude:
y = 1 - tanh(3.*x./5);
%
% Step 3: Display results in a table
%
disp(' ') % Skips a line
disp(' X Y')
disp([x' y'])

Command window
OUTPUT
X Y
-5.0000 1.9951
-4.5000 1.9910
-4.0000 1.9837
-3.5000 1.9705

-3.0000 1.9468
-2.5000 1.9051
-2.0000 1.8337
-1.5000 1.7163
-1.0000 1.5370
-0.5000 1.2913
0 1.0000
0.5000 0.7087
1.0000 0.4630
1.5000 0.2837
2.0000 0.1663
2.5000 0.0949
3.0000 0.0532
3.5000 0.0295
4.0000 0.0163
4.5000 0.0090
5.0000 0.0049
SOLUTION TO IN-CLASS EXERCISE
Use of repetition (‘for’)
Use of repetition (‘for’)

Repetition: In the previous example, the fastest way to
Repetition: In the previous example, the fastest way to
compute y was used. An alternative way is as follows:
compute y was used. An alternative way is as follows:
Replace:
Replace:


y = 1 - tanh(3.*x./5);

y = 1 - tanh(3.*x./5);
% This is “vectorized” approach.
% This is “vectorized” approach.
With:
With:
for n=1:21
for n=1:21


y(n) = 1 - tanh(3*x(n)/5);
y(n) = 1 - tanh(3*x(n)/5);
end
end
Remark: Of course, the output is the same.
Remark: Of course, the output is the same.
This is from editor: It is
lec3_2.m
%
% Ski slope offsets for the
% HYPERBOLIC TANGENT DESIGN
% by D.T. Valentine January 2007
%
% Points at which the function
% is to be evaluated:
% Step 1: Input x
x = -5:0.5:5;
% Step 2: Compute the y offset
for n=1:21
y(n) = 1 - tanh(3*x(n)/5);
end

%
% Step 3: Display results in a table
disp(' ') % Skips a line
disp(' X Y')
disp([x' y'])

Command window
OUTPUT
X Y
-5.0000 1.9951
-4.5000 1.9910
-4.0000 1.9837
-3.5000 1.9705
-3.0000 1.9468
-2.5000 1.9051
-2.0000 1.8337
-1.5000 1.7163
-1.0000 1.5370
-0.5000 1.2913
0 1.0000
0.5000 0.7087
1.0000 0.4630
1.5000 0.2837
2.0000 0.1663
2.5000 0.0949
3.0000 0.0532
3.5000 0.0295
4.0000 0.0163
4.5000 0.0090
5.0000 0.0049

SOLUTION TO IN-CLASS EXERCISE
Decision: Application of ‘if’
Decision: Application of ‘if’

Temperature conversion problem:
Temperature conversion problem:

Convert C to F or F to C.
Convert C to F or F to C.
%
% Temperature conversion from C to F
% or F to C as requested by the user
%
Dec = input(' Which way?: 1 => C to F? 0 => F to C: ');
Temp = input(' What is the temperature you want to convert? ');
%
% Note the logical equals sgn (==)
if Dec == 1
TF = (9/5)*Temp + 32;
disp(' Temperature in F: ')
disp(TF)
else
TC = (5/9)*(Temp-32);
disp(' Temperature in C: ')
disp(TC)
end
Decision: Application of ‘if’
Decision: Application of ‘if’

Temperature conversion problem:

Temperature conversion problem:

Convert C to F or F to C.
Convert C to F or F to C.

SOLUTION:
SOLUTION:
Summary
Summary

Introduced, by an example, the structure plan approach
Introduced, by an example, the structure plan approach
to design approaches to solve technical problems.
to design approaches to solve technical problems.

Input: Assignment with and without ‘input’ utility.
Input: Assignment with and without ‘input’ utility.

Output: Graphical & tabular were shown.
Output: Graphical & tabular were shown.

Illustrated array dot-operation, repetition (for) and
Illustrated array dot-operation, repetition (for) and
decision (if) programming tools.
decision (if) programming tools.

×