1 / 22

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

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

Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 40. Input Statements Direct Assigning:- Values of variables can be assigned directly. x = 5.6 From 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

  3. 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 ;

  4. 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

  5. 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]:= 5 Out[17]= 5 In[18]:= %^3 Out[18]= 125 In[19]:= %%+7 Out[19]= 12 In[20]:= %18+15 Out[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..

  6. 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`*"]

  7. 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

  8. 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.5 0.666667 0.6 0.625 0.615385

  9. 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]= 4 16 36 64 100

  10. 48.3 Nested loops Many Do loops may be used in a program. Do[ statements, { i, m1, n1, k1}, { j, m2, n2, k2 } ]

  11. 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]= 1 8 27 64 125

  12. 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] = 1 2 3 4

  13. 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

  14. 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.

  15. 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

  16. 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]

  17. 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 ]] …….. …….. …….. )

  18. 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.

  19. 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.

  20. 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.dat 1 1 3 27 5 125 7 343 9 729 In[153]:= ReadList["File1.dat",(Number)] Out[153] = {1, 1, 3, 27, 5, 125, 7, 343, 9, 729}

  21. 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

  22. End of part IV

More Related