12.010 Lecture 12
(Fall, 2007)

Slide 03: Examples of basic Mathematica Features

Code (use shift-return to evaluate)

x = 3;
If[x < 4, Print["x is less than 4"], Print["x is not less than 4"]]

Numerics to arbitrary precision

Pi

N[Pi] //FullForm

N[Pi, 20]

N[2, 100]

1/7 //N //FullForm

Algebra

Expand[(a + b)^25]

Calculus

Integrate[y^2, y]

Integrate[y^n, y]

Integrate[y^-1, y]

Log[10] //N

Log[10, 20]

Integrate[Exp[-y^2/2], y]

Graphics

Plot[Sin[y], {y, 0, 2Pi}];

Plot[Sin[y], {y, 0, 2Pi}, Frame->True, Axes->False];

Notebooks

This file is a notebook.  Close it and open it with a text editor to see the raw format of a Mathematica notebook.

Formats

Open the palettes and enter an equation

(∫_π^(2π))(y^2)\[DifferentialD]y

{{2, 3, 4}, {5, 4, 7}, {8, 6, 1}}

{{2, 3, 4}, {5, 4, 7}, {8, 6, 1}} //TableForm

{{2, 3, 4}, {5, 4, 7}, {8, 6, 1}} //FullForm

2 + sx //FullForm

Plot[y^2, {y, 2, 4}] //FullForm

BaseForm[1934, 3]

BaseForm[1934, 16]

BaseForm[1934 + 10, 25]

Slide 08: Variable types

Variable types

Head[5]

Head[5/2]

Head[4/2]

Head[3.0]

Head[3.0 + 5 I]

Head[rx]

Head[Pi]

Head[N[Pi, 50]]

n1 = 11111111111111111111111111111111111111111111111111111111111;

Head[n1]

Head["this is a string"]

Slide 09: Examine variable behavior with =

y=2 ; x =4;
x= y  (* This will assign the value of y to x*)

x   (* Check the value of x *)

y = 4  (* Reassign the value of y *)
x  (* Value of x has not changed *)

x := y  (* This delayed assignment, x is now y *)
x

y = 6 (* Change the value of y and x changes also*)
x

Precedence (simple cases below, we would need to add parentheses to change results or to use IntergerPart)

1 + 2^3 / 4 * 5

1 + 2^3  * 5 / 4

2^3 / 4 * 5 + 1

Slide 10: Example of user Contexts to isolate variables (variables available everywhere are Global`)

Contexts (As I start here variables are Global` context)

Clear["Global`*",anotherContext`z] (*Add this command to clear all variables used so far *)
Context[z]
z=4

BeginPackage["anotherContext`"] (*Begin a package which has its own Context.  Variables defined in this context will be different to Global` eventhough they have the same names*)

Context[]  (* Lets me know what context am in working in*)

z  (* z has not been defined and so it is just the symbol *)

Context[z]
z=8

EndPackage[]  (*Ends the Package usage, now back to Global*)

Context[]  (* Tells me current Context *)

Context[z]

Context[anotherContext`z]

$ContextPath  (* Tells all the contexts that are available*)

$Context

Global`z   (* Lets me see the values in the differenr contexts*)
anotherContext`z

Slide 13: Examples of function use

Types in function definitions

Clear[f]
?f

f[x_Integer] := x^2  (* Here we define how f should behave when an integer argument is passed*)

f[9]

f[x_Real] := Sqrt[x]  (*This is how f behaves with a real argument*)

?f  (*Enquires about nature of function f*)

f[9.]

f["hello"]  (* Currently no information on how f should be behave when a string is passed*)

f[x_String] := StringDrop[x, -1]  (*Tells it to drop last character*)

f["hello"]

example := f  (*Here I now make a copy of f called example*)

example[9]
example[9.]
example["Nine"]

Bugs

Division

x1= 1.992;

x1/x1 //FullForm

x2 = 1.9920000000000002;

x2/x2 //FullForm

N[x2/x2, 17]

Scratch Area

f[x_]:= {2, 3}

{a, b} = f[j]

c = f[j]

Clear[a]
f[x_] := 2*x   (* Define to double x*)
g[y_] := Sin[y]
h[z_] := f[z]+g[z]

h[10]
h[a]