Off[General::spell1];

Building functions

Mathematica has a lot of knowledge about elementary mathematics built in.  
It can draw graphs, solve systems of equations, differentiate and integrate.

Often though we are faced with problems for which there is no built-in solution.  
For example, there is no built-in Mathematica function that calculates areas of triangles.  
In this Notebook we shall tell Mathematica how to do that.  

We shall build a function that does what we want — calculates areas of triangles — from the functions that we have — Mathematica’s built-in functions such as addition, multiplication and powers.

We are going to give our own meanings to several words.  
It’s a good idea to start by clearing any meanings that the user may have attached to those words in the current Mathematica session.

Clear[length,segment,point,x,y,
      perimeter,semiperimeter,triangle,area]

Length

A good way to tackle any mathematical problem, especially one that you can’t get started on, is to work on an easier, but similar, problem.  The natural simpler analogue of calculating areas of triangles is calculating lengths of line segments.  Here is our definition of a function to do this:

length[segment[A_,B_]]:= Sqrt[(x[B]-x[A])^2 +(y[B]-y[A])^2]

When you enter that definition you are telling Mathematica what you might tell a person: 
	to get the length of the segment between points A and B calculate the square root of the sum of the square of the difference of the x-coordinates of the two points and the square of the difference of their y-coordinates.   
Actually the formula is probably easier to take in than the words!

There are no built-in functions to get the x and y coordinates of a point, so we shall make our own.  
Here are the two definitions:

x[point[a1_,a2_]]:=a1;
y[point[a1_,a2_]]:=a2

When you define functions it’s important to test them.  
The answers to the following questions should be what you expect.

x[point[3,4]]

y[point[3,4]]

length[segment[point[0,0],point[3,4]]]

length[segment[point[0,0],point[-3,4]]]

length[segment[point[1,1],point[4,5]]]==$$$

length[segment[point[1,2],point[4,$$$]]]==5

You may have wondered why we used the words point and segment in our definitions.   
They’re not  essential, but saying point[3,4], for example, rather than just {3,4} helps us to keep in mind what we’re talking about.

Perimeter

The perimeter of a triangle is another quantity which is a little easier to describe than the area:

perimeter[triangle[A_,B_,C_]]:=length[segment[A,B]]+
   length[segment[B,C]]+length[segment[C,A]]

perimeter[triangle[point[0,0],point[1,0],point[1,1]]]

If you want an (inexact) decimal answer you should apply the built-in Mathematica function N:

N[perimeter[triangle[point[0,0],point[1,0],point[1,1]]]]

?N

Area

We are now in a good position to use a formula that gives the area of a triangle in terms of the lengths of its sides.  Of course everyone remembers:
                             area=(Sqrt[s(s-a)(s-b)(s-c)])

We shall adapt that to get a function that takes a triangle with its three vertices as input and returns the area as output.

Recall that the s in the formula is the so-called semi-perimeter of the triangle. That’s easy to define:

semiperimeter[triangle[A_,B_,C_]]:=
     perimeter[triangle[A,B,C]]/2

After all this its easy to define our area function.

area[triangle[A_,B_,C_]]:= Sqrt[
(semiperimeter[triangle[A,B,C]]*
(semiperimeter[triangle[A,B,C]]-length[segment[A,B]])*
(semiperimeter[triangle[A,B,C]]-length[segment[B,C]])*
(semiperimeter[triangle[A,B,C]]-length[segment[C,A]]))]

We could have made this definition much shorter by omitting the words triangle, semiperimeter and segment and using letters instead of words.  But then its meaning would be less obvious.

Let’s test that on some examples:

area[triangle[point[0,0],point[1,0],point[1,1]]]

area[triangle[point[0,1],point[1,0],point[1,1]]]

area[triangle[point[0,0],point[1,0],point[a,2]]]==$$$

area[triangle[point[0,0],point[$$$,0],point[a,2]]]==4

Note

In this notebook, the active cells (the ones which are meaningful to Mathematica )  have been marked with the keyword question  or definition  or command.   A question asks Mathematica for some information; a definition conveys some information to Mathematica;  and a command tells Mathematica to alter its current state.

You can see the keywords attached to the cells by turning on the toggle Show Cell Tags on the Cell menu.

Mathematica does not automatically simplify complex algebraic expressions.  To avoid complications the following initialization cell was entered when you opened this Notebook.  Its effect is to ensure that all output from Mathematica  is automatically simplified.

$Post=Simplify;

Slogans

A program tells a person or a machine 
how to do something 
by describing what must be done
in terms of things it already knows how to do.

To make a function with properties we would like it to have, 
we build it from functions we already have, 
using the ways that are available for putting functions together.

A piece of text that describes how to build a function out of given components is called an algorithm.  
Many different algorithms may describe the same function.

Programming is the activity of designing algorithms.

Mathematica is a programming language.