Mathematica: Nested User defined function

In summary, the conversation discusses an issue with defining a function beta[M, θ]. The person is trying to divide the equation into smaller pieces but is encountering a problem with the part showing up in red. They ask for help with the "scope" issue and share two versions of the code, with the second one successfully solving the problem.
  • #1
Saladsamurai
3,020
7
Hello :smile: I am a total amateur here and can't seem to get this to work: I have a function beta[M, θ] that I am trying to define. beta is a nasty equation that is way too many lines for me to input at once, so I tried to break it up into pieces. However, the part that is showing up in red is giving me trouble. I haven't even gotten to the beta part of the code, but I expect it will give me the same problem.

I think you can see what I am trying to here. How can I get around the "scope" issue that Mathematica is giving me?

thanks!

Code:
lambda[M_, \[Theta]_] := ((M^2 - 1)^2 - 
     3*(1 + 0.2 M^2) (1 + 1.2 M^2)*Tan[\[Theta]]^2)^.5;

X[M_, \[Theta]_] := ((M^2 - 1)^3 - 
     9*(1 + 0.2 M^2)*(1 + 0.2 M^2 + 2.4/4 M^4) Tan[\[Theta]]^2)/
   lambda[B][COLOR="Red"][M_][/COLOR][/B]^3;

beta[M_, \[Theta]_] := 
  ArcTan[ (M^2 - 1 + 2 lambda*Cos[(4 Pi + ArcCos[X])/3])/(
    3*(1 + .2 M^2)*Tan[\[Theta]])]*180/Pi;
 
Physics news on Phys.org
  • #2
Meh ... This works!

Code:
Clear["Global`*"]

lambda[MM_, \[Theta]\[Theta]_] := ((MM^2 - 1)^2 - 
     3*(1 + 0.2 MM^2) (1 + 1.2 MM^2)*Tan[\[Theta]\[Theta]]^2)^.5;

X[MMM_, \[Theta]\[Theta]\[Theta]_] := ((MMM^2 - 1)^3 - 
     9*(1 + 0.2 MMM^2)*(1 + 0.2 MMM^2 + 
        2.4/4 MMM^4) Tan[\[Theta]\[Theta]\[Theta]]^2)/
   lambda[MMM, \[Theta]\[Theta]\[Theta]]^3;
beta[M_, \[Theta]_] := 
  ArcTan[ (M^2 - 1 + 
     2 lambda[M, \[Theta]]*Cos[(4 Pi + ArcCos[X[M, \[Theta]]])/3])/(
    3*(1 + .2 M^2)*Tan[\[Theta]])]*180/Pi;
 
  • #3
Do you understand why the second one worked and the first one didn't?
 
Last edited:

Related to Mathematica: Nested User defined function

1. What is a nested user-defined function in Mathematica?

A nested user-defined function in Mathematica is a function that is defined within another function. This means that the inner function can only be used within the outer function, and cannot be called on its own. This type of function is useful for creating more complex and specific operations within a larger function.

2. How do you create a nested user-defined function in Mathematica?

To create a nested user-defined function in Mathematica, you can define the inner function using the "Function" keyword within the definition of the outer function. For example, you can use the syntax "outerFunction[x_]:= Function[innerFunction[x]]" to define a nested function.

3. Can you use multiple levels of nesting in user-defined functions in Mathematica?

Yes, it is possible to use multiple levels of nesting in user-defined functions in Mathematica. This means that you can define an inner function within an outer function, and then define another inner function within the inner function, and so on. However, it is important to keep track of the levels of nesting and make sure that the functions are being called in the correct order.

4. Are there any limitations to using nested user-defined functions in Mathematica?

There are a few limitations to using nested user-defined functions in Mathematica. One limitation is that the inner functions can only be used within the outer function, so they cannot be called on their own. Additionally, if a function is defined with more than one argument, it cannot be used as an inner function. Finally, there may be some performance issues when using multiple levels of nesting, so it is important to consider the efficiency of your code.

5. How can nested user-defined functions be useful in Mathematica?

Nested user-defined functions can be useful in Mathematica for creating more complex and specific operations within a larger function. They can also help to improve the organization and readability of code by breaking it down into smaller, more manageable functions. Additionally, nested functions can be used to create custom algorithms or mathematical operations that may not be available in the built-in Mathematica functions.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
179
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
550
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Replies
3
Views
670
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
349
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
373
Replies
0
Views
452
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top