mathematica: an interpreter for mathematical operations at symbolical and numerical level this...

26
Mathematica: An Interpreter for Mathematica: An Interpreter for Mathematical Operations at Mathematical Operations at Symbolical and Numerical Level Symbolical and Numerical Level This chapter is mainly devoted to some This chapter is mainly devoted to some applications of Mathematica. Although applications of Mathematica. Although Mathematica is taken as the basic platform Mathematica is taken as the basic platform for these hours the main purpose of the for these hours the main purpose of the course is to emphasize on symbolical and course is to emphasize on symbolical and numerical calculations by using computers. numerical calculations by using computers. That is, the syntax and the algorithms That is, the syntax and the algorithms not the languages themselves are not the languages themselves are considered as the basic components. This considered as the basic components. This chapter contains some basic structures for chapter contains some basic structures for the usage of mathematica. the usage of mathematica.

Post on 21-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

Mathematica: An Interpreter for Mathematica: An Interpreter for Mathematical Operations at Mathematical Operations at

Symbolical and Numerical LevelSymbolical and Numerical Level This chapter is mainly devoted to some This chapter is mainly devoted to some

applications of Mathematica. Although applications of Mathematica. Although Mathematica is taken as the basic platform Mathematica is taken as the basic platform for these hours the main purpose of the for these hours the main purpose of the course is to emphasize on symbolical and course is to emphasize on symbolical and numerical calculations by using computers. numerical calculations by using computers. That is, the syntax and the algorithms not That is, the syntax and the algorithms not the languages themselves are considered as the languages themselves are considered as the basic components. This chapter contains the basic components. This chapter contains some basic structures for the usage of some basic structures for the usage of mathematica.mathematica.

Page 2: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

Introduction Introduction The computer scientific term The computer scientific term interpreterinterpreter is used is used for the programs which can work mostly in for the programs which can work mostly in interactive modeinteractive mode. That is, the program creates an . That is, the program creates an environment for the user such that each environment for the user such that each instruction given by the user through the instruction given by the user through the interaction is immediately processed without interaction is immediately processed without waiting for the next entries. The interactive waiting for the next entries. The interactive mode can be avoided by using straightforward mode can be avoided by using straightforward processing of a bundle of commands included in processing of a bundle of commands included in a file prepared by the user. This processing a file prepared by the user. This processing fashion is called fashion is called batch modebatch mode. .

Page 3: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

There are various interpreters for There are various interpreters for symbolic and numeric operations. The symbolic and numeric operations. The first important development toward this first important development toward this target was the creation of the LISP target was the creation of the LISP programming language which was programming language which was designed for mostly artificial intelligence. designed for mostly artificial intelligence. LISP was an interpreter and its born LISP was an interpreter and its born stimulated the development of some well-stimulated the development of some well-known symbolic and numeric interpreters known symbolic and numeric interpreters like REDUCE,like REDUCE, Macsyma Macsyma, and , and MapleMaple. .

Page 4: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

This main purpose is these programs was This main purpose is these programs was chosen as giving the capability of dealing chosen as giving the capability of dealing with mathematical operations at high level with mathematical operations at high level as much as possible. as much as possible.

The program which was developed by the The program which was developed by the researchers in Princeton University at the researchers in Princeton University at the years of 1980s was designed in these years of 1980s was designed in these directions. However, this language which directions. However, this language which was named was named Mathematica Mathematica later is not a LISP later is not a LISP based program. It is costructed by using C based program. It is costructed by using C programming language.programming language.

Page 5: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

The purpose of the utilization of the C The purpose of the utilization of the C based structure is to avoid the slowness of based structure is to avoid the slowness of LISP interpreter.LISP interpreter.

A lot of symbolic and numeric interpreter A lot of symbolic and numeric interpreter including Mathematica are licensed and including Mathematica are licensed and needs payment which can not be considered needs payment which can not be considered inexpensive for moderate organizations inexpensive for moderate organizations although their academic versions may be although their academic versions may be reasonable for the budgets of some Turkish reasonable for the budgets of some Turkish organizations like universities.organizations like universities.

Page 6: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

