Sunday, May 10, 2015

Overview of Functions

Imagine you have a blender. To your blender, you add a couple scoops of ice cream and some milk. Then you press the buttons on the blender. Soon, you have a delicious vanilla milkshake. Next, you add strawberries and press buttons again. The result is even better than before: a strawberry milkshake.


Input
Output
Of course, strawberries aren't the only option. Suppose that, instead of strawberries, you added cocoa powder and avocado. The result is a chocolate-avocado milkshake. Or you could have added some mint leaves, for a mint milkshake. Or some peaches, for a peach milkshake.

In each of these cases, you pick the ingredient, and get a different milkshake. Whatever ingredient you pick, the result is always a milkshake. In other words, the blender took your ingredients, and returned a flavor of milkshake.

The blender is like a function.


In math, a function takes a number, and follows a set of rules to do something to that number. No matter what the number is, the function always follows the same rules.

Here's an example of a mathematical function:
f(x) = 2x + 3


Let me explain how this works. This function is being represented by the letter f. Any other letter would work, but I picked the letter f for this function. The function is going to take a number, multiply it by 2, and then add 3.

We can replace the x with any number. I'll try 4:
f(4) = 2(4) + 3 = 11


So f(4) is equal to 11.

Here's another example of a function:
g(x) = f(x + 1)


In this case, the function g is defined using another function f. We'll assume that this f is the same as the f I mentioned a little while ago. So what is g(4)? I'll work it out:
g(4) = f(4 + 1)
= f(5)
= 2(5) + 3
= 13


So g(4) = 13.



It is often helpful to visualize data from a function. One way of doing this is by listing values of f for different values of x in a table:

x f(x)
0 3
1 5
2 7
3 9
4 11
5 13
6 15

Tables can be very useful for organizing data.

Another method for visualizing data from a function is to graph it:
Parametric Plot
Min x: Max x:
Min y: Max y:

y(x) =
Other functions:
Step size:
Above, you can see a graphing application I wrote that will graph a function y(x). The whole graph is divided into a grid; at a given point on the graph, the x-value is the distance from the left side of the graph, and the value for y(x) is the distance from the bottom.

The default function being graphed is y(x) = 2x + 3.

Using a graph, we can find a value of y(x) for any value of x. To find y(3), start at the lower left corner, and move 3 units to the right. Then, move up until you get to the line. You'll have to go up 9 units to get to the line. Thus, y(3) = 9.

We can find y(1) by moving 1 unit to the right, and then moving up 5 units to the line: y(1) = 5. And we can find y(0) by starting all the way at the left, and moving up 3 units to the line.


Now how are functions useful?


Suppose you need to figure out how much to charge for a pizza (in dollars) based on the diameter (in inches). You could use the following function:
p(s) = s


So a 10" pizza would cost p(10) = $10, and a 14" pizza would cost $14. In other words, the pizza costs a dollar per inch.

But that doesn't make sense, mathematically. Nobody cares how wide the pizza is - they care how much there is. Instead of charging by the diameter, you should charge by the area. How about $0.10 per square inch? You'll need to use the formula for the area of a circle (remember that radius is half the diameter):
p(s) = π(s/2)2*0.10


In this case, a 10" pizza would cost π*25*0.10 ≈ 3.14*25*0.10 ≈ $7.85, and a 14" pizza would cost $15.39. These prices make much more sense.

You can also use the function the other way around. If you can calculate the price per square inch of a small pizza, you can determine the pizza-pricing function. Then you can use the function to calculate the optimum prices of other sizes of pizza (relative to the small pizza). Then check whether your calculated prices match the actual prices. Interestingly, large pizzas are usually cheaper (by area).

By using functions, you might actually be able to save money.

So math isn't only for programmers and physicists. It can be used by everyone else too.


Take Calculus for example...
But that'll have to wait.


New posts every month - subscribe for free!


No comments:

Post a Comment