4
$\begingroup$

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.

$\endgroup$
1
  • 3
    $\begingroup$ Fyi, your function is affine linear (strictly linear functions are required to map 0 to 0, which yours doesn't). $\endgroup$ Sep 17, 2012 at 14:37

4 Answers 4

10
$\begingroup$
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.

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

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).

$\endgroup$
1
  • $\begingroup$ With[] is a convenient alternative to replacement rules. $\endgroup$ Sep 17, 2012 at 13:40
5
$\begingroup$

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}

$\endgroup$
2
  • 1
    $\begingroup$ +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. $\endgroup$
    – halirutan
    Sep 19, 2012 at 3:43
  • 1
    $\begingroup$ Thanks. I myself took a long time to appreciate the usefulness of pure functions. For a while looked like some cryptic graffiti. $\endgroup$
    – DavidC
    Sep 19, 2012 at 4:10
2
$\begingroup$

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}
$\endgroup$
1
  • $\begingroup$ To complete this answer: this works due to the Listable attribute of Plus[] and Times[]. $\endgroup$ Sep 18, 2012 at 3:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.