1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture 3 Creating M - files docx

17 398 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Creating M-files
Tác giả Daniel Valentine
Trường học Elsevier
Chuyên ngành Programming
Thể loại Lecture
Năm xuất bản 2007
Thành phố Unknown
Định dạng
Số trang 17
Dung lượng 241,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

An example of technical computing Let us consider using the hyperbolic tangent to model a down-hill section of a snowboard or snow ski facility..  Let us first examine the hyperbolic t

Trang 1

Lecture 3 Creating M-files

Programming tools:

Input/output (assign/graph-&-display)

Repetition (for) Decision (if)

© 2007 Daniel Valentine All rights reserved Published by

Elsevier.

Trang 2

 Arrays

– List of numbers in brackets

 A comma or space separates numbers (columns)

 A semicolon separates row

– Zeros and ones Matrices:

 zeros()

 ones()

– Indexing

 (row,column)

– Colon Operator:

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

– Manipulating Arrays & Matrices

 Transpose

Trang 3

 Examples of input to arrays:

– Single number array & scalar: 1 × 1

>> a = 2

– Row array & row vector: 1 × n

>> b = [1 3 2 5]

– Column array & column vector: n x 1

>> b = b’ % This an application of the transpose.

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

>> c = [1 2 3; 4 5 6; 7 6 9] % This example is 3 x 3.

Trang 4

Basic elements of a program

 Input

– Initialize, define or assign numerical values to

variables

 Set of command expressions

– Operations applied to input variables that lead

to the desired result

 Output

– Display (graphically or numerically) result.

Trang 5

An example of technical computing

 Let us consider using the hyperbolic tangent

to model a down-hill section of a snowboard

or snow ski facility

 Let us first examine the hyperbolic tangent function by executing the command:

>> ezplot( ‘tanh(x)’ )

We get the following graph:

Trang 6

Ezplot(‘tanh(x)’)

Trang 7

Hyperbolic tangent: tanh(X)

 Properties (as illustrated in the figure):

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

– As X approaches Inf, tanh(X) approaches +1. – X = 0, tanh(X) = 0.

– Transition from -1 to 1 is smooth and most

rapid (roughly speaking) in the range

-2<X<2.

– REMARK: With this familiarity with tanh(X), let

us apply it to solve the following problem.

Trang 8

Problem background

 Let us consider the design of a slope that

is to be modeled by tanh(X) Consider the range -3<X<3, and assume the slope

changes negligibly for |X|>3

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

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

 Thus, as shown next, the following

formula is needed: y = 1 – tanh(3*X/5)

Trang 9

Formula for snow hill shape

Trang 10

Problem statement

 Find the altitude of the hill at 0.5 meter

intervals from -5 meters to 5 meters using the shape described and illustrated in the previous two slides

 Tabulate the results

Trang 11

Structure plan

 Structure plan

– Initialize the range of X in 0.5 meter intervals.

– Compute y, the altitude, with the formula:

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

– Display the results in a table.

 Implementation of the structure plan

– Open the editor by typing edit in the

command window.

– Translate plan into the M-file language.

Trang 12

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

Trang 13

Use of repetition (‘for’)

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

Replace:

With:

for n=1:21

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

end

Remark: Of course, the output is the same.

Trang 14

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

Trang 15

Decision: Application of ‘if’

– Convert C to F or F to C.

Trang 16

% 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 (==)

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’

 Temperature conversion problem:

– Convert C to F or F to C.

– SOLUTION:

Trang 17

 Introduced, by an example, the structure plan approach to design approaches to

solve technical problems

 Input: Assignment with and without ‘input’ utility

 Output: Graphical & tabular were shown

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

Ngày đăng: 07/03/2014, 08:20

TỪ KHÓA LIÊN QUAN

w