Exercise set 1: Introduction to Mathematica

1. Calculator
Evaluation of a cell: Shift+Enter
Assign a value to a variable: a = value
Clear value of a symbol/variable: Clear[a]
Semicolon (;) hides an output
Arithmetic functions: +, -, *, /, ^, ...
Elementary functions: Sqrt[x], Sin[x], Abs[x], Log[x], Log[base, x], ...
Constants: I, Pi, E, ...
Numerical value: N[x]
Derivative: D[f, variable]
Primitive function, integral: Integrate[f, variable], Integrate[f, {variable, lower bound, upper bound}]
Numerical value of an integral: NIntegrate[f, {variable, lower bound, upper bound}]
Transformation of expressions: Simplify[], Expand[], Factor[], ...

Exercises
1.1 Assign any real numbers from the interval [-1,1] to variables a and b. Compute their sum, product, difference and quotient.
1.2 Compute the natural logarithm of sin(exp(b))π.
1.3 Find first 50 digits of π.
1.4 Clear the values of a and b.
1.5 Find numerical value of cos(2.2) and cos(22/10) with 5 digits precision.
1.6 Compute the derivatives of: sin(x),((tg(xy))/t) wrt. x, y, t.
1.7 Find a primitive function of (1/((x^3)+1)).
1.8 Compute the integral of (1/((x^3)+1))for x ∈ [0,1].
1.9 Compute the numerical value of the integral of (Sqrt[1+cos(x)]) for x ∈ [0,1].
1.10 Factor the polynomial: -125 (x^13)+75 (x^10) y+125 (x^11) y-15 (x^7) (y^2)-75 (x^8) (y^2)+(x^4) (y^3)+15 (x^5) (y^3)+500 (x^12) (y^3)-(x^2) (y^4)-300 (x^9) (y^4)-500 (x^10) (y^4)+60 (x^6) (y^5)+300 (x^7) (y^5)-4 (x^3) (y^6)-60 (x^4) (y^6)-500 (x^11) (y^6)+4 x (y^7)+300 (x^8) (y^7)+500 (x^9) (y^7)-60 (x^5) (y^8)-300 (x^6) (y^8)+4 (x^2) (y^9)+60 (x^3) (y^9)-4 (y^10)
1.11 Simplify the expression:(1/(3 (1+x)))-((-1+2 x)/(6 (1-x+(x^2))))+(2/(3 (1+(1/3) (((-1+2 x))^2))))

2. Lists and vectors
Create a list by listing its elements: {x1, x2, x3, ...},
or using Table[f[i], {i, min, max}]
Access the i-th element of a list: a[[i]]	(Lists are indexed from 1!)
Useful functions: Append[list,element], Drop[list,index], Insert[list,element,index], Union[list1,list2], Intersection[list1,list2], Reverse[list], ...
Length of a list: Length[list]
Vector is a list, matrix is a list of lists.
Element (i, j) of a matrix A: A[[i, j]] or A[[i]][[j]]
Nice output for a matrix: MatrixForm[A]
Scalar multiple: a*A
Matrix multiplication: A.B
Other useful functions: Norm[vector], Det[matrix], ...

Exercises
2.1 List the first 10 powers of 6 and divide the second of them by 3.
2.2 Create a list called a of integers from 1 to 20.
2.3 Append -6 at the end of the list a.
2.4 Add 21 at the beginning of the list a.
2.5 Reverse the list a.
2.6 Sort the output from 2.5 in descending order.
2.7 Construct a 6x7 matrix B, such that B(i,j) = (i^j).

3. Equations and substitutions
Equations have the form leftSide == rightSide
Solving (algebraic) equations: Solve[equation, variable]
Solving equations numerically: NSolve[equation, variable]
Solving a system of linear equations: LinearSolve[A,b]	(Finds one particular solution. To find all the solutions of the system, you can use Solve[A.{x1,...,xn} == b].)
Substitutions: To an expression with a variable t substitute t = a: expr(t)/.{t->a}

Exercises
3.1 Solve for x: (Sqrt[x]) = 1 - x.
3.2 Solve the system of equations: x = y+1, (y^2) = z, x+z = 7 and save the solution to a variable s.
3.3 To expression x+y+z assign the values from the solution s.
3.3 Find a solution to a system of linear equations:
		5(Subscript[x, 1])+ 3(Subscript[x, 2])= 2
		3(Subscript[x, 1]) + 2(Subscript[x, 2]) + (Subscript[x, 3]) = 0
		(Subscript[x, 2]) + 3(Subscript[x, 3]) = 5.

4. Functions, cycles, conditions
Defining a function: name[par1_, par2_, ...] := body	(The underscores “_” are necessary.)
Define the result of function: Return[expression]
Cycles: For[start, test, increment, body], While[condition, body], Do[body,{i,min, max}]
Conditions: If[test, body], If[test, body, else body]

Exercises
4.1 Write a function that takes a matrix A as an input and returns a matrix B such that B(i,j) = sin(A(j,i)).
4.2 Write a function that takes a list of integers as an input and returns the prime numbers in the list (you might want to use PrimeQ[arg]).

5. Graphs, curves, surfaces
Planar:
Graph of a one variable function: Plot[f(x),{x,min,max}]
Plot a curve given by an implicit equation: ContourPlot[f(x,y)==g(x,y),{x,min,max},{y,min,max}]
Plot a parametric curve: ParametricPlot[{x(t),y(t)},{t,min,max}]

Spatial:
Graph of a two variable function: Plot3D[f(x,y),{x,min,max},{y,min,max}]
Plot a surface given by an implicit equation: ContourPlot3D[f(x,y,z) = g(x,y,z),{x,min,max},{y,min,max},{z,min,max}]
Plot a parametric curve: ParametricPlot3D[{x(t),y(t),z(t)},{t,min,max}]
Plot a parametric surface: ParametricPlot3D[{x(t,u),y(t,u),z(t,u)},{t,min,max},{u,min,max}]

Specify the style: PlotStyle, ContourStyle
Useful options: AspectRatio->const, Axes->True/False, AxesOrigin->{0,0}, PlotRange->All,...

Geometric shapes (plot them using Graphics[] or Graphics3D[])
A point: Point[{x,y}], Point[{x,y,z}]
A line segment/polygonal chain: Line[{x1,y1},{x2,y2},...], Line[{x1,y1,z1},{x2,y2,z2},...]
A circle: Circle[{x,y},r]
An ellipse: Circle[{x,y},{a,b}]
A sphere: Sphere[{x,y,z},r]
...

Show several graphics/plots combined: Show[image1,image2,...], Show[list of images]

Exercises
5.1 Plot the implicit curve (y^2 - x^2) (x - 1) (2 x - 3) - 4 (((x^2 + y^2-2x))^2) = 0.
5.2 Plot the functions sin(xy) and cos(xy) together in one plot, use different color for each of them.
5.3 Plot 9 spheres whose centers lie on the curve  {t, (t^2),(t^3)}, t∈ (-2,2), whose radii are decreasing.