MATHEMATICA
How to input expressions in Mathematica:
We’ve discussed some of the basic features of Mathematica as a
tool for performing basic computations as well as very complex
computations. We have discussed the different types of cells
which are in the program and some of the basic formats required.
One of the format requirements that we discussed last period
revolved around the presence of spaces between characters. Consider
the following expressions:
1 + 2 or 1+2
2 x + 3 y or 2x+3y
Actually we can either put spaces between the characters in these
problems or leave spaces out and put the characters side by side
(no spaces). In these cases, Mathematica will automatically put
the spaces in or and treat the spaces as a multiplication.
The one area in which it is necessary to be very careful with
spaces is in cases with of characters or variables. For example
if we have a variable x and a variable y and we want to multiply
the characters, it is imperative that we leave a space between the
characters:
x y NOT xy
The reason for the required space has to do with the fact that
Mathematica allows multicharacter symbols. In other words, we
could use xy as a symbol.
A symbol in Mathematica is any uninterupted string of characters
and digits that begins with a letter.
Basic Mathematical Operators:
addition +
subtraction -
division /
multiplication space or *
exponent ^
It is very important to use parentheses to maintain the order of
the calculations.
(x+y)/2
Without the parentheses, x+y/2, Mathematica will only divide the
y by 2.
Built-In Functions:
Mathematica has a number of built-in functions that are necessary
to become familiar with in order to fully utilize the program.
All commands in Mathematica will begin with a capital letter.
Any arguments that are associated with the command are enclosed
in square brackets. For example if we want to take the Sine of
an angle,
Sin[angle]
where angle is either some numerical value or a symbol.
Equality:
There are different methods of distinguishing equality in Mathematica.
Case 1:
= the most common form of equality comes in the standard
= operator. This form is used when we simply want to assign a
particular value to a symbol. For example x = 1, x = y + z, etc.
Case 2:
:= this form is used in cases when it is desired to define a
particular function that may be evaluated later in the notebook.
An example of this type of operator is used in the first lab:
h[t_] := vO t - ˝ g t^2
An important feature of this expression is the t_ which is present
within the square brackets of the function. The underscore
essentially tells the kernel that this may be any value. It
does not have to be referred to as "t" later in the notebook.
For example, if we had defined the function "h" as above, and we
wished to plot the function we could later issue the command:
Plot[h[y],{y,0,5}]
If we wanted we could also have defined our function with multiple
variables:
h[v0_,t_] := v0 t - ˝ g t^2
When we want to evaluate this expression later we can put in any
two values for v0 and t.
h[100,10] would evaluate our function at v0 = 100 and t =10.
Case 3:
= = This operator asks the kernel to compare the expression
to the left to the expression on the right of the operator to see
if it is true or false.
For example 10 = = 5 + 1 would return an answer of "false"
since the two sides are not equal.
We can also use functions such as <=, >, >=, and != , in these
expressions:
For example 10 >= 5 + 1 would return an answer of "true" since
10 is greater than or equal to 6.
Use of "Units"
You can actually "trick" Mathematica into using units on your
calculations, however you need to be aware of what you are doing
or you may run into problems later in your notebook.
When we specified the acceleration due to gravity as g = 32.2,
we could actually have specified it as:
g = 32.2 ft / s^2
Consider our expression for h again:
h[v0_,t_] := v0 t - ˝ g t^2
If we wanted to evaluate the expression at v0 = 90 feet/second
and t = 3 seconds, we could type:
h[90 ft/s, 3 s] ; which should result in
units of ft.
Mathematica is not really working with the units. It is actually
treating "ft" and "s" as a symbol which has not been defined.
This will work fine for cases involving calculations, however
it can result in problems for cases such as plotting functions.
Mathematica does not know what to do with these undefined symbols
when it tries to plot the function.
Exact Answers
Mathematica will try to leave answers in "exact" form unless we
tell it to do otherwise. For example numbers such as 1/7, p,
etc. cannot be expressed exactly using any finite number of decimal
digits. Mathematica will leave these numbers as 1/7, or p unless
we tell it to approximate the numbers.
For example if we want to take the square root of 12 we would type: Sqrt[12] , Mathematica will display 2 Sqrt[3] because the square root of 3 cannot be expressed exactly.
One way to tell Mathematica to approximate a number is to include
a decimal point:
For example: Sqrt[12.] will give us an approximation of the
square root: 3.641.
We can also instruct the program to give some fixed number of
decimal points:
N[number, precision]
N[Sqrt[12], 10] will display the square root
to 10 decimal places.
MATHEMATICA
Logical If:
In many cases we may wish to have a command issued based on some
conditions which must be satisfied. This is done by a logical If
statement. What we need to do is to establish the conditions of
the If statement. For example say that we wish to have our solution
be equal to 2 if x<2 and equal to x if x>2.
If[condition, true_definition, false_definition]
Derivatives
We can use Mathematica to take the derivative of a function:
D[f ,x] Takes the derivative of a function with respect to
D(f,{x,n}) Takes the nth derivative of a function with respect to x
Integrals
We can use Mathematica to take either definite or indefinite
integrals:
Indefinite:
Integrate[f,x] Integrates a function with respect to x.
say we have a function x^(-3/2)
Definite:
Integrate[f,{x,0,3}] Integrates a function with respect to x
and evaluates the integral from the limits of x=0 to x=3.
If we have a function of two variables x and y:
Integrate[f,{x,-1,4},{y,2,4}] Integrates a function with respect
to x and y and then evaluates the function from the limits x=-1
to x=4 and y=2 to y=4.
Lists (Page 60 - 71 of text)
We will often deal with lists of numbers in engineering problems.
Lists are designated with the curly brackets and take the form:
{ element1, element2, element3,…..,elementn}
For example we may have a list of numbers:
{10, 3, 11, 4, 10}
We have already dealt with lists in an indirect way with some of
the commands that we have dealt with. In many cases a command
may have a list such as
Plot[h[x], {x, 0, 2}]
the list {x, 0, 2} tells Mathematica to evaluate the function h[x]
from x = 0 to x=2.
In many cases we may wish to evaluate a function at a number
of points that follow some given pattern. This can be very easy
with the use of the Table function.
The Table function takes the following form:
Table[expression, {variable, min, max, step}]
TableForm
For example say that our function is 3x2 +1 and we want to evaluate
this value from x = 1 to x=35 in steps of 2
We could say
fx = 3 x^2 +1
Table[ fx , {x , 1, 35, 2}]
This will create a list with our results.
In many situations we may want to read a list of data which
contains some output directly into Mathematica:
ReadList [file_name,type]
the type of file can be Number, Word, or String. The most common
case will probably be a list of numbers with output from another
program. The numbers will be read into the program as a list of
numbers.
Matrices:
The most common type of list that we may deal with is matrices in
Mathematica which are essentially nested lists of numbers.
For example: consider the following matrix
A = {{10, 11, 9}, {0, 2, 12}, {1, 3, 8}}
There are a number of matrix functions built in to Mathematica.
Refer to Table 4.1 on page 69 of your text for a list of some
of the functions:
If we wish to multiply two matrices we simply use the following
form:
A.B (place a period between the matrices)
Inverse[A] takes the inverse of A
A very important feature of how Mathematica deals with vectors
has to do with how we specify the operation. If we want to
multiply a vector by a matrix, it will assume that the vector
is oriented in a manner that the operation can be made. For
example considering the matrix A above (3x3), if we were to
define a vector C
C={1, 5, 3}
If we specify C.A, the program will assume that C is a row vector.
If instead we type A.C, the program will assume that C is a column
vector.
Plotting In Mathematica (Page 80 - 93 in Text)
We can plot extremely complex function using Mathematica. We have
already discussed a 2 dimensional plotting a little in past lectures.
For two dimensional plotting we use the Plot command:
Plot[func, {x, xmin, xmax},
option -> option_details,
option -> option_details, ...]
There are numerous options available to play with the format of
the plot:
AspectRatio -> number
PlotRange -> {ymin,ymax} , note that the range of x values are
controlled by the plot command.
Frame -> true Places a frame around the graph
FrameLabel -> {bottom_label, left_label,top_label,right_label}
Three Dimensional Plotting
Plotting in 3-D is relatively simple. Use the Plot3D command
which is similar to the Plot command already discussed:
if we have a function with two variables,
Plot3D[func, {x, xmin, xmax},{y, ymin, ymax}
option -> option_details,
option -> option_details, ...]
Say we have a function (x/(y+1))sin2xy over the range 0 {x,y,z} x,y,z are numbers similar
to AspectRatio for 2D
ViewPoint -> {x,y,z} defines the direction that you
are looking at the plot. It is probably easiest to define the
viewpoint with the use of the viewpoint selector which you use
as you define the Plot3D command.
MATHEMATICA
Lists:
Reading lists from files: We discussed reading lists from files a
few lectures ago. This weeks lab will require you to read a list
from a file:
temp1=ReadList["c:\users\lab10\lab10.txt,Numbers]
The above expression will read the numbers from the file located
in C:\users\lab10\lab10.txt into a list. The numbers will be read
into a list named temp1. You may sometimes have a problem with
reading from specific directories and only be able to read from
the "root" directory of a disk.
Readlist["c:\filename",Table[Number,{n}]] will read the data from
the file c:\filename into an array with "n" elements in each column.
Recall that we can pull out a given element from the list temp1 by
later typing:
temp1[[n]], where n is the element that we wish to evaluate.
temp1 is the name of the list we are dealing with.
In order to determine the length of a list we can use the Length
command:
Length[temp1] (will return the number of elements in the list)
In many cases we may be dealing with arrays which may be either
one or two dimensional:
Array[f,n] is for a one dimensional array f, which has 3 elements
Array[f,{n1,n2}] is for a two dimensional array named f, which is
n1 x n2
f[3,2] = 10 sets the 3,2 element of array f equal to 10.
f[2,1] returns the element from the second row and 1 column of
array f.
Solving systems of Equations: (Page 116-122)
Mathematica has the ability to solve systems of equations. The
command takes the following form:
Solve[eqns,var]
the command attempts to solve an equation or system of equations
for the variable "var".
For example say we have two functions:
f1=5+x
f2=1+3x
We have a system in which the smaller of the above expression
controls our capacity. Say we want to get a graph of the expressions
only showing the controlling regions.
x /. Solve[f1==f2,x][[1]]
the operator /. Extracts results from the Solve command. The Solve
command essentially gives a "list of results" {{x -> #}}.
Using the /. replaces the solution with the term x. The [[1]]
from the above expression takes the solution out of a list form.
The resulting solution becomes x = 2
We can then use this to define a new variable x2 = x.
If we then make a plot of f1 from x,x2,5
and a plot of f2 from x = 0,x2
Give names to your plots so that you can later superimpose them:
plot1 = Plot[f1,{x,x2,5}]
plot2 = Plot[f2,{x,0,x2}]
Show[plot1,plot2]
Statistical Data: (page 108 of text)
Invoking the statistical functions requires you to load the
statistical library by typing:
<