MATHCAD'S PROGRAMMING LANGUAGE The for Loop

Similar documents
Microsoft Office Excel Use Excel s functions. Tutorial 2 Working With Formulas and Functions

It can be confusing when you type something like the expressions below and get an error message. a range variable definition a vector of sine values

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

EGR 111 Loops. This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Macro Programming Reference Guide. Copyright 2005 Scott Martinez

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

EGR 111 Loops. This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times.

Vocabulary: Looking For Pythagoras

Chapter 2: Functions and Control Structures

Simple Java Programming Constructs 4

Parallelizing The Matrix Multiplication. 6/10/2013 LONI Parallel Programming Workshop

MATLAB TUTORIAL WORKSHEET

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Dept. of CSE, IIT KGP

Functions and Graphs. The METRIC Project, Imperial College. Imperial College of Science Technology and Medicine, 1996.

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

9. Elementary Algebraic and Transcendental Scalar Functions

Activity: page 1/10 Introduction to Excel. Getting Started

YOLOP Language Reference Manual

Lecture 3 - Template and Vectors

A = [1, 6; 78, 9] Note: everything is case-sensitive, so a and A are different. One enters the above matrix as

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

Computer Science 210 Data Structures Siena College Fall Topic Notes: Methods

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

function at the given point. Thus, for our previous example we would find that lim f(x) = 0

REPETITION CONTROL STRUCTURE LOGO

Programming for Electrical and Computer Engineers. Loops

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

The Mathcad Workspace 7

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

LOOPS. Repetition using the while statement

n Group of statements that are executed repeatedly while some condition remains true

LECTURE 0: Introduction and Background

What is an algorithm?

Grace days can not be used for this assignment

2. Use elementary row operations to rewrite the augmented matrix in a simpler form (i.e., one whose solutions are easy to find).

Vectors and Matrices. Chapter 2. Linguaggio Programmazione Matlab-Simulink (2017/2018)

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

Shift Register: Exercise # 1: Shift Register Example VI. 1. Build the following front panel. Figure (8.1): Shift register exercise front panel

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

ECE 202 LAB 3 ADVANCED MATLAB

