SCCC Mathematica Tutorial, © 2007-2020, Seattle Central Community College Math Dept., contact: Greg.Langkamp@seattlecolleges.eduVersion 1.9/ March 2020
Important!
Click "Make your own Copy" in the black ribbon above to make this document interactive. You will also need to be logged into Wolfram Cloud
Click "Make your own Copy" in the black ribbon above to make this document interactive. You will also need to be logged into Wolfram Cloud
Lesson 4 General Functions
Lesson 4 General Functions
4.1 How to define a function in Mathematica.
4.1 How to define a function in Mathematica.
Clear[f,t,x]
In this section you will learn how to define your own functions in Mathematica. As a first example, we will consider the linear function f(x) = 5 x + 13. The Mathematica command to define this function is . The (referred to as “blank”) on the left-hand side is required. Remember to put a on the left-hand side, but not on the right-hand side, of your definition. Also note that we have used := instead of just =.
f[x_]:=5x+13
_
_
◼
Define the function . Before you evaluate the input cell notice that the function name is colored blue. This indicates that this function name has not yet been used. As soon as you evaluate the input the f will turn black which will be a signal to you that the function f now has a definition. Later when we clear the function f, it will turn back to blue.
f(x)=5x+13
f[x_]:=5x+13
◼
Enter f[x] to check that the function was defined properly.
f[x]
You can now use your function freely in any mathematical expression. Just remember to keep using square brackets.
◼
Evaluate the function f when .
x=5
f[5]
Function names can be more than one letter and any variable can be used to make the definition.
◼
Define the function . Evaluate
speed(t)=200+5t
2
t
speed(4.5)
speed[t_]:=200t^2+5t
speed[4.5]
Redefining a function. If you give f a new definition, the new definition will replace the former one.
f[x_]:=x*Sin[x]+7
f[x]
Clearing a function. The same Clear function that we used to clear variables works for functions.
◼
Clear the function f. Note that as soon as the next line is evaluated, f is colored blue again since it is now free from any definition.
Clear[f]
◼
Enter f[x] to confirm that the function is undefined.
f[x]
Exercise 4.1 A
Define the function -5 and then evaluate .
g(x)=
3
x
2
x
g(3.5)
Answer to Exercise 4.1A
g[x_]:=x^3-5x^2
g[3.5]
Exercise 4.1 B
Define the function and then evaluate . Remember to use Sin[x] for sin(x).
f(x)=
sin(x)
x
f
π
2
Answer to Exercise 4.1B
f[x_]:=Sin[x]/x
f[π/2]
4.2 Working with functions.
4.2 Working with functions.
Clear[f,g,h,m,x]
In this section we will present a few examples of how you can manipulate functions that you have defined in Mathematica.
Evaluating a function for a symbolic input.
◼
Let's begin by defining the function .
f(x)=+2x+5
2
x
f[x_]:=x^2+2x+5
◼
Evaluate the function when .
f
x=2b+7
f[2b+7]
Mathematica has substituted the value for , but it has not expanded the resulting expression out. The following input does both operations in one step.
x
Expand[f[2b+7]]
◼
Evaluate .
f(x-1)
Expand[f[x-1]]
Composition of functions.
If we define two or more functions we can combine then by arithmetic operations and/or by composition to define new functions. Mathematica allows us to do this easily. We begin by defining three functions.
If we define two or more functions we can combine then by arithmetic operations and/or by composition to define new functions. Mathematica allows us to do this easily. We begin by defining three functions.
f[x_]:=
x
g[x_]:=x+2
h[x_]:=1/x
◼
Find the composition .
f(h(x))
f[h[x]]
◼
Find the composition .
g(f(x))
g[f[x]]
◼
Define a new function as the composition . Then evaluate
m
m(x)=f(g(h(x)))
m
1
4
m[x_]:=f[g[h[x]]]
m[x]
m[1/4]
Computing the difference quotient.You may recall that for any function the difference quotient is defined as: . Depending on the function, computing and simplifying this expression can be considerable work by hand. Let's give this problem to Mathematica.
y=f(x)
f(x+h)-f(x)
h
◼
Find and simplify the difference quotient for the function .
f(x)=+3x
2
x
f[x_]:=x^2+3x
Simplify[(f[x+h]-f[x])/h]
◼
Find and simplify the difference quotient for the function .
g(x)=
3
x
g[x_]:=x^3
Simplify[(g[x+h]-g[x])/h]
Exercise 4.2 A
Evaluate and expand where is the function
f(5-m)
f
f(t)=-2t+4
3
t
Clear[f,m,t]
Answer to Exercise 4.2 A
Clear[f,m,t]
Exercise 4.2 B
Answer to Exercise 4.2 B
Exercise 4.2 C
Answer to Exercise 4.2 C
We can use the Table command to create a table of values for one or more functions.
The basic syntax is: Table[ { x, f[x] } , { x, start, end, increment} ]
The basic syntax is: Table[ { x, f[x] } , { x, start, end, increment} ]
For a vertical display, we can apply either the TableForm or Grid command.
If you want more of a spreadsheet look you could try the following.
The Table command allows us to evaluate more than one function. The next command produces the square, cube, fourth and fifth powers of the integers from 1 to 20.
Exercise 4.3 A
Answer to Exercise 4.3A
Exercise 4.3 B
Answer to Exercise 4.3B
Exercise 4.3 C
Answer to Exercise 4.3C