6

I want to evaluate f[x,y]=-4 x + x^2 - 4 y - y^2 at points (1,-2); (2,-3); (3,-2); (2,-1).

I tried using Outer but for some reason it does not give me actual values. Help.

4 Answers 4

10

Remember that Mathematica has a specific way of defining functions. In your case it would be f[x_,y_]:=-4 x + x^2 - 4 y - y^2. Then you could simply use f[1,-2] etc.

3
  • I tried that way, but the output I get is a little graph of that function and values I used next to it.
    – Koba
    Apr 26, 2012 at 1:54
  • Really? It should just return a number. Did you remember the underscores in f[x_,y_], and the ":=" instead of "="?
    – arshajii
    Apr 26, 2012 at 3:08
  • I tried your way today and it worked. Do not know what was wrong or different yesterday. Thanks.
    – Koba
    Apr 26, 2012 at 16:16
5

Perhaps consider using a 'pure' function. For example:

-4 #1 + #1^2 - 4*#2 - #2^2 & @@@ {{1, -2}, {2, -3}, {3, -2}, {2, -1}}

gives

{1, -1, 1, -1}

2
  • 1
    @Dostre. You probably are already aware of this, but another possibility is replacement rules. For example: -4 x + x^2 - 4 y - y^2 /. Thread[{x, y} -> #] & /@ {{1, -2}, {2, -3}, {3, -2}, {2, -1}} or, for an individual case, -4 x + x^2 - 4 y - y^2 /. {x -> 1, y -> -2}.
    – 681234
    Apr 27, 2012 at 14:34
  • Yep thanks for pointing this out. Now, I can choose the way I consider convenient from the bunch posted here. This makes this thread more complete. Thanks again
    – Koba
    Apr 27, 2012 at 14:44
4

Here are some variations on the theme:

Clear[f]

f[{x_, y_}] := -4 x + x^2 - 4 y - y^2
points = {{1, -2}, {2, -3}, {3, -2}, {2, -1}};
Map[f, points]

{1, -1, 1, -1}

f[x_, y_] := -4 x + x^2 - 4 y - y^2
f[1, -2]

1

f = Function[{x, y}, -4 x + x^2 - 4 y - y^2];
f[1, -2]

1

3

You can use functions like Apply and Map to evaluate a function in a list of points, for example

f[x_, y_] := -4 x + x^2 - 4 y - y^2 

pts =  {{1, -2}, {2, -3}, {3, -2}, {2, -1}};

Apply[f, pts, {1}]

(* out: {1, -1, 1, -1} *)

or using @@@ as a short hand for Apply[ ...., {1}]

f @@@ pts
0

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.