Table : IEEE Single Format ± a a 2 a 3 :::a 8 b b 2 b 3 :::b 23 If exponent bitstring a :::a 8 is Then numerical value represented is ( ) 2 = (

Sketchpad Graphics Language Reference Manual. Zhongyu Wang, zw2259 Yichen Liu, yl2904 Yan Peng, yp2321

EXTENSION. a 1 b 1 c 1 d 1. Rows l a 2 b 2 c 2 d 2. a 3 x b 3 y c 3 z d 3. This system can be written in an abbreviated form as

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

Double Integrals in Polar Coordinates

1 Built-In Math Functions

Warm-up and Memoization

BoredGames Language Reference Manual A Language for Board Games. Brandon Kessler (bpk2107) and Kristen Wise (kew2132)

Repetition CSC 121 Fall 2014 Howard Rosenthal

Notes on efficient Matlab programming

Practical 4: The Integrate & Fire neuron

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

EE 301 Signals & Systems I MATLAB Tutorial with Questions

Lecture 57 Dynamic Programming. (Refer Slide Time: 00:31)

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Relational and Logical Operators. MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs. Examples. Logical Operators.

MATLAB Lesson I. Chiara Lelli. October 2, Politecnico di Milano

Looping. Arizona State University 1

Unit 3: Multiplication and Division Reference Guide pages x 7 = 392 factors: 56, 7 product 392

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Repetition Structures Chapter 9

Computer Vision. Matlab

School of Computer Science CPS109 Course Notes Set 7 Alexander Ferworn Updated Fall 15 CPS109 Course Notes 7

Dynamics and Vibrations Mupad tutorial

ü 1.1 Getting Started

MAT 243 Test 2 SOLUTIONS, FORM A

Flow of Control. 2.1 The if Statement

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

1 Overview of the standard Matlab syntax

Stokes Modelling Workshop

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

ECE Lesson Plan - Class 1 Fall, 2001

University of Massachusetts Lowell

MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

Matlab Introduction. Scalar Variables and Arithmetic Operators

Chapter 5: Loops and Files

6.001 SICP. Types. Types compound data. Types simple data. Types. Types procedures

Outline. Midterm Review. Using Excel. Midterm Review: Excel Basics. Using VBA. Sample Exam Question. Midterm Review April 4, 2014

Chapter 4 Introduction to Control Statements

FOR loops! Objectives! 1E3! Topic 7! n To be able to write a FOR statement! n To be able to predict what a given FOR statement will do.!

SPARK-PL: Introduction

Lab 1 Intro to MATLAB and FreeMat

STUDENT LESSON A12 Iterations

Oracle SQL. murach s. and PL/SQL TRAINING & REFERENCE. (Chapter 2)

142

2.3 Algorithms Using Map-Reduce

Matlab Tutorial. The value assigned to a variable can be checked by simply typing in the variable name:

6.001 Notes: Section 4.1

Matrices. Chapter Matrix A Mathematical Definition Matrix Dimensions and Notation

LESSON 6 FLOW OF CONTROL

Intro. Scheme Basics. scm> 5 5. scm>

Transcription:

MATHCAD'S PROGRAMMING LANGUAGE The Loop Introduction In this section we look at the loop construct in Mathcad programming. A loop is a means of perming a sequence of similar operations repeatedly over some range of values. Looping outside of programs can be accomplished with a range variable. When a Mathcad document is treated as a program, the entire worksheet can be iterated by external scripting of variables. Range variables can be used in many ways, such as filling an array, i 2 X i sin() i accumulating values contained in a specific array, sum_of_cubes sum_of_cubes i sum_of_cubes i X i sum_of_cubes (.769 ) or perming more complicated iterations over large ranges, A B.5 j A j B j A j 2 j j B j exp mod A j B j Range variables can also be used in a programmatic sense with the until function, demonstrated earlier.

These computations may be part of a sequence of steps that lend themselves to a program definition. As we mentioned in the section on assignments, defining range variables is not allowed within a program. The construct there is the loop. The Loop Operator Clicking on the button in the Programming toolbar creates an operator that looks like Note: As with the if operator we emphasize that you cannot type this operator. You must use the button. Bee we give the details, here are some simple uses of the loop in a program. This function creates an array specifying N equal subintervals of the interval [a, b] Partition( ab N ) Δ b a N P i N P i a i Δ Partition( 5).2.4 Partition( 4 4).6.8 2 4

The next function adds the reciprocals of the nonzero elements in a vector: rsum( V) sum x V sum sum if x = sum x otherwise rsum 2.8 rsum So, there you see in action. Here are the details. The first placeholder in the operator, iteration variable is reserved the name of a variable that we'll refer to as the iteration variable: i k x

The second placeholder contains a set of values that the iteration variable will take on over the course of the loop. range, list of values, vector This set of values can be specified with a range i The range may be increasing, decreasing, have a specified increment, or use the default increment (decrement) of (-). Alternatively, the set of values may be specified simply as a list of numbers separated by commas. k 7 59 Lastly, the set of iteration variable values may be the elements of a vector V.2 9. 2. 8. x V Or some combination of the above may be used, separated by commas.

The final placeholder contains an expression or set of expressions, that is, a subprogram that you wish executed each value of the loop variable in the specified range. set of expressions These will usually be local assignment statements but do not have to be. Here are some examples of completed loops. i t i i k 7 59 sum sum k prod x V prod k sign sign if x sign sign if x sign sign otherwise Execution of a loop then perms the indicated computations in the set of expressions each value of the loop variable in the order specified. However, this execution is only of interest (or of use, that matter) within a program.

2.44.72 t i i i 4 5 6 7 2 2.26 2.449 2.646 8 2.828 9.62 You shouldn't need to use the loop outside of a program, mainly because the assignments inside a loop must be local. Range variables in conjunction with worksheet-level vector/matrix assignments should do what is required at the worksheet level. Note that the t assigned above is local only, and not available in the worksheet. t

Examples of programs using the loops above are r i r r 5 2.26 t t i i r Q sum prod k 7 59 sum sum k prod sum prod p sign p 2 x V prod k sign sign if x sign sign if x sign sign otherwise Q 24 2.295 In the case of a range specified with a vector, the order of values the iteration variable takes on is from the first element to the last in sequence.

The symbol is usually read as "element of" or simply "in" and loops such as i x.2 might be read as " i in the set to do..." and " x from to with steps of.2 do the following..." respectively. Note: The three methods described that define the range of iteration may be combined into a single list. Just separate the pieces with commas. For example, the loop in q w q 2..5. q q w 6 5 8 2 ( 2 2) perms the exact same sequence of iterations as 22.9 q w 2..5.5 8 6 2 24682 q q q w 22.9

or W 2..5. 5 8 6 2 2 4 6 8 2 T q w W q q q w 22.9 Note in specifying the values 2..5. 6 5 8 2 ( 2 2) we enclosed the range piece 2 2 in parentheses. Using parentheses is a practice we highly recommend. The comma that appears in a range definition means something different from the comma used to separate values in our iteration value list. These different meanings can lead to confusion. For example at a glance, the list 4 6 could be a list of the values and 4 followed by the range 6 meaning a total set of values: 4 6 7 8 9 or the list could mean the single value followed by the range 46 meaning the set: 4 6 8 Parentheses remove any ambiguity. 4 ( 6 ) ( 46 )

Examples Using Loops As you might guess, loops are especially useful when working with arrays. Our first function takes a function of two variables f as an argument and an integer N and returns an (N+) x (N+) grid of values of f over the square x y fgrid( f N ) S i N S ij j N f i j N N Fxy ( ) sin( x) cos( y) fgrid( F 5).99.89.565.77.84.98.79.69.545.697.822.92.2..486.68.76.825.24.25.9.54.667.697.895.86.26.44.58.54.79.9.5.258.82 Note in this program a loop lives inside another loop. There is nothing wrong with that. For the first value of the outer iteration variable i the innermost loop is executed in its entirety, that is S j j N f j N N

then the next value of the outer iteration variable is used, i S j j N f j N N and so on. Note that this construct means the array S is filled up a row at a time. Our next example locates all occurrences of a number in a vector returning a vector of indices or the number - if no occurrences are found. locate( xv ) count ilast( V) if x = V i I count i count count I if count otherwise 9 locate 2 6..4 locate... 2

Our next example computes the Cartesian product of two vectors. Given two vectors of any sizes, the Cartesian product is the set of all points (vectors of two elements) that can be med by taking an element from the first vector and an element from the second. CartProd( X Y ) ind x X P y Y P ind x y ind ind C CartProd..5.7 C T..5.7..5.7 The two previous examples illustrate how easy it is to define functions that operate on vectors and arrays of unknown size by using loops.

The following example demonstrates that you can iterate over a vector of matrices. The function Prod takes a vector of arrays V and returns the array that is the product of all the arrays in V. Prod( V) P identity rows V A V P P P A 2 2. V 6 8 5 V 2.. 5 5. 4 9.8.8 4.4 2. V 2 V ( 4).7 V V V 2 V 2.4 4. 95.5 2.75 5.29 6.2 2.99 286.5 6.225 5.87 2.4 4. 955. 2.75 52.9 48.6 7.2 82.4 8. 25.6 Prod( V) 2.4 4. 95.5 2.75 5.29 6.2 2.99 286.5 6.225 5.87 2.4 4. 955. 2.75 52.9 48.6 7.2 82.4 8. 25.6 More examples of loops appear in the section Examples of Programming in Mathcad.