Mathematica: Nested User defined function

  • Context: Mathematica 
  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Function Mathematica
Click For Summary
SUMMARY

The forum discussion centers on defining a nested user-defined function in Mathematica, specifically the function beta[M, θ]. The user encountered scope issues when attempting to define the function with multiple parameters. The solution involved using clear variable names and ensuring proper function definitions, which resolved the scope problem. The successful implementation of the beta function demonstrates the importance of correctly managing variable scope in Mathematica.

PREREQUISITES
  • Understanding of Mathematica syntax and function definitions
  • Familiarity with variable scope and its implications in programming
  • Basic knowledge of trigonometric functions and their usage in Mathematica
  • Experience with nested functions and parameter passing in Mathematica
NEXT STEPS
  • Explore Mathematica's documentation on function scope and variable management
  • Learn about debugging techniques in Mathematica to identify scope issues
  • Investigate advanced function definitions, including anonymous functions in Mathematica
  • Study the use of trigonometric functions in Mathematica for complex calculations
USEFUL FOR

Mathematica users, particularly those developing complex mathematical models, programmers looking to enhance their understanding of function scope, and anyone interested in advanced mathematical computations using Mathematica.

Saladsamurai
Messages
3,009
Reaction score
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][M_][/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
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;
 
Do you understand why the second one worked and the first one didn't?
 
Last edited:

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
10
Views
3K