Functions

by
K. D.  Stroyan
University of Iowa

copyright 1997 by Academic Press, Inc. - All rights reserved.

Notebook Overview

A function in general is some well-defined procedure that accepts inputs from a certain "domain" and gives unique "outputs."   Mathematica is a function-oriented programming language, so sub-programs are often defined as higher-order functions like the Plot[.] function that draws graphs.

The simplest functions arise as real-variable formulas.  The purpose of this NoteBook is to show you how to define basic high school math functions with Mathematica.  (These are important, they are just not the only kind of functions.)

References to the Text

This NoteBook goes with Chapter 28 of the text on High School Review.

Step-by-step

Real-Valued Functions in Mathematica &
Exercise 28.3.2

The formula for the area of a circle is

A = π (r^2)

where A is area and r the radius of the circle.

Given some particular value for the radius of a circle, the "area-finding machine" would square that value, multiply it by π, and out would pop A.  We define this function in Mathematica as follows: (Locate the cursor in the input cell below, click and press <Enter> on the number pad or <shift> + <Return> to teach Mathematica this function now.)

Clear[A, r]; A[r_] := π (r^2); A[r]

Notice that there is an underscore (_) next to the r on the left hand side.  The underscore tells Mathematica that the letter  r  is the input variable.  The function takes as input some object that we have called r just to give it a name, squares it and multiplies the result by  π.   π is an assigned parameter, not a variable.

Now use your function to compute the area of a circle of radius 5.  (<Enter> the next cell.)

A[5]

The function has taken as input the value 5, squared it to get 25 and then multiplied the result by  π to return to us 25 π.  <Enter> the next computation for a numerical approximation.

N[A[5]]

Mathematica also works with symbolic inputs.  Enter the next computation to see an example.

\(Clear[x]; 
A[x]\)

What do you think the next computation will give?

N[A[x]]

What do you think the next computation will give?

\(x\  = \ 5.3; 
N[A[x]]\)

We will see that it is important to enter symbolic expressions in functions.  Mathematica can do this too.   Enter the following cell.

\(Clear[r, dr]; 
A[r + dr]\)

Mathematica can also build up symbolic expressions such as the one in the next cell.  Enter it.

error[r_, dr_] := A[r + dr] - A[r] - (2 π r) dr; error[r, dr]

The error formula we have just defined can now be used in numerical computations as follows:

r = 2dr = 0.01N[error[r, dr]]

Exercise

In Exercise 28.3.2 you found the surface area,  s,  of a sphere as a function of its volume, v.  Define your function in Mathematica in the next input cell.  (Remember the underscore.)

s[v ?? ??

Use your Mathematica function to find the surface area of a sphere of volume  v = 4 and a volume of 5.6.

s[4]N[s[5.6]]

Combinations of Functions
(help with Exercise 28.7.4)

Exercise 28.7.4 in the text defines the following functions: (Enter the definitions now.)

\(Clear[h, q, p, t]; 
h[t_] := t\^2 + 1; 

    \(q[t_] := 7\ E\^t; 
p[t_] := t\/\(t + 3\); \)\)

Part  (1)  of the exercise asks for  h[t] + q[t].  The following command cell will compute h[t] + p[t].  Alter it so that you can check your answer for h[t] + q[t].

h[t] + p[t]

Symbolic expressions can also be computed:

\(Clear[t, dt]; 
p[t + dt]\)

You can continue this way to check your work in the exercise.

Exercises 28.7.1, 2, 3 - See the program
SymbIncr
in the Chapter 5 Folder