12.010 Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room 54-820A, Chris Hill, Room 54-1511,

Slides:



Advertisements
Similar presentations
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Advertisements

Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
Introduction to C Programming
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
ITEC113 Algorithms and Programming Techniques
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Structured programming
6 April, 2000 CS1001 Lecture 15 EXAM1 - results SUBPROGRAM - - revisit FUNCTION.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
6 April, 2000 CS1001 Lecture 13 PRACTICE EXAM - revisit SUBPROGRAM FUNCTION.
Basic Elements of C++ Chapter 2.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Functions. Program complexity the more complicated our programs get, the more difficult they are to develop and debug. It is easier to write short algorithms.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Introduction to Python
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Input, Output, and Processing
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
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.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
Some Fortran programming tips ATM 562 Fall 2015 Fovell (see also PDF file on class page) 1.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – PART IV- Programming in Mathematica Input and Output Logical.
JavaScript, Fourth Edition
Artificial Intelligence Lecture No. 26 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Awk- An Advanced Filter by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Chapter 9: Value-Returning Functions
Chapter Topics The Basics of a C++ Program Data Types
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Basic Elements of C++.
Introduction to Scripting
Scripts & Functions Scripts and functions are contained in .m-files
User-Defined Functions
Basic Elements of C++ Chapter 2.
Computational Methods of Scientific Programming
Faculty of Computer Science & Information System
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Flow of Control.
Introduction to the Lab
An Overview of C.
Presentation transcript:

Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room , Web page

10/21/ Lec 132 Mathematica Look in more detail at some of the programming features in Mathematica There are many of these features and in all Mathematica expressions there are Function names and “short-hand” symbols The + usage is actually a function Plus, * is Times Use of FullForm shows full form of expressions

10/21/ Lec 133 Subroutines (declaration) name[v1_Type, …] := Module[{local variables}, body] Type is optional for the arguments (passed by value) Invoked with name[same list of variable types] Example: sub1[i_] := Module[{s}, s = i + i^2 + i^3; Sqrt[s]] In main program or another subroutine/function: sum = sub1[j] Note: Names of arguments do not need to match those used to declare the function, just the types (if declared) needs to match, otherwise the function is not defined. *

10/21/ Lec 134 Functions: Comparison Fortran Real*8 function func(list of variables) Invoked with Result = func( same list of variable types) Example Real*8 function eval(i,value) Integer*4 I Real*8 value eval = I*value In main program or subroutine or function Real*8 result, eval Integer*4 j Real*8 sum Result = eval(j,sum) Mathematica func[list of variables] Invoked with result = func[same list of variables] Example eval[i_,value_] := i*value OR eval[i_Integer,value_Real] := i*value In main program or subroutine or function result = eval[j,sum]

10/21/ Lec 135 Functions 02 Functions can return any of the variable types The function name is a symbol The function must always appear with the same name, but other names can be defined in desired.

10/21/ Lec 136 Intrinsic functions These functions are embedded in the language and often go by "generic names." Mathematica has MANY of these (check out the Help under "Built in Functions")! Examples include Sin, Cos, Tan, ArcTan. Precisely which functions are available are machine independent. If a function is not available: function called is returned unchanged (i.e. function[x])

10/21/ Lec 137 Flow control If statement form: If[condition, t, f] gives t if condition evaluates to True, and f if it evaluates to False. If[condition, t, f, u] gives u if condition evaluates to neither True nor False. The standard conditions tests are ==, !=,, >= Multiple test are && (and) || (or) It also possible combine: if[ 7 > 6 > 5,..] rather than if[ 7 > 6 && 6 > 5, …] Which allows a range of actions: Which[test1, value1, test2, value2, test2, value2] Switch allows action based on result of expression: Switch[expr, form1, value1, form2, value2] Examples in Lec13.nb Lec13.nb

10/21/ Lec 138 Loop structures Do structure: Most general structure Do[expr, {i, imin, imax,di}, {j, jmin, jmax,dj}, … ] This would loop through values of j from jmin to jmax in increments of dj, for each value of i which would loop from imin to imax in increment of di. If the increment is not given 1 is assumed, if imax is not given, then loops from 1 to imin. If only 1 argument is given, expr is evaluated that many times. While[ test, body] executes code in body (statements are separated by ;) while ever test is true. Return[val] can be used to return a value from the body code; Break[]can be used to exit body For[start, test, incr, body] executes start, then repeatedly evaluates body and incr until test fails to give True Mathematica does have a Goto[tag] statement using Label[tag]

10/21/ Lec 139 Functions Function[body] or body& is a pure function. The formal parameters are # (or #1), #2, etc. Function[x, body] is a pure function with a single formal parameter x. Body can have mulitple statements separated by ; Function[{x1,x2,… }, body] is a pure function with a list of formal parameters. If the body is more than one statement, normally there would be a Return[.. ] call to set the quantity returned form the call. Map[f, expr] or f expr applies f to each element on the first level in expr. Apply[f, expr] or f expr replaces the head of expr by f. This is basically a way of changing what something is in Mathematica e.g., if expr is a list {…}, it can be changed to Times (multiply)

10/21/ Lec 1310 Pattern Matching _ or Blank[ ] is a pattern object that can stand for any Mathematica expression. _h or Blank[h] can stand for any expression with head h. We used this in an earlier lecture to make x_Integer for an integer argument. __h or BlankSequence[h] can stand for any sequence of one or more expressions, all of which have head h. g[x_, y__] := x + y; g[a, b, c] yield a+b+c Replace and Rules: -> (arrow on Palette) applies a rule for to convert lhs to rhs, /. is the replace all e.g. 1 + x /. x -> a yields 1+a (same as ReplaceAll[1 + x, x -> a]) There are many more forms of rules and replacements that are given in the Pattern Matching and Rule applications in the Programming section of the Mathematica help.

10/21/ Lec 1311 Format types Mathematica offers many different types of ways to display results and convert to different formats These are given in the Format Types under Input Output sections of the Built in Functions Some examples are TableForm, MatrixForm, TreeForm N[expr] gives the numerical value of expr. N[expr, n] attempts to give a result with n-digit precision.

10/21/ Lec 1312 Files and directories Directory[ ] - give your current working directory SetDirectory["dir"] - set your current working directory FileNames[ ] - list the files in your current working directory FileNames["form"] - list the files whose names match a certain form <<name - read in a file with the specified name (Get) <<context` - read in a file corresponding to the specified context CopyFile[“file1”,”file2”] - copies file1 to file2 DeleteFile[“file1”] - deletes the file. Input[“prompt”] is used to read information from the keyboard

10/21/ Lec 1313 Graphics Mathematica supports a variety of graphics plots through is basic plot command. Simple plots can be modified with options given in the plot command. Mathematic 6.0 and above has a new Manipulate command Syntax of command: The variable a here is the one that can be manipulated between values of 0 and 2. Manipulate[Plot[Sin[x (1 + a x)], {x, 0, 6}], {a, 0, 2}]

10/21/ Lec 1314 Final Comments Users of Mathematica need to understand the basics of the syntax of the program. The online help however provides the details of the capabilities of the program Built-in Functions is grouped by Numerical Computation Algebraic Computation Mathematical Functions Lists and Matrices Graphics and Sounds Program development should be knowing what you want to do and then finding the Functions that, in combination, will do the task. With Notebooks, you can keep track and comment on the way the program works. Homework #4 will be due Thursday Nov 18, 2010.