(*Introduction to Mathematica: Part I.

Mathematica is a software package that lets you do mathematics and computation,analyze data,develop algorithms,do simulation and modeling,and produce graphical displays and graphical user interfaces.This section will
introduce you to how to use Mathematica within the scope of physics laboratory experiments.In this instruction,  a command in Mathematica is given near the In[...] line (you use Shift-Enter to activate the command); the number of the In[...] line is generated automatically  upon each evaluation. Notes between (*...*) are just comments.

Installation of Mathematica on your own computer.
If you wish to have Mathematica software installed on your own computer,you can download it from NJIT
Information Services& Technology Department 
https://ist.njit.edu/mathematica-njit*)

(*Getting Started. To run Mathematica on a PC,double-click on the Mathematica icon.
Help.1) if you know the exact name command,but want to refresh which arguments it requires,use ? . E.g.,*)
?Sin

(* 2) if you approximately know the spelling,use ? with * for the unknown part,e.g.*)
?*ParametricPlot*

(*and then you click on the required command for further info*)

(*3) if you have no idea-use Help button

Note: all Mathematica commands and constants start with a Capital; your own can start with any*)

(* Ending Mathematica Session.

Exit the notebook; save it if you need

Inputting Commands.
To input a command and execute it,just type it  and press Shift-Enter”
in the keyboard.For example,to run simple calculation of 1+1
*)
1+1

%*4 (*will multiply the answer by 4; % means the previous answer*)

(*you are now ready to use Mathematica as a simple calculator. Everything is standard but a few useful extras:
Frequent type-and space-saving commands:
1)as above,  % uses the last output as input,  E.g.
In[12]:=Sin[1.]
Out[12]=0.841471
In[13]:=ArcSin[%]
Out[13]=1.

2) space-can be used instead of * for multiplication:
      In[1]:=2 2
     Out[1]=4
Do not need space if number in front of a symbol:
         In[2]:=2x
      Out[2]=2 x
(useful to keep  close to hand-written formulas)
but will keep  a symbol unchanged if the number is after without a space
In[3]:=x2,
Out[3]=x2
so, don't forget space if you intend multiplication:
            In[4]:=x 2
           Out[4]=2 x

3) ;  will not produce an output on the screen (but can work with it further!)
In[5]:=Factorial[1000];  (*N  gives approximate numerical value*)
In[6]:=N[%]
                                                                2567
Out[6]=4.023872600770938 10

Brackets.
()–as in regular math
[]–argument(s) of ANY function:Sin[x], Plot[x^3, {x,-1,1}],etc. Can be empty,e.g.
RandomInteger[] with output 0 or 1
{}–lists, e.g.{1,2,3} or {a,b,c}

Equal signs.
= -  immediate assignment (standard)
:=- delayed. E.g.: *)

r:=RandomInteger[];Table[r,{5}]

(*Compare to *)r=RandomInteger[];Table[r,{5}]

(*==-logical,e.g.Solve[x^2==4,x]*)

Solve[x^2==4,x]

Sin[x]^2+Cos[x]^2==1//Simplify

(*Inputting Vectors and (will not need immediately) Matrices.
A vector is an ordered list of numbers in curly brackets separated by comas (not spaces!). There is no difference in Mathematica between row vector and column vector. Often, we will label long vectors as "lists" , keeping the word "vectors" for physical objects in 2D and 3D. E.g. :*)

x={-1,1,2,4,6}

(*Suppose that you want to create a vector (list) of values ranging from 1 to 10. Here’s how to do it without typing each number; you can use a more universal Table or the specific Range commands*)

Table[i, {i,10}]

Range[10](*gives the same*)

(*The default
increment is 1 and so is the initial value.The increment can be specified, together with initial value, as *)

Table[i, {i,1,9,2}]

Range[1,9,2](*gives the same*)

(*Increments can be fractional or negative. The elements of the vector X can be
extracted as X[[1]],X[[2]],etc. Note double squarebrackets! For example:*)

X={3, 2, 4, 6, 20}

X[[1]]

X[[3]]

(*A matrix is a "nested" list:*)

A={{1,2,3,4},{5,6,7,8},{9,10,11,12}}

(*to get a human view use MatrixForm:*)

A//MatrixForm

(*The math operation (+,-, *, /) also can work on matrices. and  vectors. Make sure the dimenssions match! Some examples*)

A={2,3,5,10,3}

B={1,3,4,5,4}*2

A*3

A+B

A.B (*dot product*)

(*Some points should be made here.*)

A^2 //MatrixForm (*squares every element of a matrix*)

(*to multiply a square matrix by itself use MatrixPower or . (dot) product *)

A={{1,2},{2,1}}

A//MatrixForm

MatrixPower[A,2]//MatrixForm

