mathematica – an introduction r.c. verma physics department punjabi university patiala – 147 002

22
MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002 PART IV- Programming in Mathematica Input and Output Logical Structures Transfer of control Subscripted Variables File Operations Loading a Package

Upload: chaney-franks

Post on 31-Dec-2015

50 views

Category:

Documents


0 download

DESCRIPTION

MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002 PART IV- Programming in Mathematica Input and Output Logical Structures Transfer of control Subscripted Variables File Operations Loading a Package. 40. Input Statements - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002

MATHEMATICA – AN INTRODUCTION

R.C. VermaPhysics DepartmentPunjabi UniversityPatiala – 147 002

PART IV- Programming in Mathematica

Input and Output

Logical Structures

Transfer of control

Subscripted Variables

File Operations

Loading a Package

Page 2: MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002

40. Input Statements

Direct Assigning:- Values of variables can be assigned directly.

x = 5.6From the Key Board

Value to a variable may be given through the keyboard using Input[ ]

Encountering this command, Mathematica would prompt with a window. Type the symbol and its value. e.g.

In[7]:= Input[ "mass = ?"]

When window appears, type mass = 5.0, and press the enter key. Mathematica then assigns value 5 to the variable mass, and responds with the following output:

Out[7]= 5.0

Page 3: MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002

41. Output statements

Results obtained in a program are generally written using the following statement:

Print[ variable ]

Messages can be printed on screen by enclosing them in double quote (“) sign:

In[8]:= Print[“You are welcome!”]Out[8] = You are welcome!

42. Suppressing Output

Some of the Mathematica commands produce superfluous output.

For instance, when a variable is assigned a value, Mathematica echoes the value in an Output cell.

It can be suppressed by putting a semicolon ; at the end. expression ;

Page 4: MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002

43. Placing two or more commands on the same line:

Two or more commands, separated by semicolon, can be given in one line.

expression1; expression2; expression3;

44. Shortening the Display in Output:-

Output of a certain command can be limited to approximately one line by suffix

// Short

expression // Short

Page 5: MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002

45. Referring to Previous Output

% symbol to the last output generated; %% to next-to-last output; and %n to output in Out[n].

In[17]:= 5Out[17]= 5

In[18]:= %^3Out[18]= 125

In[19]:= %%+7Out[19]= 12

In[20]:= %18+15Out[20]= 140

However, it is preferable to assign a variable to any expression or command, as the line number keeps on changing in different execution of the same notebook..

Page 6: MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002

46. Clearing Values

Mathematica never forgets values assigned to a variable unless instructed to do so.

A common source of puzzling bugs is the inadvertent reuse of previously defined variables or functions definitions.

Clear the value of a variable either before using it or immediately after using it.

To clear the value of the variable y, type

y = . or Clear[y].

In[15] : = Clear [y]

Several variables can be cleared together,

Clear[f, x, a]

To clear all the items, use the following command:

ln[16]:= Clear["Global`*"]

Page 7: MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002

47. Logical Structures

Like other languages, Mathematica supports the following logical structure:

Sequential: Top to Bottom flow

Repetitive: Loops: Do, While, For

Selective: If true/false conditions

Page 8: MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002

48. Repeating a Job: Repetitive structure

48.1 Do loops

Do[ statement/s , {n}]

The statements after Do are executed n times.

In[76]:= x = 1.0; Do[ x = 1/(1+x); Print[x], {5}]Out[76]=

0.50.6666670.60.6250.615385

Page 9: MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002

48.2 Do loops using a Counter

If a counter is given, as in the following line

Do[ statement/s, {j, jmin, jmax, dj}]

Then the statements are executed starting with j = jmin (starting parameter), whereupon the value of j is incremented by dj.

In[77]:= n = 10; Do[ Print[j^2], {j,2,n,2}]

Out[77]=4163664100

Page 10: MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002

48.3 Nested loops

Many Do loops may be used in a program.

Do[ statements, { i, m1, n1, k1}, { j, m2, n2, k2 } ]

