Clearing variables and functions in Mathematica

In summary, the conversation is about a problem with calculating the magnetic susceptibility in Mathematica. Sometimes it works perfectly, but other times it gives an error message with no obvious reason. The issue can arise from changing a parameter, but even when returning to previous values, the error persists. The speaker suspects it may be an internal problem and is seeking help. Another problem with evaluating functions in Mathematica is also discussed, where parts of the function may diverge but overall it cancels out. It is suggested that the issue may lie in the function not being analytical at a specific point.
  • #1
matoma28
2
0
Can anyone help on this?

I am calculating the magnetic susceptibility, essentially a second derivative evaluated at zero magnetic field. The problem is that sometimes Mathematica works perfectly, and sometimes it gives me a "1/0 complexinfinity" error message, with no obvious reason. The problem can arise simply by changing the value of a parameter, but once I return to the parameter values for which it worked fine, Mathematica no longer gives me back the results (it still gives me the same error message). I suspect that it might be an internal problem, i.e. perhaps I need to clear my (user-defined) functions some way.

Chi[Δ_, λ_, θ_, β_] = -D[F, {h, 2}] /. {h -> 0}; (* This works fine *)
Chi[0.4, 0.6, 0.2, β_] (* The problem is here; in particular, when I change the value of θ. Last time the problem occurred because I gave θ a value outside the function (so that I could use in other functions too);

Any help will be greatly appreciated!
 
Physics news on Phys.org
  • #2
Some problems arise in mathematica with evaulation of functions where parts of the function diverge, though overall it may cancel. An example:

Clear[f]
f[x_] = x/(2 - x) + 2/(x - 2)
f[2]
Limit[f[x], x -> 2]

f[2] will give you 1/0 div by zero error, but this function actually goes to -1 at x=2.

Perhaps there is something in your function that diverges at those specific values (at least in part of the equation), but wouldn't overall.
 
  • #3
Thanks a lot! It turns out that my function was not analytical at the point of interest, that is why Mathematica did not know which (directional) derivative to give me. Thanks for your response.
 

Related to Clearing variables and functions in Mathematica

1. What is the purpose of clearing variables and functions in Mathematica?

Clearing variables and functions in Mathematica is a necessary step in order to avoid conflicts and errors when reusing variable or function names. It allows you to start with a clean slate and avoid any unexpected results.

2. How do I clear a single variable or function in Mathematica?

To clear a single variable or function, you can use the Clear function followed by the name of the variable or function you want to clear. For example, Clear[x] will clear the variable x from the current session.

3. Can I clear all variables and functions at once in Mathematica?

Yes, you can use the ClearAll function to clear all variables and functions at once. This will remove all definitions, attributes, and values associated with variables and functions, essentially resetting the Mathematica session.

4. Can I specify which type of definitions to clear in Mathematica?

Yes, you can use the Clear function with specific options to clear specific types of definitions. For example, Clear[f, {NumericFunction, Protected}] will only clear the numeric definitions and any protected attributes associated with the function f.

5. Is there a way to clear variables and functions in a specific context in Mathematica?

Yes, you can use the Clear function with the Context option to clear variables and functions in a specific context. For example, Clear["MyContext`*", Context] will clear all variables and functions in the context MyContext.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
534
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
994
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top