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.

Looking at this QA, I'm trying the following - 2 functions (r in vector format) using Theta { A, 2 } functions (sorry for the inconsistent names...).

ihat = {1, 0, 0}
jhat = {0, 1, 0}
khat = {0, 0, 1}
ThetaA[t_] := 0.2 Pi Cos[50 t];
Theta2[t_] := 0.2 Pi Sin[50 t - Pi/3];
L1 = L2 = 5.0;
rAO[ThetaA_, t_] := L1 Sin[ThetaA[t]] ihat + L1 Cos[ThetaA[t]] jhat
rBA[t_] := (L2 Sin[Theta2[t]] + rAO[[1]]) ihat 
         + (L2 Cos[Theta2[t]] + rAO[[2]]) jhat

which gives me the following errors:

enter image description here

Could anyone tell me what's wrong with my functions. Thank you.

share|improve this question
2  
Looks like maybe r_(A/O) and r_(B/A) had values before you tried to turn it into a function. –  Brett Champion Feb 6 '12 at 2:10
4  
Please don't post images of code you would like others to evaluate. Reduce your question to a minimal example, and then write the code of that in your post. –  David Feb 6 '12 at 2:18
2  
Not as extreme as @David's position, but: you're asking us for help to debug your code. Please do bother to take time to post something, anything, that can be easily copied into Mathematica in your question. The image only serves as an additional diagnostic, and other than that, it's not something we can debug... –  J. M. Feb 6 '12 at 2:45
2  
Just so that my position is glaringly clear: a screenshot is nice to have; copyable code is need to have. –  J. M. Feb 6 '12 at 2:50
2  
I copied and formatted your code as the picture of it would not help anyone who happens to run across it later. In the process, I removed all of the Mathematica markup as it would have made the code very difficult to read. –  rcollyer Feb 6 '12 at 3:19

1 Answer 1

up vote 6 down vote accepted

The reason you're getting that error is because you're using O, which is a built-in function to represent a term of a particular order. This has the attribute Protected, which prevents you from assigning any definition to it.

Attributes@O
Out[1]= {Protected, ReadProtected}

However, to answer the question in your title, here are a few simple ways in which you can use a function inside another function:

1: Simply call the function!

f[x_] := x^2
g[x_] := Sin[f[x]]

enter image description here

2: Use a Module

g[x_] := Module[{f},
  f[y_] := y^2; Sin[f[x]]
  ]

enter image description here

3: Pass the symbol for the function

f[x_] := x^2
g[func_Symbol, x_] := Sin[func[x]]

enter image description here

share|improve this answer
    
I think Clear did the work for me (now have another error), and it makes me realize that capital 'O' is reserved by Mathematica. Anyway thanks! –  IsaacS Feb 6 '12 at 2:43
1  
@IsaacSaito Yes, that's a built in function. I've added a link to the documentation. I was a bit hesitant at first, because I wasn't sure if it was o or O, which is where a copyable code would've helped :) –  rm -rf Feb 6 '12 at 2:57
    
We probably should have a list of functions like D[], N[], and O[] stashed here somewhere... ;) –  J. M. Feb 6 '12 at 3:04
3  
@J.M. or maybe just remind people not to use capital letters to name variables and functions... a pretty useful lesson for those new to Mathematica. –  Andy Ross Feb 6 '12 at 4:50

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.