Page 11: MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002

48.4 While loop

While[ test, body of statements ]

evaluates body of statements, so long as the test is true.In[78]:= n = 1; While[ n <= 5, Print[n^3]; n = n +1 ]Out[78]=

182764125

Page 12: MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002

48.5 For Loop

For[ start, test, increment, body of statements ]

evaluates start, then repetitively evaluates statements, & increment, until test fails.

In[79]:= For[ j = 1, j < 5, j++, Print[j] ]Out[79] =

1234

Page 13: MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002

49. Relational Expressions

MATHEMATICA has the following relational expressions:

Operator Meaning

= = Equal To

!= Not Equal To

< Less Than

> Greater Than

<= Less Than Or Equal To

>= Greater Than Or Equal To

Page 14: MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002

Two variables x and y in MATHEMATICA can be compared using the following relational statements:

(x = y) true if x equals y otherwise false;

(x != y) true if x and y are unequal otherwise false;

(x > y) true if x is greater than y, false otherwise;

(x < y) true if x is less than y, false otherwise;

(x >= y) true if x is greater than or equal to y, false otherwise;

(x <= y) true if x is less than or equal to y, false otherwise.

Page 15: MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002

50. Decision Making: Selective Structure

To execute the selective structure, Mathematics has the following command:

If[ logical expression, t-statements, f-statements ]

The t-statements will be executed if the logical expression is true,

otherwise f-statements will be executed if the logical expression is false.

In[80]:= x=51; y = 65; If[ x==y, Print["x equals y"], Print["x is not equal to y"]] Out[80]= x is not equal to y

Page 16: MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002

50. Logical operators

Relations given above may be combined with the following logical operator:

And, Or, Not

(A && B) is true only if both A and B are true, otherwise it is false.

(A || B) is true if either A or B is true (both may be true), otherwise it is false.

(! A) is true if A is false, and false if A is true.

Example

In[81]:= x = 26 If[ x <=50 && x >=10 , Print["Given no. lies in [10, 50]"] , Print["Given no. does not lie in [10, 50]"] ]

Out[81]= Given no. lies in [10, 50]

Page 17: MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002

51. Transfer of Control: Unconditional Jumping

The simple Goto statement transfers the control to another line within a procedure.

( ………….……. label ; ……. ……. ……. IF[ logical expression, Goto [ label ]] …….. …….. ……..)

Page 18: MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002

52. File Operations

52.1 Input files:-

<<input-file to read in a input file.

ReadList[“file”, Number]

reads numbers from a input file, and returns a list of them.

ReadList[“file”, Number, RecordLists->True]

reads numbers from a input file, making a separate list for each line in the file.

Page 19: MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002

52.2 For Output files:

expression >> output-file

to create an output file, and send expression in that file.

expression >>> output-file

appends expression to the already produced output file.

Page 20: MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002

52.3 Displaying file

!!file

displays the contents of a plain text file.

Example:-

In[152]:=(Do[WriteString["File1.dat",i," ", i^3, "\n"], {i,1,10,2}]; Close["File1.dat"]) !!File1.dat

Out[152] = File1.dat1 13 275 1257 3439 729

In[153]:= ReadList["File1.dat",(Number)]Out[153] = {1, 1, 3, 27, 5, 125, 7, 343, 9, 729}

Page 21: MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002

53. Loading Packages

Mathematica has a number of packages, which contain such extra functionality.

These also introduce new commands.

To use a command from a package, you must load the package,

<<package

reads in the package mentioned.

Need[“package`subpackage`] command is also provided to load a package.

In[154]:= Needs["Algebra`Trigonometry`"]

To see what are contained in this package, place a ? sign before it,

In[155]:= ?Algebra`Trigonometry`*Out[155] = ComplexToTrig TrigExpand TrigReduce TrigCanonical TrigFactor TrigToComplex

Page 22: MATHEMATICA – AN INTRODUCTION R.C. Verma Physics Department Punjabi University Patiala – 147 002

End of part IV