Functions as arguments
Notice that if you have a function of a single variable, Mathematica knows what you mean by:
In[18]:=
Out[18]=
Partial derivatives are also pretty straightforward:
In[19]:=
Out[19]=
Sometimes, it is useful to define a function that operates on other functions. So, for example, I could define a function to find the derivative matrix for functions from :
In[30]:=
In[32]:=
Out[32]//MatrixForm=
In[31]:=
Out[31]//MatrixForm=
(Notice that I needed to transpose the matrix to get the rows/columns to work out like we want. I used MatrixForm just because it looks cool.) However, you have to be really careful with these. For example, if I just make one minor little change when calling it:
In[34]:=
Out[34]//MatrixForm=
The problem is that, when we defined our function, we assumed that our variables would be called x and y, so any other choice of variables gets interpreted as a constant. One way to fix this would be to explicitly specify your independent variables in the definition:
In[35]:=
In[36]:=
Out[36]//MatrixForm=
This now works, though at the cost of a bit of redundancy in calling the function. (There are fancier tricks we could use to generalize this, but this is a good starting point.)
If you need to access one of the components of a vector, you can use the notation:
In[66]:=
Out[66]=
In[67]:=
Out[67]=
Problem
Define your own functions to compute the gradient (call it myGrad), divergence (call it myDiv), and curl (call it myCurl) of an arbitrary function of 3 variables. (You may not use any of the built-in functions that handle these.) Demonstrate that each of these actually work for a few different functions.
Created by Mathematica (September 14, 2004)