We can itemize some important capabilities We can itemize some important capabilities of Mathematica as follows: of Mathematica as follows: 1.1. Mathematica can be used a pocket Mathematica can be used a pocket calculator. Calculations can be done either calculator. Calculations can be done either symbolic or numeric level. The default is symbolic or numeric level. The default is symbolic for mathematica. This utilization symbolic for mathematica. This utilization can be realized through an interactive way. can be realized through an interactive way. To activate an interactive session under To activate an interactive session under Linux the user should give the command Linux the user should give the command mathmath at the prompt of Linux shell. This at the prompt of Linux shell. This produces the following display: produces the following display:

Page 7: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

Mathematica 4.0 for Linux Mathematica 4.0 for Linux Copyright 1988-1999 Wolfram Research, Inc. Copyright 1988-1999 Wolfram Research, Inc. -- Terminal graphics initialized ---- Terminal graphics initialized -- In[1]: = In[1]: = where In[1]:= is the prompt of where In[1]:= is the prompt of Mathematica where the user command is Mathematica where the user command is waited for. Each entry given by the user waited for. Each entry given by the user numbered started from 1. Therefore, the numbered started from 1. Therefore, the user must type its first command at this user must type its first command at this prompt. The command will be entered prompt. The command will be entered when the user presses when the user presses EnterEnter or or ReturnReturn key. key.

Page 8: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

Generally, each input label or prompt, In[n]:=, Generally, each input label or prompt, In[n]:=, corresponds to an output label, Out[n]=, with corresponds to an output label, Out[n]=, with the same n value. the same n value. 2.2. It is possible to plot the relations It is possible to plot the relations between two or three entities by using a quite between two or three entities by using a quite advanced graphics utility coming with advanced graphics utility coming with Mathematica. The relations between the Mathematica. The relations between the entities can be given by either functions or entities can be given by either functions or data tables. The displaying can be realized in data tables. The displaying can be realized in either text (by some symbols like dots, letters) either text (by some symbols like dots, letters) or graphics display medium (by pixels).or graphics display medium (by pixels).

Page 9: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

The graphic display medium under Unix or The graphic display medium under Unix or Linux operating system is generally X Linux operating system is generally X window. The pixel by pixel displaying window. The pixel by pixel displaying under DOS is not possible but you can use under DOS is not possible but you can use graphical display facilities by passing into graphical display facilities by passing into the Windows medium. the Windows medium. 3.3. Mathematica Mathematica can be used as a high level programming can be used as a high level programming language. You can construct programs language. You can construct programs composed of Mathematica commands and composed of Mathematica commands and combine them into quite advanced combine them into quite advanced structures.structures.

Page 10: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

The high level statement here implies the great The high level statement here implies the great support of the structure of the program to the support of the structure of the program to the user. user. 4.4. It is possible to make data structural It is possible to make data structural analysis and modelling via Mathematica. analysis and modelling via Mathematica. 5.5. It is possible to get scientific and It is possible to get scientific and technical knowledge by using Mathematica. technical knowledge by using Mathematica. 6.6. The software desing of The software desing of Mathematica permits us to call some external Mathematica permits us to call some external facilities which are provided by some other facilities which are provided by some other programming languages or tools and use them programming languages or tools and use them on Mathematica background.on Mathematica background.

Page 11: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

7.7. It is possible to interactively design and It is possible to interactively design and create certain documents like web pages. create certain documents like web pages. Animations and various voice applications Animations and various voice applications can be constructed by using Mathematica. can be constructed by using Mathematica. 8.8. It is possible to It is possible to use and control system facilities within a use and control system facilities within a Mathematica session. That is, you can use Mathematica session. That is, you can use some system commands during a some system commands during a Mathematica session without existing. Mathematica session without existing. 9.9. Mathematica can be Mathematica can be used as an embedded systems. used as an embedded systems.

Page 12: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

Interactive Usage of MathematiceInteractive Usage of Mathematice A mathematica session is started by A mathematica session is started by giving the giving the mathmath command under Linux text command under Linux text console. If you use a term window under X console. If you use a term window under X Windows you can do the same thing and Windows you can do the same thing and proceed as if you work through the next proceed as if you work through the next terminal. However, it is possible to use a terminal. However, it is possible to use a different interaction between the user and different interaction between the user and Mathematica. It is called Mathematica. It is called notebooknotebook..

The NT version of Mathematica uses the The NT version of Mathematica uses the notebook interface mostly.notebook interface mostly.

Page 13: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

You can even use the text based You can even use the text based interaction under X Window system. interaction under X Window system. Then you can do everything you are Then you can do everything you are able to do in the textural interface and able to do in the textural interface and furthermore the graphical display furthermore the graphical display becomes available.becomes available.

The commands of Mathematica can be The commands of Mathematica can be given separately, that is, one command given separately, that is, one command at each single attempt or collectively, at each single attempt or collectively, that is, more than one commands at that is, more than one commands at each single attempts.each single attempts.

Page 14: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

In the case of more than one commands, In the case of more than one commands, the commands must be separated by using the commands must be separated by using a semicolon character between the a semicolon character between the commands. However, there is a pitfall commands. However, there is a pitfall here. According the Mathematica syntax here. According the Mathematica syntax the commands need to be terminated by the commands need to be terminated by semicolon and if a command is followed by semicolon and if a command is followed by a semicolan then Mathematica gives up a semicolan then Mathematica gives up display the result of the action defined by display the result of the action defined by that command otherwise it is displayed.that command otherwise it is displayed.

Page 15: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

It is possible to recall a previously given It is possible to recall a previously given command and execute it one more time command and execute it one more time through the Mathematica sessions. For through the Mathematica sessions. For this purpose, you can use % symbol to this purpose, you can use % symbol to enter the command given as the (n-1). enter the command given as the (n-1). entry through the prompt In[n]:=. entry through the prompt In[n]:=. If % symbol is inserted into a If % symbol is inserted into a command then the result of the previous command then the result of the previous command (not itself) is used at the place command (not itself) is used at the place where % is given.where % is given.

Page 16: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

There are a few possibilities to exit There are a few possibilities to exit Mathematica. The most generally used Mathematica. The most generally used one is the one is the Quit Quit command. You can also command. You can also use the keystrokes use the keystrokes Ctrl CCtrl C for breaking the for breaking the procedure and procedure and Ctrl ZCtrl Z for hanging the for hanging the process and rebooting the system. The process and rebooting the system. The selection from these possibilities depends selection from these possibilities depends on the user’s preference and the situation. on the user’s preference and the situation.

Page 17: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

Batch Operations Under Mathematica Batch Operations Under Mathematica You can write a bundle of Mathematica You can write a bundle of Mathematica commands into a file and construct a commands into a file and construct a program and then you can call this program program and then you can call this program within an interactive Mathematica session. within an interactive Mathematica session. For this purpose, you can enter the For this purpose, you can enter the command <<command << filename filename or the command or the command getget [[filenamefilename]] where where filenamefilename denotes the name denotes the name of the file under consideration. The first one of the file under consideration. The first one of these commands is similar to the of these commands is similar to the redirectioning feature of the UNIX systems. redirectioning feature of the UNIX systems.

Page 18: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

We can take the commands which will be We can take the commands which will be executed by Mathematica during a session executed by Mathematica during a session from a file. We can write the commands into from a file. We can write the commands into a file such that each line corresponds to a a file such that each line corresponds to a separate entry. It is better to terminate the separate entry. It is better to terminate the file by the command file by the command QuitQuit. We can exemplify . We can exemplify the batchwise utilization of Mathematica by the batchwise utilization of Mathematica by considering the following program file: considering the following program file: 1/2 1/2 2*3+5 2*3+5 Quit Quit

Page 19: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

Where the name of the Mathematica Where the name of the Mathematica program file can be anything within the program file can be anything within the regulations of the operating system under regulations of the operating system under consideration. However, it is better to consideration. However, it is better to use .m extension to distinguish Mathematica use .m extension to distinguish Mathematica files from the other files although some files from the other files although some other programs like other programs like ModulaModula may use this may use this suffix. If we name this file suffix. If we name this file sample1.msample1.m then we then we can use the command can use the command math < sample1.mmath < sample1.m which which is redirection of the input stream from is redirection of the input stream from Standard Input STDIN to the file Standard Input STDIN to the file sample1.msample1.m. .

Page 20: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

When this command is given the following When this command is given the following display appears on the screen: display appears on the screen: Mathematica 4.0 for Linux Copyright Mathematica 4.0 for Linux Copyright 1988-1999 Wolfram Research, Inc. -- 1988-1999 Wolfram Research, Inc. -- Terminalgraphics initialized --Terminalgraphics initialized -- In[1]:= In[1]:= Out[1]= 1/2 Out[1]= 1/2 In[2]:= In[2]:= Out[2]= 11 Out[2]= 11 In[3]:= In[3]:=

