Introduction to Mathematica for High School Math (for Students and Teachers) #3
Author
Ruth Dover
Title
Introduction to Mathematica for High School Math (for Students and Teachers) #3
Description
Introduction to Mathematica for High School Math
Category
Educational Materials
Keywords
Mathematics, education
URL
http://www.notebookarchive.org/2021-09-6h1x6h6/
DOI
https://notebookarchive.org/2021-09-6h1x6h6
Date Added
2021-09-14
Date Last Modified
2021-09-14
File Size
21.22 kilobytes
Supplements
Rights
Redistribution rights reserved



Tutorial 3: Define
Tutorial 3: Define
R. Dover, IMSA
Defining Variables
Defining Variables
Defining a variable should be about as you would expect. Define both and , using a semicolon to separate two inputs within one line. With the semicolon at the end, no output is shown:
a
b
a=4;b=2;
3a+
2
b
Want to know if ? This requires a double equal sign:
a=b
ab
Or if is equal to itself?
a
aa
But now we will redefine the value of (this overwrites the old value, but it is better practice to clear the old value first):
b
b=5
3
b
Short memory? Or not sure if a value is still in Mathematica's® memory? Just ask:
b
Clear[a,b]
Defining Functions
Defining Functions
To define a function, the Wolfram Language™ has some special notation using an underscore and a colon. (There is no space between and .) This is to show a "delayed assignment." That is, Mathematica reads the rule, but only evaluates the function when asked to do so. Watch the notation carefully!
:
=
f[x_]:=+5x-2
3
x
After defining the function, evaluation works as expected—with square brackets:
f[3]
f[5]+f[-2]
Define a value for and then evaluate the following:
a
f[+1]
2
a
f[2+3]
f[f[f[x]]]
Define a new function and evaluate it with several values and expressions.
g
When finished, clear both and :
f
g
Clear[f,g,a]
Substituting
Substituting
You may also substitute numbers or variables into expressions. First, create an expression and call it "poly." (Note the space between the and in the middle term. The space indicates multiplication of the two variables and as opposed to a single variable named , similar to our name , which is one variable with a four-letter name. Spaces are frequently used in the Wolfram Language simply to help make the mathematical expressions easier to read. In this case, it is necessary.):
x
y
x
y
xy
poly
poly=+4xy+5
3
x
2
y
To make a substitution, we use the not-so-obvious notation along with an arrow (from the palette or created by a "–" and a ">"):
/.
poly/.x2
Now substitute into this poly. Mathematica does not store these values of and .
y=5
x
y
To substitute and simultaneously, we can use curly braces to create a list of values:
x=3
y=−1
poly/.{x3,y-1}
Now substitute and simultaneously.
x=a
y=b
Then substitute +2 for and −2y for .
2
x
x
3
y
y
When we have finished using poly, it is important to clear it from Mathematica's memory. You may wish to reuse the name later. Also, you do not want the program to assume something you had forgotten about. (This happens particularly with function names. happens to be a very popular choice!):
f
Clear[poly]


Cite this as: Ruth Dover, "Introduction to Mathematica for High School Math (for Students and Teachers) #3" from the Notebook Archive (2021), https://notebookarchive.org/2021-09-6h1x6h6

Download

