11
$\begingroup$

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.

$\endgroup$
  • 2
    $\begingroup$ Looks like maybe r_(A/O) and r_(B/A) had values before you tried to turn it into a function. $\endgroup$ – Brett Champion Feb 6 '12 at 2:10
  • 4
    $\begingroup$ 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. $\endgroup$ – David Feb 6 '12 at 2:18
  • 2
    $\begingroup$ 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... $\endgroup$ – J. M. will be back soon Feb 6 '12 at 2:45
  • 3
    $\begingroup$ Just so that my position is glaringly clear: a screenshot is nice to have; copyable code is need to have. $\endgroup$ – J. M. will be back soon Feb 6 '12 at 2:50
  • 2
    $\begingroup$ 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. $\endgroup$ – rcollyer Feb 6 '12 at 3:19
22
$\begingroup$

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

$\endgroup$
  • $\begingroup$ 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! $\endgroup$ – IsaacS Feb 6 '12 at 2:43
  • 2
    $\begingroup$ @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 :) $\endgroup$ – rm -rf Feb 6 '12 at 2:57
  • $\begingroup$ We probably should have a list of functions like D[], N[], and O[] stashed here somewhere... ;) $\endgroup$ – J. M. will be back soon Feb 6 '12 at 3:04
  • 4
    $\begingroup$ @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. $\endgroup$ – Andy Ross Feb 6 '12 at 4:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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