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

Slides:



Advertisements
Similar presentations
A number of MATLAB statements that allow us to control the order in which statements are executed in a program. There are two broad categories of control.
Advertisements

Introduction to MATLAB The language of Technical Computing.
Introduction to M ATLAB Programming Ian Brooks Institute for Climate & Atmospheric Science School of Earth & Environment
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
Lecture 4 Structure plan Program design process Program design process Basic elements of code: Basic elements of code: –Input (assignment of variables)
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Lecture 16 Symbolic Mathematics Symbolic mathematics: algebraezplotcalculus © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Maths for Computer Graphics
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
Lecture 8 Logical Operations Logical functions & Relational operators © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
Introduction to MATLAB MECH 300H Spring Starting of MATLAB.
Lecture 15 Practice & exploration Subfunctions: Functions within functions “Guessing Game”: A hands-on exercise in evolutionary design © 2007 Daniel Valentine.
What is MATLAB ? MATrix LABratory –Originally, it was a front-end to FORTRAN matrix routines developed in the U. of New Mexico and Stanford –Today.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
Creating scalars, vectors, matrices Ex1 & 2. Dot Product & Cross Product Ex3. Plotting Graphs Ex4. Conversion Table Ex5. Plotting functions Finishing Ex4.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Introduction to MATLAB 7 Engineering 161 Engineering Practices II Joe Mixsell Spring 2010.
Matlab for Engineers Manipulating Matlab Matrices Chapter 4.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Fundamental Programming: Fundamental Programming Introduction to C++
MATLAB Environment ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
Working with Arrays in MATLAB
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Introduction to MATLAB 7 MATLAB Programming for Engineer Hassan Migdadi Spring 2013.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Introduction to MATLAB 7 Engineering 161 Engineering Practices II Joe Mixsell Spring 2012.
3.6 Solving Systems Using Matrices You can use a matrix to represent and solve a system of equations without writing the variables. A matrix is a rectangular.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Array Creation ENGR 1187 MATLAB 2. Today’s Topics  Arrays: What are they?  Vectors  Matrices  Creating Arrays.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
Section 9-1 An Introduction to Matrices Objective: To perform scalar multiplication on a matrix. To solve matrices for variables. To solve problems using.
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
Program design and algorithm development We will consider the design of your own toolbox to be included among the toolboxes already available with your.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
An Introduction to Programming in Matlab Emily Blumenthal
Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
ENG College of Engineering Engineering Education Innovation Center 1 Arrays in MATLAB Topics Covered: 1.Creating arrays of numbers vectors matrices.
ECE 1304 Introduction to Electrical and Computer Engineering
Manipulating MATLAB Matrices Chapter 4
Lecture 1: Introduction
Use of Mathematics using Technology (Maltlab)
Arrays and Matrices in MATLAB
Lecture 4 Structure plan
2.5 Absolute Value Equations and Inequalities
Working with Arrays in MATLAB
Presentation transcript:

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.

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

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 = [ ] –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.

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.

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:

Ezplot(‘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. – 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.

Problem background Let us consider the design of a slope that is to be modeled by tanh(X). Consider the range Let us consider the design of a slope that is to be modeled by tanh(X). Consider the range 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).

Formula for snow hill shape

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.

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.

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 SOLUTION TO IN-CLASS EXERCISE

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.

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 SOLUTION TO IN-CLASS EXERCISE

Decision: Application of ‘if’ Temperature conversion problem: Temperature conversion problem: –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’ Temperature conversion problem: Temperature conversion problem: –Convert C to F or F to C. –SOLUTION:

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.