Mathematica setting variable to be real number

In summary, you can initialize a variable in your Newton-Raphson scheme by setting it to be a real number, but if the process iterates the variable gets a complex component. You can try using the Solve[] fcn to solve the equation, but it is difficult to tell if the complex component is causing your problem. You can try fitting or approximating the equation to find a real value that isn't a solution, but this is difficult without knowing more about your problem.
  • #1
tau1777
30
0
Hi All,

I am trying to solve a set of coupled non-linear algebraic equations. I'm doing this using the LinearSolve fcn of mathematica and an implementation of the Newton-Raphson method. One of the issues I'm having is that I need to tell mathematica that one of these variables,let's say x, is a real number. In my Newton-Raphson scheme I initialize all of my variables, and I set x to initially be a real number. But as the process iterates x gets a complex component. Is there any command that will tell mathematica that x always needs to remain real?

I have seen that if one uses the functions Reduce and Solve, they can pick the output to be real. But unfortunately I do not believe I can use those fcns since I have a matrix equation to solve, and I do need some of the variables to be complex.

Any help is greatly appreciated.

Happy Holidays.
 
Physics news on Phys.org
  • #2
Could you post the code please? It's awfully hard to tell why this would happen without seeing it.
 
  • #3
Hi Nile3,

Sorry but I can't put up the code, this problem is related to a research project and although I don't think putting up the code would reveal to much I'd just rather not.

I guess I don't really care what in my code might be causing this to happen. I just want to figure out a way to set this variable,x, to be a positive real number and have it just stay that way no matter what.

Thanks.
 
  • #4
Try with the Solve[] fcn instead using Reals as the domain.

Solve[expr,var,Reals]
 
  • #5
I don't know how to use Solve to solve matrix equations. Also some of my variables can be complex. Just not this one x, variable.

Thanks.
 
  • #6
tau1777 said:
I don't know how to use Solve to solve matrix equations.

This
http://reference.wolfram.com/mathematica/tutorial/LinearAlgebraMatrixComputations.html#81663253
shows you exactly how to solve matrix equations.

But you began this by saying you have a nonlinear problem. It is not clear that showing you how to solve a linear problem is going to help you.

It is also difficult to decide exactly how to respond when Mathematica tells you the solution is complex and you respond "no, I want a real solution."

You can extract the real component from a complex value using Re[], but that doesn't sound like it is what you are going to want.

Beginning students in the past would sometimes ask to only see real solutions, and thus to have Mathematica discard the complex solutions. That doesn't sound like it applies in your case and there were a variety of difficulties with the methods that tried to accomplish that.

Perhaps you could do some sort of fit or approximation for your problem and to have a wildly expensive penalty for any complex component. That might let you find a real value that isn't a solution to your problem but minimizes the penalty between that and the actual (complex) solution.

As you say, without having any idea what your actual problem is, it is very difficult to give you any concrete directions for what to do.

But there is no switch that puts Mathematica into "real mode", even if you don't need to continue using complex numbers in the rest of your calculations.
 
  • #7
Yes, I am using LinearSolve, I basically have turned my couple nonlinear PDE problem into a nonlinear algebraic problem, but to the best of my understanding LinearSolve can still handle that as long as its in matrix form.

Anyways, I was thinking about adding in an extra condition so that x would have to be real. However after checking my code I've found some errors, and fixed those. Now I no longer seem to be getting a complex value for this variable.I will re-check everything now and hopefully it will just work. Without having to dig back into this issue of setting variables.

Thanks for all the help you guys.
 

1. What is Mathematica?

Mathematica is a powerful software program used for mathematical and scientific computations. It allows users to perform a wide range of calculations, visualize data, and create interactive simulations.

2. How do I set a variable to be a real number in Mathematica?

To set a variable to be a real number in Mathematica, you can use the command "Set" or the symbol ":=" followed by a decimal number. For example, "x = 5.2" or "x := 3.14" will set the variable x to be a real number.

3. Can I change the precision of a real number in Mathematica?

Yes, you can change the precision of a real number in Mathematica by using the command "SetPrecision" or the symbol "``" followed by the desired precision. For example, "SetPrecision[x, 20]" will set the precision of the variable x to be 20 digits.

4. How can I check if a variable is a real number in Mathematica?

To check if a variable is a real number in Mathematica, you can use the function "NumericQ". This function returns "True" if the variable is a numeric value, including real numbers, and "False" if it is not.

5. Are there any built-in functions for working with real numbers in Mathematica?

Yes, Mathematica has a wide range of built-in functions for working with real numbers, including arithmetic operations, trigonometric functions, and statistical functions. You can also use the "RealDigits" function to convert a real number into a list of its digits, and the "RealPart" and "ImaginaryPart" functions to extract the real and imaginary parts of a complex number.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
132
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
257
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • General Math
Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
3K
Back
Top