Page 21: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

Where the command inputs which are taken Where the command inputs which are taken from the file from the file sample1.msample1.m are not shown. The are not shown. The display contains the output lines and also input display contains the output lines and also input labels. It is also possible to redirect the output labels. It is also possible to redirect the output stream from standard output STDOUT to a file stream from standard output STDOUT to a file whose name is, say, whose name is, say, sample1.sessample1.ses by revising the by revising the above command to above command to math <sample1.m > math <sample1.m > sample1.sessample1.ses. The name of the output file here is . The name of the output file here is quite arbitrary under the limitation of the quite arbitrary under the limitation of the operating system. However, it is better to operating system. However, it is better to specific structures like .specific structures like .sesses to recall some to recall some features.features.

Page 22: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

Mathematical Operations Via Mathematica Mathematical Operations Via Mathematica Arithmetic OperationsArithmetic Operations The aritmethical operationsThe aritmethical operations under under Mathematica are done by using the operators Mathematica are done by using the operators which are denoted by the symbols +, -, /. You which are denoted by the symbols +, -, /. You can insert one of these symbols between two can insert one of these symbols between two symbolic or numeric value to get the symbolic or numeric value to get the corresponding actions. It is generally corresponding actions. It is generally preferred to leave spaces before and after each preferred to leave spaces before and after each symbol. There is no need to use a specific symbol. There is no need to use a specific symbol for the multiplication although the star symbol for the multiplication although the star * symbol can also be used for this purposes.* symbol can also be used for this purposes.

Page 23: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

To leave a space between two consecutive To leave a space between two consecutive entity is interpreted as multiplication by entity is interpreted as multiplication by Mathematica. The power operator is the Mathematica. The power operator is the caret character ˆ. The left side of this caret character ˆ. The left side of this operator is the base while its right entity is operator is the base while its right entity is the exponent. The level of the evaluation the exponent. The level of the evaluation depends on the arguments of this operator.depends on the arguments of this operator.

The results obtained from the arithmetic The results obtained from the arithmetic operations on integers are displayed in such a operations on integers are displayed in such a way that all digits of the result is shown in way that all digits of the result is shown in the display. the display.

Page 24: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

There is no finite limitations due to There is no finite limitations due to memory allocations of the other memory allocations of the other structural programming languages like structural programming languages like C, Fortran.C, Fortran.

You can evaluate the numerical value of You can evaluate the numerical value of any symbolic quantity which has any symbolic quantity which has numerical value. For this purpose the numerical value. For this purpose the command command NN[[entityentity] where ] where entity entity denotes denotes the entity whose value will be evaluated the entity whose value will be evaluated by Mathematica.by Mathematica.

Page 25: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

Mathematica can deal with the complex Mathematica can deal with the complex numbers also. For this purpose it is numbers also. For this purpose it is necessary to use the imaginary numbers unit necessary to use the imaginary numbers unit -1 which denoted by I in Mathematica. -1 which denoted by I in Mathematica. Archive Constants Archive Constants The values of The values of some widely used mathematical constants some widely used mathematical constants are known by Mathematica. In other words are known by Mathematica. In other words there are some reserved constants under there are some reserved constants under Mathematica to use for well known values of Mathematica to use for well known values of universal Mathematical constants.universal Mathematical constants.

Page 26: Mathematica: An Interpreter for Mathematical Operations at Symbolical and Numerical Level This chapter is mainly devoted to some applications of Mathematica

Amongst these we can mention I which is Amongst these we can mention I which is the unit imaginary numbers, that is, the unit imaginary numbers, that is, -1, -1, Infinity Infinity ((), ), Pi Pi (3.1415926..), (3.1415926..), DegreeDegree (The conversion factor from degree to (The conversion factor from degree to radian), radian), GoldenRatioGoldenRatio ((1+ ((1+5)/2), E 5)/2), E (e=2.71828..), (e=2.71828..), EulerGamma EulerGamma ((= = 0.577216..), 0.577216..), Catalan Catalan ((CC=0.915966..).=0.915966..).