Mathematica setting variable to be real number

by tau1777
Tags: mathematica, real number, variable
tau1777 is offline
Dec23-11, 01:56 PM
P: 30
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.
Phys.Org News Partner Science news on
Lemurs match scent of a friend to sound of her voice
Repeated self-healing now possible in composite materials
'Heartbleed' fix may slow Web performance
Nile3 is offline
Dec23-11, 05:52 PM
P: 42
Could you post the code please? It's awfully hard to tell why this would happen without seeing it.
tau1777 is offline
Dec23-11, 06:38 PM
P: 30
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.


Nile3 is offline
Dec23-11, 06:45 PM
P: 42

Mathematica setting variable to be real number

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

tau1777 is offline
Dec23-11, 07:29 PM
P: 30
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.

Bill Simpson
Bill Simpson is offline
Dec23-11, 09:22 PM
P: 971
Quote Quote by tau1777 View Post
I don't know how to use Solve to solve matrix equations.
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.
tau1777 is offline
Dec23-11, 10:08 PM
P: 30
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.

Register to reply

Related Discussions
Mathematica setting double vs. float Math & Science Software 4
Is an irrational root of a real number imaginary or real? General Math 9
Mathematica:How can I acclaim a variable is real in a complex function Math & Science Software 2
proving that a real number exists in between a real number, Calculus 19
HS setting: Mathematica vs Maple vs? Math & Science Software 2