Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Currently trying to use elements from a matrix in defining a function however having some trouble. This is a somewhat simplified version of the problem.

Say I have the matrix

m={{Sin[x],Cos[x]},{Tan[x],ArcTan[x]}}

And I want to define a function in terms of the first element. How would I go about doing this ? Have tried

f[x_]:=m[[1,1,1]]

This seems to work until I try and evaluate it for a value of x.Then it just returns Sin[x].

Any help would be much appreciated.

share|improve this question

closed as off-topic by Louis, m_goldberg, MarcoB, J. M. May 23 at 16:09

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Louis, m_goldberg, MarcoB, J. M.
If this question can be reworded to fit the rules in the help center, please edit the question.

    
m = {{Sin[x], Cos[X]}, {Tan[x], ArcTan[x]}}; f[x_] := Evaluate@m[[1, 1]]; f[Pi] – Dr. belisarius Jan 9 '14 at 17:15
    
Thanks, now got it working. Although I needed to use m[[1,1,1]] to get the first element from the matrix. m[[1,1]] returns the first row. – user11642 Jan 9 '14 at 17:28
    
I now understand why. It is because I have it in matrix form. – user11642 Jan 9 '14 at 17:35

You need to use Set instead of SetDelayed. Please see here for an explanation of the difference: What is the difference between Set and SetDelayed?

First make sure that x has no value assigned. Then you can do

In[1]:= m = {{Sin[x], Cos[x]}, {Tan[x], ArcTan[x]}}
Out[1]= {{Sin[x], Cos[x]}, {Tan[x], ArcTan[x]}}

In[2]:= f[x_] = m[[1, 1]]
Out[2]= Sin[x]

In[3]:= f[1]
Out[3]= Sin[1]
share|improve this answer

One approach is this:

m = {{Sin[x], Cos[x]}, {Tan[x], ArcTan[x]}}
f[y_] := m[[1, 1]] /. x -> y

f[a]  -> Sin[a]

another:

m[x_] = {{Sin[x], Cos[x]}, {Tan[x], ArcTan[x]}}
f[y_] := m[y][[1, 1]] 

f[b]  -> Sin[b]
share|improve this answer

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