Mathematica

“For more than 25 years, Mathematica has defined the state of the art in technical computing and provided the principal computation environment for millions of innovators, educators, students, and others around the world.”

Wolfram Demonstration Projects  http://demonstrations.wolfram.com/

Comprehensive Documentation http://reference.wolfram.com/language/

Mathematica Notebooks (*.nb)

When Mathematica is first started, it displays an empty notebook with a blinking cursor. You can start typing right away.

1+3+2*5

Mathematica by default will interpret your text as input. Type Shift+Return (it’s the same as Shift+Enter) to make Mathematica process your input. If your keyboard has a numeric keypad, you can use its Enter key instead of Shift+Return.

Mathematica will  label  your input with In[n]:=1+3+2*5. It labels the corresponding output Out[n]=14. Labels are added automatically and they reflect the order in which the input is evaluated by the Kernel. By default, input/output pairs are grouped using rectangular cell brackets displayed in the right margin.

Input Expressions

You can also use variables or put several commands, separated by semicolons, on one line. A semicolon also has the effect of suppressing the corresponding output. To write the power as a superscript, go to Palettes->Basic Math Assistant.

x=5;  y=27; y +(x^3)

We can also write each command on a separate line (enter/return) within the same cell.

x=5
y=27;
y +(x^3)

Symbolic versus Numeric Computations

Sin[2]

If you type in Sin[2], Mathematica just returns your input unevaluated. The reason is that we have specified an exact argument and Mathematica tries to find an exact solution in this case. Since there is no simpler exact expressionf for our input, Mathematica leaves it as it is. You can tell Mathematica that the input is not an exact number by adding a decimal point. Alternatively you can also use the N[..] command to get a numeric answer. In addition, this allows to give the number of digits you want as an optional argument.

Sin[2.]

N[Sin[2],100]

Getting Started with Mathematica
	Functions
	Conditional Statements(If, Switch)
	Loops (For, While, Do)
	Control structures (Break, Return)
	Objects (Graphs, Matrices, ...)

Functions

Mathematica has a large amount of functions already built in. The arguments of a function are put in between square brackets and separated by commas. Some basic functions are given below. Note that all built in functions and variables start with capital letters. To avoid potential conflicts it is a good idea to start your own functions and variables with lower case letters.

Sqrt[x]     square root (Sqrt[x])
                         Exp[x]     exponential (e^x)
                         Log[x]     natural logarithm (Subscript[log, e] x)
                     Log[b, x]     logarithm to base b (Subscript[log, b] x)
  Sin[x], Cos[x], Tan[x]     trigonometric functions 
   ArcSin[x], ArcTan[x]     inverse trigonometric functions                                    
                         Abs[x]     absolute value
                     Round[x]    closest integer to x

Round[Sqrt[Abs[-5]]+ArcSin[1/2]]

User defined functions

We can define our own functions as follows:

f[x_]:=(x^2)+Sin[x]

In this expression the underscore represents a pattern. A single underscore will match a single expression (argument) and x is the name of the pattern (it can be used to refer to the matched expression on the right hand side). The colon tells Mathematica not to evaluate the right hand side. It will only be evaluated when you invoke the function. This is why you get no output. Now you can use your function:

f[angle]+Pi

f[2Pi]

Try to assign some value to x, then compute f[2Pi]. What do you notice? Does f use the global or the local values of x when computing f[2Pi]?

x=5;
f[2Pi]

Warning:  Do not omit the colon when defining functions!

x=5;
g[x_]=(x^2)+Sin[x]
g[2Pi]

Can you explain this result? You can look at the definition of a function by using a question mark:

?f

?g

To clear the function or variable definitions, use Clear[..]

Clear[x,f]

Plotting a function

Documentation available at http://reference.wolfram.com/language/ref/Plot.html

Consider the differential equation (dy/dx)=(y^(2/3)). A family of solutions is given by y(x)=(((((x-c)/3)))^3)

f[x_,c_]:=((x-c)/3)^3

Plot[f[x,2], {x, -4,8}]

