Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
    
m = {{Sin[x], Cos[X]}, {Tan[x], ArcTan[x]}}; f[x_] := Evaluate@m[[1, 1]]; f[Pi] –  belisarius Jan 9 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 at 17:28
    
I now understand why. It is because I have it in matrix form. –  user11642 Jan 9 at 17:35
add comment

2 Answers

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
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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