banner



How To Get Mathematica To Output Numerical Answer


This section explores the basic concepts behind numerical calculations using Mathematica.  In this case the software is treated more as a calculator.  In the next section some of these concepts will be expanded to be more oriented towards the consideration of algebraic expressions.

Integers and floating-point numbers

There are two types of numbers that will be typically encountered in the following work: integers and floating-point numbers.

An integer is a whole number, or the negative of a whole number, that is expressed without a decimal point.  It is considered by Mathematica to be an exact value, and thus, when evaluated in certain functions, will yield an exact value, not simplified to a "convenient" decimal value.

Example 1: The sine function satisfies the identity

                      sin(2x) = 2 sin(x) cos(x),

Thus we see that when x = 2 we should get

                      sin(4) - 2 sin(2) cos(2) = 0.

When we consider this expression in Mathematica, we obtain

In[1]:= Sin[4]-2*Sin[2]*Cos[2] Out[1]= -2 Cos[2] Sin[2] + Sin[4]

which is a rewrite of the input.  This does not simplify to 0 because Mathematica is treating the expressions involving integers as exact values, and thus is only able to simplify the expression by considering it symbolically, which it is unable to do without some form of additional specification.
Example 2: If we wish to add the fractions 1/2 + 1/7 + 1/17 and get the exact value, then we can do so with the programIn[2]:= 1/2+1/7+1/17

           167

Out[2]= ---
238


A floating-point number is a real number that is expressed with a decimal point.  It is basically a number that is represented with a finite amout of precision, and thus any computation involving such numbers has the potential for error.  Computations involving floating-point numbers, or at least one such number, will always yield floating-point numbers.
Example 3: Let us now consider the expression

                      sin(4) - 2 sin(2) cos(2) = 0

by representing the numerical values as floating-point numbers in Mathematica.  We do this as follows:

In[3]:= Sin[4.]-2*Sin[2.]*Cos[2.] Out[3]= 1.11022×10-16

Note that this computation does not yield the exact value of 0, although the significance of this inaccuracy may be considered as rather small.

An exact integer expression exprssn can be converted to a floating-point number by means of the command

N[ exprssn ]

This will perform any unsimplified numerical calculations and yield a floating-point number as the output.

Grouping operations

If a set of arithmetic operations needs to be grouped together and performed separately from among a larger collection of arithmetic operations, then it is grouped by means of parentheses, () , and not with brackets, [] , or braces, {} . Mathematica will always perform grouped operations first.

Addition and Subtraction

Mathematica performs the operations of addition and subtraction by following the same rules that are defined for these operations--by adding or subtracting numbers by proceeding from the left to the right, performing operations in parentheses first.  Examples of addition and subtraction in Mathematica follow:

Example 1: 5 - 7 - 4In[4]:= 5-7-4 Out[4]= -6


Example 2: 5 - (7 - 4) In[5]:= 5-(7-4) Out[5]= 2

Multiplication

Mathematica performs operations involving multiplication according to the defined rules: operations in parentheses first, then multiplications performed from left to right, and finally additions and subtractions proceeding from left to right.  Examples of multiplication in Mathematica follow:

Example 1: 10 (5 - 3)In[6]:= 10*(5-3) Out[6]= 20


Example 2: 4 (2 - 7) + 3 (4 - 2) In[7]:= 4*(2-7)+3*(4-2) Out[7]= -14

Division

With division is where Mathematica makes a noticeable distinction between integer arithmetic and floating-point arithmetic.  Division is a form of multiplication, and so follows the same rules as multiplication.  Examples of division in Mathematica follow:

Example 1: 12 / 3 / 2In[8]:= 12/3/2 Out[8]= 4


Example 2: 3 - 2 * (4 - 7)/5 In[9]:= 3-2*(4-7)/5

           21

Out[9]= --
5


Example 3: 1/3/1/3 verses (1/3)/(1/3)In[10]:= 1/3/1/3
(1/3)/(1/3)

            1

Out[10]= -
9Out[11]= 1

Powers

In Mathematica, to denote the quantity "a raised to the power b", one types a^b .  Powers are always performed before multiplications and divisions, and a series of powers is evaluated from the right to the left.  Examples of powers in Mathematica follow:

Example 1: 5 * 10^(13 - 4) In[12]:= 5*10^(13-4) Out[12]= 5000000000


Example 2: 2^4^2 verses (2^4)^2In[13]:= 2^4^2
(2^4)^2 Out[13]= 65536Out[14]= 256

Roots

In Mathematica the square root of a number n can be represented by the expression Sqrt[ n ] .  Other roots can be represented by means of fractional powers--the mth root of n can be expressed as n ^(1/ m ) .  Some examples follow.

Example 1: The square root of 12 In[15]:= Sqrt[12]
Sqrt[12.] Out[15]= 2 Sqrt[3]Out[16]= 3.4641


Example 2: The 5th root of 3In[17]:= 3^(1/5)
3.^(1/5) Out[17]= 31/5 Out[18]= 1.24573

Scientific Notation