There are a number of options that can be used with the Plot command, see the Mathematica documentation for a comprehensive list of available options and examples. For instance, AxesLabel specifies labels for the x and y axes. PlotStyle sets the style of the curve. You can fill in the space between the curve and the x-axis using the option Filling.

Plot[f[x,2],{x,-4,8},AxesLabel->{"x-axis","f[x]"}, PlotStyle->{Red, Thick}, Filling->Axis]

Plot[{f[x,0],f[x,-2],f[x,4]}, {x, -10,10} , AspectRatio->1/1]

The condition operator /;

Many functions are only valid if certain conditions are met. Mathematica can handle such situations. The Condition operator (/;) can be used to make sure a pattern is used only if a condition is met.

fun[x_,c_]:= 0 /; x ≤c
fun[x_,c_] := ((x-c)/3)^3 /; x>c

You can list all definitions associated with a function as before. In particular, this will also show the order in which the definitions are applied.

?fun

Plot[fun[x,3],{x,-5,10}]

fun2[x_,c_]:= 0 /; x ⩾c
fun2[x_,c_] := ((x-c)/3)^3 /; x<c

Plot[fun2[x,3],{x,-5,10}]

Solving Differential Equations

Example 1:  Solve the ODE  (dy/dx)=((x^2)/(1-(y^2)))

This ODE was solved using separation of variables: 
(1-(y^2))dy = (x^2)dx 
and the general solution was found (by integration) to be  
y-((y^3)/3)+c=((x^3)/3), where c is a constant parameter.

The implicit curves
 y-((y^3)/3)+c=((x^3)/3) are called the integral curves of the differential equation.

Example 2 (Initial value problem):
Solve the ODE  (dy/dx)=((x^2)/(1-(y^2))), with initial condition y(0)=-1.1

Otherwise said, we need to find the unique solution of the ODE that passes through the point ((Subscript[x, 0]),(Subscript[y, 0]))=(0,-1.1)

Given a point ((Subscript[x, 0]),(Subscript[y, 0])), to find the solution that passes through the point ((Subscript[x, 0]),(Subscript[y, 0])) means to solve the equation (Subscript[y, 0])-(((Subscript[y, 0])^3)/3)+c=(((Subscript[x, 0])^3)/3) and find the value of the parameter c.

Vector field plot

Pick any point P=((Subscript[x, 0]), (Subscript[y, 0])) and compute the tangent line at P to the solution curve passing through the point P. 
The slope of the tangent line at P is  (Subscript[m=(dy/dx)|, P])=(((Subscript[x, 0])^2)/(1-((Subscript[y, 0])^2))), 
so the equation of the tangent line at P is  y-(Subscript[y, 0]) = m(x-(Subscript[x, 0])).

VectorPlot[{vx, vy}, {x, xmin, xmax}, {y, ymin, ymax}]
generates a vector plot of the vector field as a function of x and y.

VectorPlot[{1,x^2/(1-y^2)},{x,-3,3},{y,-3,3},VectorScale->{Tiny,Small, None}]

The option VectorScale determines the length and arrowhead size of field vectors that are drawn.

VectorPlot[{1,x^2/(1-y^2)},{x,-3,3},{y,-3,3},VectorScale->{Tiny,Small, None},StreamPoints->Coarse,StreamColorFunction->Hue]

Stream Plot

StreamPlot[{vx, vy}, {x, xmin, xmax}, {y, ymin, ymax}]
generates a stream plot of the vector field as a function of x and y.

StreamPlot[{1,x^2/(1-y^2)},{x,-3,3},{y,-3,3}]

Plot the integral curves of the ODE (dy/dx)=((x^2)/(1-(y^2))), with the integral curve that passes through the point (0, -1.1) plotted in red.

StreamPlot[
{1,x^2/(1-y^2)},{x,-3,3},{y,-3,3},
StreamScale->Tiny,
StreamPoints->{{{{0,-1.1},Red},{{-2,2.6},Green},{{1.2,0.9},Magenta},  Automatic}}]