Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture 3 Creating M-files Programming tools: Input/output(assign/graph-&-display) Repetition (for) Decision (if) © 2007 Daniel Valentine. All rights reserved."— Presentation transcript:

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.

2 Review Arrays 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

3 Input Examples of input to arrays: 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.

4 Basic elements of a program Input Input –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 result. Output Output –Display (graphically or numerically) result.

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 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: Let us first examine the hyperbolic tangent function by executing the command: >> ezplot( ‘tanh(x)’ ) We get the following graph:

6 Ezplot(‘tanh(x)’)

7 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. – X = 0, tanh(X) = 0. –Transition from -1 to 1 is smooth and most rapid (roughly speaking) in the range -2<X<2. -2<X<2. –REMARK: With this familiarity with tanh(X), let us apply it to solve the following problem.

8 Problem background Let us consider the design of a slope that is to be modeled by tanh(X). Consider the range -3 3. Let us consider the design of a slope that is to be modeled by tanh(X). Consider the range -3 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 meters the hill drops 2 meters. 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). Thus, as shown next, the following formula is needed: y = 1 – tanh(3*X/5).

9 Formula for snow hill shape

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. 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. Tabulate the results.

11 Structure plan 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). y = 1 – tanh(3*X/5). –Display the results in a table. Implementation of the structure plan Implementation of the structure plan –Open the editor by typing edit in the command window. –Translate plan into the M-file language.

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

13 Use of repetition (‘for’) Repetition: In the previous example, the fastest way to compute y was used. An alternative way is as follows: Repetition: In the previous example, the fastest way to compute y was used. An alternative way is as follows:Replace: y = 1 - tanh(3.*x./5); % This is “vectorized” approach.With: for n=1:21 y(n) = 1 - tanh(3*x(n)/5); end Remark: Of course, the output is the same.

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

15 Decision: Application of ‘if’ Temperature conversion problem: Temperature conversion problem: –Convert C to F or F to C.

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 (==) 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’ Temperature conversion problem: Temperature conversion problem: –Convert C to F or F to C. –SOLUTION:

17 Summary Introduced, by an example, the structure plan approach to design approaches to solve technical problems. Introduced, by an example, the structure plan approach 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 decision (if) programming tools. Illustrated array dot-operation, repetition (for) and decision (if) programming tools.


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

Similar presentations


Ads by Google