# Mathematica setting variable to be real number

1. Dec 23, 2011

### tau1777

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.

2. Dec 23, 2011

### Nile3

Could you post the code please? It's awfully hard to tell why this would happen without seeing it.

3. Dec 23, 2011

### tau1777

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. Dec 23, 2011

### Nile3

Try with the Solve[] fcn instead using Reals as the domain.

Solve[expr,var,Reals]

5. Dec 23, 2011

### tau1777

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. Dec 23, 2011

### Bill Simpson

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. Dec 23, 2011

### tau1777

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.