The input of a number in the form a × 10 b , where a has a decimal point and b is an integer, can be done in two different ways in Mathematica.  It can be input in the exact same form, a*10^b , or it can be input in the form a*^b , in which the base 10 of the exponent is not explicitly expressed but is assumed.

Example 1: 4.34 * 10^15 In[19]:= 4.34*^15
4.34*10^15 Out[19]= 4.34×1015 Out[20]= 4.34×1015

Trigonometric Functions

The six circular and the six hyperbolic trigonometric functions are each defined as functions in Mathematica, as are the inverses of these functions.  They are defined as follows:

Trigonometric Functions
Function Mathematica function Function Mathematica function
sin(x) Sin[ x ] sinh(x) Sinh[ x ]
cos(x) Cos[ x ] cosh(x) Cosh[ x ]
tan(x) Tan[ x ] tanh(x) Tanh[ x ]
cot(x) Cot[ x ] coth(x) Coth[ x ]
sec(x) Sec[ x ] sech(x) Sech[ x ]
csc(x) Csc[ x ] csch(x) Csch[ x ]
Arcsin(x) ArcSin[ x ] Arcsinh(x) ArcSinh[ x ]
Arccos(x) ArcCos[ x ] Arccosh(x) ArcCosh[ x ]
Arctan(x) ArcTan[ x ] Arctanh(x) ArcTanh[ x ]
Arccot(x) ArcCot[ x ] Arccoth(x) ArcCoth[ x ]
Arcsec(x) ArcSec[ x ] Arcsech(x) ArcSech[ x ]
Arccsc(x) ArcCsc[ x ] Arccsch(x) ArcCsch[ x ]

Mathematica evaluates the numerical input of these functions as if the input is in radians (as opposed to degrees), and yields the output of the inverse functions as if in radians.  Note that the first letter of each function is capitalized, and that brackets, instead of parentheses or braces, must be used in the designation of the argument of these functions.

Exponential and Logarithmic Functions

In Mathematica the exponential function, e x , is represented by Exp[ x ] , while the natural logarithm function, ln(x), is represented by Log[ x ] .  The base a logarithm function, log a (x), is represented by Log[ a , x ] .

Various Functions

There are a number of other functions defined in Mathematica.  We present a few of them here.

Function Mathematica definition
Max[ x 1,x 2,...,x n ] yields the maximum value from among x 1, x 2, ..., x n.
Min[ x 1,x 2,...,x n ] yields the minimum value from among x 1, x 2, ..., x n.
Abs[ x ] yields the absolute value of x.
n ! yields the number 1*2*3*···*(n-1)*n, provided that n is a positive integer.

Defining Functions

In addition to the functions that are built into Mathematica, one can define functions independently.  If fnctn is some function in the variable x, then it can be defined as a function of x in Mathematica with a command of the form

g[x_] = fnctn

Note that some letter, or letters, other than

g could have used to name this function in our definition, and something other than x could have been used for the variable.  However, the underscore bar, " _ ", that accompanies the variable on the right is a necessity in the definition.  Once a function is defined, we can use it to evaluate different quantities without having to redefine the function.
Example 1: Let us define the function f(t) = t - t 3/6 in Mathematica, and then use this function to evaluate f(.2) and f(.3).

We can do this with the following program:

In[21]:= f[t_]=t-t^3/6;
f[.2]
f[.3] Out[21]= 0.198667Out[22]= 0.2955


Example 2: Let us define r(x) = x - x 3/6 - sin(x) in Mathematica, and use this to evaluate r(1) and r(2).

This is done as follows:

In[23]:= r[x_]=x-x^3/6-Sin[x];
r[1.]
r[2.] Out[23]= -0.00813765Out[24]= -0.242631

Mathematical Constants

Mathematica has some constants that are built in, that it treats as exact and not approximate.

One of the constants is the mathematical constant = 3.14159..., represented as Pi in Mathematica.  Note that this constant is irrational and cannot by held in terms of a decimal expansion by any computer.  However, Mathematica represents this value with a symbol, Pi , as is done mathematically with the symbol .

Another constant represented specifically in Mathematica is the constant e = 2.71828..., the base of the natural logarithm function.  It is represented as E by Mathematica.  Note that E = Exp[1] , and so the Mathematica command E^x is equivalent to Exp[x] .

A special value that Mathematica also represents with a symbol is the square root of -1, the imaginary number i, so that i 2 = -1.  This is represented in Mathematica as I .

Mathematica uses Infinity to denote positive infinity.

Other Sites

Since these are just the basic numerical calculations, a more exhaustive study of other numerical calculations can be found in the following sites:

1. Saaz Tutorials: Numerical Calculations
2. Princeton's Tutorial: Basic Calculations

Some of this is beyond our intentions in this course, however this material serves as a quick reference guide.

How To Get Mathematica To Output Numerical Answer

Source: http://www.mathcs.emory.edu/~fox/NewCCS/ModuleI/ModIP3.html

Posted by: holleyseentrusels.blogspot.com

0 Response to "How To Get Mathematica To Output Numerical Answer"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel