intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Lecture 3 Creating M - files

Chia sẻ: Nguyễn Doãn Hùng | Ngày: | Loại File: PPT | Số trang:17

69
lượt xem
3
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Programming tools: Input/output (assign/graph-&-display) Repetition (for) Decision (if) 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

Chủ đề:
Lưu

Nội dung Text: Lecture 3 Creating M - files

  1. Lecture 3 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 Review 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 Input  Examples of input to arrays: – Single number array & scalar: 1×1 >> a = 2 >> – Row array & row vector: 1×n >> b = [1 3 2 5] >> [1 – Column array & column vector: nx1 >> b = b’ % This an application of the transpose. >> This – 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. >> This
  4. Basic elements of a program 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.
  5. An example of technical computing 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:
  6. Ezplot(‘tanh(x)’) Ezplot(‘tanh(x)’)
  7. Hyperbolic tangent: tanh(X) 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
  8. Problem background Problem background  Let us consider the design of a slope that  is to be modeled by tanh(X). Consider the  range ­3
  9. Formula for snow hill shape Formula for snow hill shape
  10. Problem statement 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.
  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). – 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.
  12. SOLUTION TO IN-CLASS EXERCISE  C o m m a n d   w i n d o w   OUTPUT This is from editor: It is         X        Y lec3.m    ­5.0000    1.9951 %    ­4.5000    1.9910 % Ski slope offsets for the    ­4.0000    1.9837 % HYPERBOLIC TANGENT DESIGN    ­3.5000    1.9705 % by D.T. Valentine...January 2007    ­3.0000    1.9468 %     ­2.5000    1.9051 % Points at which the function    ­2.0000    1.8337 % is to be evaluated:    ­1.5000    1.7163    ­1.0000    1.5370 % Step 1: Input x    ­0.5000    1.2913    x = ­5:0.5:5;          0    1.0000 % Step 2: Compute the y offset,     0.5000    0.7087 % that is, the altitude:     1.0000    0.4630    y = 1 ­ tanh(3.*x./5);     1.5000    0.2837 %     2.0000    0.1663 % Step 3: Display results in a table     2.5000    0.0949 %      3.0000    0.0532     3.5000    0.0295   disp(' ') % Skips a line     4.0000    0.0163   disp('       X        Y')     4.5000    0.0090   disp([x' y'])     5.0000    0.0049
  13. Use of repetition (‘for’) Use of repetition (‘for’)  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. SOLUTION TO IN-CLASS EXERCISE  C o m m a n d   w i n d o w   OUTPUT This is from editor: It is         X        Y lec3_2.m    ­5.0000    1.9951 %    ­4.5000    1.9910 % Ski slope offsets for the    ­4.0000    1.9837 % HYPERBOLIC TANGENT DESIGN    ­3.5000    1.9705 % by D.T. Valentine...January 2007    ­3.0000    1.9468 %     ­2.5000    1.9051 % Points at which the function    ­2.0000    1.8337 % is to be evaluated:    ­1.5000    1.7163    ­1.0000    1.5370 % Step 1: Input x    ­0.5000    1.2913    x = ­5:0.5:5;          0    1.0000 % Step 2: Compute the y offset     0.5000    0.7087 for n=1:21     1.0000    0.4630    y(n) = 1 ­ tanh(3*x(n)/5);     1.5000    0.2837 end     2.0000    0.1663 %     2.5000    0.0949 % Step 3: Display results in a table     3.0000    0.0532     3.5000    0.0295   disp(' ') % Skips a line     4.0000    0.0163   disp('       X        Y')     4.5000    0.0090   disp([x' y'])     5.0000    0.0049
  15. Decision: Application of ‘if’ Decision: Application of ‘if’  Temperature conversion problem: – Convert C to F or F to C.
  16. Decision: Application of ‘if’ Temperature conversion problem:  – Convert C to F or F to C. – SOLUTION: % % 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
  17. Summary Summary  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.
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2