Mathematica in 3×60 minutes - Part II

Gergely Gyebrószki | Dept. of Applied Mechanics, Budapest University of Technology and Economics

Brackets

( ) precedence
[ ] Function arguments
{ } Lists
[[ ]] Part of list

Operators from the previous part

= Assignment
:= Delayed assignment
/. ReplaceAll
//. ReplaceRepeated
-> Rule
? Check definitions
@ Prefix function calling
// Postfix function calling
@@ Apply
@@@ Apply one level below
/@ Map
# & Pure function (single argument)
#1 #2 & Pure function (two arguments)

?N

Head[{a,b,c}]

Plus@@{a,b,c}

FullForm[a+b+c]

f/@{a,b,c}

FullForm[(Sqrt[a])]

(Sqrt[#])&/@{a,b,c}

Creating lists

Range[3,17,3]

Table[(i^2)+1,{i,1,5}]

Array[RandomReal,5]

Multi-dimensional lists (list of lists)

Table[10i+j,{i,1,3},{j,1,2}]

Array[f,{3,2}]

Array[10#1+#2&,{3,2}]

Vectors and Matrices

No special type for vectors and matrices
Vector = List
Matrix = List of Lists
MatrixForm: output modifyer
TraditionalForm

Table[10i+j,{i,1,3},{j,1,2}]//MatrixForm//TraditionalForm

Array[f,{3,2}]//MatrixForm

Regular operations work as expected

a={ax,ay,az};
b={bx,by,bz};

a-2//MatrixForm

Cross product EsccrossEsc

Cross[a,b]//MatrixForm

a\[Cross]b//MatrixForm

a*b

Multiplication with . (dot)

a.b

A=Table[2i+j-2,{i,3},{j,3}]//MatrixForm

FullForm[A]

A.b

(A = Table[2 i + j - 2, {i, 3}, {j, 3}])// MatrixForm

A+(Sqrt[A])//MatrixForm

A+3//MatrixForm

A.b//MatrixForm

b.A//MatrixForm

A

Det[A]

Transpose[A]//MatrixForm

Eigenvalues[A]

Eigenvalues[A]//N

ev=Eigenvectors[A]//N

ev[[1]]

(e^A)

MatrixExp[A]//FullSimplify

MatrixExp[A]//N

Equations and Logics

== Equal
!= Not equal
>, >=, <, <=
&& And
|| Or

x==3y-2

a==a

1==2

sol1=Solve[x==3y-2]

sol1[[1]]

(x==3y-2)/.First@sol1
%//FullSimplify

% last output

Note: Returns with lists of lists (outer list: different solutions, inner list: solution of all unknowns)

Clear[a,b,c]

sol2=Solve[a (x^2)+b x+c==0,x]

x/.sol2

sol3=Solve[{4 x-3y==8,-x+5y==10},{x,y}]

{4 x-3y==8,-x+5y==10}/.First@sol3

Equations can be assigned to symbols

myEquation=(a^2)==2x+(Sqrt[x])

FullForm[myEquation]

List@@myEquation

Left and right hand sides

myEquation

myEquation[[1]]

myEquation[[2]]

myEquation/2

#/2&[19]

#/2&/@myEquation//TraditionalForm

(x^2)+2+x+(x^3)//TraditionalForm

Inequalities

red=Reduce[x^2+y^2<1,{x,y}]

True&&False

True||False

Simplify symbolic results: Simplify or FullSimplify

(x^2)-2x y+(y^2)//Simplify

Control structures, procedural programming

C++:
if (x >0) {
 2x
} else {
 x/3
}

test[x_]:=If[x>0,2x,x/3]

test[4]
test[-4]

v=0;
For[i=0,i<10,i=i+1,
v=v+i]

v

i++ or i+=1 can also be used

v=0;
For[i=0,i<10,i++,
v=v+i;Print[v]]

v=0;
While[v<50,
v+=RandomReal[15];Print[v]]

Nest, NestList for sequences

Nest[f,x,3]

NestList[f,x,3]

3n + 1 problem

fC[n_]:=If[EvenQ[n],n/2,3n+1]

fC[4]

NestList[fC,41,5]

numbers=NestWhileList[fC,41,#≠1&]

ListPlot[numbers,Joined->True,PlotRange->All]

Modularity and scoping

With or Module for local symbols

x

With[{x=7,y=8},
(x^2)+y]

f[x0_]:=Module[{x=x0,y},
While[x>0,x=Log[x]];
y=100;
x+y
]

f[2]

-> Part III

Plot functions

f1[x_]:=(x^3)-x

f2[x_,y_]:=(x^2)+Sin[6y]+2x y

Plot[f1[x],{x,-1,1}]

Plot[#,{x,-1,1}]&/@{f1[x],((f1[x])^2)}

Plot[{f1[x],2f1[x]},{x,-1,1},PlotLegends->"Expressions"]

Plot3D[f2[x,y],{x,-1,1},{y,-1,1}]

Plot[{Exp[x],Log[x],x},{x,-3,3},PlotRange->5,PlotStyle->{Red,Green,Dashed},AspectRatio->1,AxesLabel->{"x","f(x)"}]

Options[Plot]