0% found this document useful (0 votes)
18 views

Mathematica Workshop

The document discusses a Mathematica workshop. It introduces Mathematica as a symbolic programming language and computational engine. It then covers various Mathematica topics like variables, functions, conditionals, loops, plotting and more through examples and exercises.

Uploaded by

1nSp1red
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Mathematica Workshop

The document discusses a Mathematica workshop. It introduces Mathematica as a symbolic programming language and computational engine. It then covers various Mathematica topics like variables, functions, conditionals, loops, plotting and more through examples and exercises.

Uploaded by

1nSp1red
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Mathematica Workshop

What is Mathematica?

High Powered Symbolic Programming Language /


Computational Engine
- Superpowered Class Pad
- Allows for representation of complex mathematical ideas and powerful computation
- Massive library and array of features

Alternatives
- Python, Matlab, etc.
Uses of Mathematica
Today’s Workshop
- Graphing and Plotting
1. Basics of Mathematica
2. Variables and Functions
3. Conditions and Loops
- Creating and Implementing Algorithms 4. Lists and other data structures in
Mathematica
5. Advanced Plotting
- Computation 6. Useful Calculus
7. Exercises and Experimentation

- Communicating ideas

- Data Science and Machine Learning


Getting Started
Cells
Basic Computation
Basic Operators:

+, -, *, /

Ctrl+@: Square root

++: Increase value by 1

+=: Increase value by…

%: Uses value of last


cell
Variables
Local Variables:
- In Mathematica, they will only be able to be referenced within the cell that they are defined
- Will appear in blue text

Global Variables:
- Value is assigned permanently and will reference that value across other cells
- Will appear in black text

Notice the difference!

x =. converts back to
local variable
The /. operator
The /. operator can be used to assign a variable to a value for a particular cell

Arrow = ->

Special characters = \[name]


Functions and Arguments
A function in Mathematica is something you may reference to perform
calculations for you.
- Mathematica has thousands of in-built functions!
- Use square bracket [ ] notation
- Most functions that are predefined in Mathematica take one or more arguments

An argument is a value or expression a function references while executing


Functions with multiple arguments
Functions with multiple arguments are separated by a comma
Defining our own functions
In Mathematica we can define our own variables by using an underscore after the
variables we want the function to map
Pure functions
You can define functions to be
operations on dummy variables
using the # and & character
- # represents an arbitrary variable of a
function
- Using the & character “joins” the
function with your specified variables or
values
Working with Functions
Works just like plugging in values to other functions!
Quick actions with functions
Derivatives - can be done with prime
notation

Integrals - Uses Integral function

Plotting - 2D or 3D support
Conditionals
Unlike in other programming languages, in Mathematica conditionals are
expressed as functions

If function:
- Takes two or three arguments
- Structure: If[Condition, True, False]
- Can be used in functions
- Double = used for comparisons
Logic Operators
AND: &&
OR: ||

NOT: !
Used in logical statements to
specify additional conditions
Which Statement
Also a function
- Works like If but better when
there are multiple cases to
consider
- Used for piecewise functions
Switch Statement
Like an If statement but more
useful for calculations as it will
return a value you specify
- Can be used to plot interesting
functions
- Not limited to binary outputs
Loops
Many types of loops in Mathematica:
- For Loop
- Do Loop
- While Loop
- Nest

All used in different circumstances

All behave like functions


For Loop
Uses a dummy variable to keep track of
iterations.
- Will run a fixed number of times defined before
the loop starts running
- Every time the loop runs, the dummy variable
will “increment” until it reaches a certain
threshold
- Structure: For[Start, Condition, Increment,
Action]
- Break[] = Stop a loop early. Normally paired
with a condition
Do Loop

A bit like a simplified for loop


- Computes the first expression certain
number of times
- Increment can be controlled
While Loop
Another type of iteration - this
time with a variable duration
- Will keep running until a specified
condition is met
- Be careful your loop won’t run
forever - no easy way to break loop
while running
- Useful for specifying boundary where
loop should stop
Lists and Arrays
Another data type in Mathematica,
can hold multiple values
- Useful for vectors
- Can perform an operation on multiple
elements of a list simultaneously
- Basic functions associated with lists:
- First[list]
- Last[list]
- Min[list]
- Max[list]
- Sort[list]
- Take[list, {first, last, increment}]
- Use indexes to reference individual
elements
Indexes
Indexes start from 1 in Mathematica and end with exactly the length of the list.

Method to reference individual elements of a list


Special Types of Lists
Range[n]: All the natural numbers up to
and including n

Range[start, end, increment]: All the


integers starting at start, ending at end,
and increasing by increment

Length[list]: Find the length of a list


Lists as sets
Special functions exist if you want to
use your list as a set
- Standard operations on lists generally only
work if the lists are the same size
- Union[list1, list2]
- Join[list1, list2]
- Intersection[list1, list2]
Matrices and Tables
Lists within lists - can be
expressed in different forms.
Singular data type

Operations and functions


performed are dynamic
Uses of Matrices and Tables
Storing multiple values

Plotting more than one graph

Vector / Matrix operations

Flatten[ ] = Convert back to one


dimensional list / array
Array Plots
Interesting (but kinda useless) visualisation of data
Random Numbers
Can be useful for statistical
analysis

Various inbuilt random


generators
More advanced plotting
2D Plotting
- Lots of additional
parameters to change
- PlotStyle = {Colour, Style}
- Frame, FrameStyle
- Axes Origin
- Filling
- GridLines
- Can plot more than one
function on single graph
- PlotLegends
More advanced plotting
Animating
- Adds a Slider to a Plot
- Can run automatically or not
- GraphicsGrid - Display multiple graphs in a table
More advanced plotting

Region Plotting
- Much easier to display regions
- 2D or 3D support

Parametric Plotting
- Great for parametric equations
- Animate to add slider
More advanced plotting

3D plots
- Some new parameters you can add
- MeshStyle, ColorFunction, PlotLegends
More advanced plotting

RegionFunction
- Specifies a region (inequality) to leave out of the graph
More advanced plotting

Plotting Data Sets


- ListPlot can plot values of a list or
table
- Great for data visualisation
- 2D or 3D
More advanced plotting

Interpolation
- Interpolation Order - automatically
interpolates data
- Creates a smooth curve
- Can Animate Point plots!
More advanced plotting

Exporting Plots
- You can export plots in many
different file formats
- Animated Plots can be exported as a
video file!
Calculus and Analysis
- Differentiation
- D function
- Prime Notation
- Differential operators in Palette
- Integration
- Integrate Function
- Operator in Pallette
- Numerical Integration
Calculus and Analysis

- Differentiation Equations
- Prime notation
- Uses Comparison ==
- List for initial conditions
- Plot multiple initial conditions!
Exercises
1. Using a conditional, create a function that will display “a divides x” if x (mod a) = 0, and “a does not
divide x” otherwise (useful functions: Mod)

2. Using a loop, create a list that displays the first 30 Fibonacci numbers. Create a function that tests
whether an integer is a Fibonacci number (useful functions: Append, MemberQ)

3. Write a program that takes two vectors in 3D space as input and plots the plane containing those
two vectors as output. Also plot a line in this plane (useful functions: Cross, Graphics3D)

4. Write a function that computes and plots sample mean distribution, using random integers from 1 to
10.

5. Create a 3D graph of a function of your choice over a certain region. Also plot a tangent vector to
the function at a particular point

You might also like