# How to define and use a linear function

I have the following equation $T = -800n + 12000$. I know that this is a linear function and that the two points $(0, 12000)$ and $(15,0)$ lie on the line.

When I have 2 other values of n between 0 and 15, how is it possible to determine their corresponding T-values with Mathematica.

-
Fyi, your function is affine linear (strictly linear functions are required to map 0 to 0, which yours doesn't). –  alancalvitti Sep 17 '12 at 14:37

t[n_] := -800 n + 12000

t[3]
t[7]


returns

9600
6400

This is how you define functions. Note the underscore after the parameter name ("n_"), and that you don't use the underscore in the parameter's references in the function definition.

Note: I changed your T to t. All Mathematica's symbols start with an uppercase letter, and it's a good habit to start your own symbols with a lowercase letter to avoid conflicts.

-
Thank You. That was simple, shall note this down. –  AppSensei Sep 17 '12 at 12:29
I'm really impressed you understood what the OP was trying to do... –  sebhofer Sep 17 '12 at 12:31
@sebhofer - probably because I'm a beginner myself :-) –  stevenvh Sep 17 '12 at 12:32
@sebhofer haha, I knew I had to plug some value in T. But, was just making sure. –  AppSensei Sep 17 '12 at 12:34

Although an answer has been accepted already, let me give another simple approach. You don't need to define a function just to evaluate the right hand side of your equation for different n-values. Mathematica can replace the n with numeric values simply be using the /. operator. Say you want to know the t-values for n=3 and n=7, you can write

-800 n - 12000 /. n -> {3, 7}
(*
Out[4]= {-14400, -17600}
*)


These rules and replacements are a big thing in Mathematica and are worth a deeper look, because they work on any kind of expression. You should check Replace, ReplaceAll, ReplaceRepeated, ReplaceList and Rule (and RuleDelayed).

-
With[] is a convenient alternative to replacement rules. –  Guess who it is. Sep 17 '12 at 13:40

The most straightforward approach for defining a function was illustrated by stevenvh.

halirutan's approach is also useful for substituting values into a function.

A pure function represents a third option. It becomes increasingly useful in intermediate to advanced programming when you need a function "on the fly" (i.e. you probably will use it only once and you want the definition right there in the code where it's being used so that you and the kernel don't need to search for what t[n] stands for).

-800 # + 12000 &


For a single input:

-800 # + 12000 &[3]


9600

Use Map for multiple inputs:

-800 # + 12000 &/@{3,7}


{9600, 6400}

-
+1 Since I really like that you extended your answer and explained why (#)& may look awful at the moment to a newbie but why it becomes increasingly important later. –  halirutan Sep 19 '12 at 3:43
Thanks. I myself took a long time to appreciate the usefulness of pure functions. For a while looked like some cryptic graffiti. –  David Carraher Sep 19 '12 at 4:10

For the record... no function, whether defined by SetDelayed (:=) or Set (=) expliclty or by constructing a pure function (using # and &) is needed. To wit:

   -800 {3, 7} + 12000
{9600,6400}

-
To complete this answer: this works due to the Listable attribute of Plus[] and Times[]. –  Guess who it is. Sep 18 '12 at 3:10