A.A//MatrixForm (*same*)

(*Algebraic or Symbolic Computation
For conducting symbolic computations,you do not need to declare anything, but it can be  useful  to clear the variables that you plan to use in the
calculation. For example,if you want to calculate (x-y)*(x-y)^2 in Mathematica,see the below:*)

Clear[x,y];

(x-y)*(x-y)^2

(*To differentiate function of x^3 in Mathematica (D is the command for differentiation),*)

D[x^3,x]

(*Inputting Mathematical Functions
In Mathematica you will use both built-in functions and functions that you create yourself.
Built-in Functions
Mathematica has many built-in functions.These include Sqrt (for square root), Cos (for cosine),Sin (for sine),Tan
(for tangent),Log (for natural logarithm,ln),Exp (for exponential function),and ArcTan (for arctangent), as well
as more specialized mathematical functions. Mathematica also has several built-in mathematical constants,including Pi (the number π), I (the complex number i=√−1), and Infinity.  E.g.:*)
Log[Exp[2]]

Log[10,100]

Sin[Pi](*calculates exactly*)

(*or, approximately, but with very high accuracy *)pi=N[Pi,25]

Sin[pi]

(*User-Defined Functions.
You will learn two methods to define your own functions in Mathematica. See the below examples:*)

(*The 1st way, mostly useful for simple formulas: *)

Clear[f,x]; (*an immediate assignment of a function, but x, the variable, must be free to change and take various values in the future*)

f=x^2+x

(*Now we can replace x by any value, using /. *)

f/.x->2

f/.x->-2

(*the function can operate on lists (vectors):*)
f/.x->{1,3,4}

(*The 2nd way is using delayed assignement; you do not have to Clear *)

g[x_]:=x^2+x (*the underscore _ indicates a slot in which you can put anything instead of x *)

g[2]

g[Sin[x]]

g[z]

g/@ {1,2,3} (* this is how the function g is applied to a list (vector). The /@ applies g to every element of the list; note: no square brackets at this stage*)

(*For the function with multi-variables, there is nothing new in Mathematica, just write:*)

Clear[f,x,y];f=x^2+y^2 (* for the 1st way of defining a function and then replace x an y using /.  :*)

f/.{x->1,y->2}

g[x_,y_]:=x^2+y^2

g[1,2]

(*Solving Equations*)

Clear[x]; Solve[x^2-3*x+1==0,x] (*Note the double == *)

N[%,7](*gives numeriical approximation of the exact result to 7 digits of accuracy*)

(*or, if I am happy with default build-in accuracy, just use //N after  the result*)

Solve[x^2-3*x+1==0,x] //N

(*The command Solve can solve higher-degree polynomial equations,as well as many other types of equations.It can
also solve equations involving more than one variable.You should
specify  which variable(s) to solve for by giving a list of those. For example,try to type Solve(2*x-log(y)=1,y] to
solve 2x−log y=1 for y in terms of x.*)

Solve[2*x-Log[y]==1,y]

FullSimplify[%, Assumptions->x >0](*we need to tell Mathematica that x is real*)

(*You can specify more than one equation as well by giving a list of equations followed by the list of unknowns.For example:*)

Solve[{x^2-y==2,y-2*x==5},{x,y}]

%//N

(*for example, the second solution can be extracted using [[2]], as for any list*)%[[2]]

(*Performing Calculus
Differentiation
The command to differentiate symbolic expressions is D, i.e. df/dx is D{f,x]. The syntax for second derivatives is D[f,{x,2}],and for nth derivatives, D[f,{x,n}].*)

D[x^3,x]

(*or*)f=x^3; D[f,x]

D[f,{x,2}]

(*The command D can also compute partial derivatives of a functions with several variables,as in D[x*y,y],and to do multiple partials with respect to mixed variables you can use D repeatedly:*)D[D[x*y/z,x],y]

(*Integration
Mathematics can compute definite and indefinite integrals.Here is an indefinite integral:*)

Integrate[x^2,x]

(*and here is a definite integral:*)
Integrate[x^2, {x,0,1}]

(*Mathematica can also do multiple integrals.The following command computes the double integral:*)

Integrate[x^2+y^2, {x,0,1},{y,0,1}]

(*Data Analysis*)

(*We have a data of (1,3,5,7,1,6,4,12).Let ‘s try to sort the data and then find maximum,minimum,and median value among the data,followed by mean value and standard deviation.First,you have to assign variable to the data:*)
x={1,3,5,7,1,6,4,12};

Sort[x]

Max[x]

Min[x]

Median[x]

Mean[x]

%//N

StandardDeviation[x]

%//N

Length[x]

Plus  @@ x  (*sum of all elements*)

Mean[x] Length[x](*should be same*)