Group Abstract Group Abstract

Message Boards Message Boards

0
|
5K Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Defining the function "qf[x_, y_, z_, is_] := x^2 + y^2 + z^2 - 5 is"?

Posted 9 years ago

Hello. I'm relatively new to new to Mathematica and have following problem: I want to define a function using a variable which has a lot of content stored on it. For example(very simplified):

Clear[x, y, z, is, q, f];
q = x^2 + y^2 + z^2 - 5 is;
f[x_, y_, z_, is_] := q;
f[1, 1, 1, 1]

The ouput is:

-5 is + x^2 + y^2 + z^2

But that is not the output I want. I need a way to get the expression from q into the defintion of the function f. One way would be to just let the programm show me the output of q and copy that in the function. However in my case, the output is 500 lines long and therefore the notebook gets needlessly long and confusing. I hope you understand my problem and that there is an easy way to deal with it.

Thanks, Stefan

POSTED BY: Stefan Reuter
4 Replies
Posted 9 years ago

Thank you all for the quick help!

POSTED BY: Stefan Reuter
Posted 9 years ago

Try this

Clear[x, y, z, is, q, f];
q = x^2 + y^2 + z^2 - 5 is;
f[x_, y_, z_, is_] := Evaluate[q];

Now let's see what f is

?f

which shows you

Global`f
f[x_,y_,z_,is_]:=-5 is+x^2+y^2+z^2

so it appears that the contents of q have been used in the definition of f.

Try using f and see what happens

f[1, 1, 1, 1]

which gives you

-2

and that seems to be the result you are expecting.

So using Evaluate was the key.

POSTED BY: Bill Simpson

... or likewise (and this probably illustrates the problem):

Clear[x, y, z, is, q, f];
q = x^2 + y^2 + z^2 - 5 is;
f[x_, y_, z_, is_] := Evaluate@q;
f[1, 1, 1, 1]
POSTED BY: Henrik Schachner

Two solutions:

In[1]:= Clear[x, y, z, is, q, f];
q = x^2 + y^2 + z^2 - 5 is;
f[x_, y_, z_, is_] = q;
f[1, 1, 1, 1]

Out[4]= -2

and

In[5]:= Clear[x, y, z, is, q, f];
q[x_, y_, z_, is_] = x^2 + y^2 + z^2 - 5 is;
f[x_, y_, z_, is_] := q[x, y, z, is]
f[1, 1, 1, 1]

Out[8]= -2
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard