Mathematica setting variable to be real number

  • Context: Mathematica 
  • Thread starter Thread starter tau1777
  • Start date Start date
  • Tags Tags
    Mathematica Variable
Click For Summary

Discussion Overview

The discussion revolves around the challenge of ensuring a variable remains a real number while solving a set of coupled non-linear algebraic equations using Mathematica. Participants explore methods to enforce this condition within the context of the Newton-Raphson method and matrix equations.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks a way to ensure that a variable, x, remains a real number throughout the iterations of their Newton-Raphson method implementation.
  • Another participant suggests using the Solve function with Reals as the domain to enforce the real condition on x.
  • A different participant expresses uncertainty about using Solve for matrix equations and indicates that some variables in their problem can be complex, but x should not be.
  • One participant mentions the difficulty of responding to Mathematica's output when it provides complex solutions, emphasizing that there is no straightforward way to enforce a "real mode" in Mathematica.
  • Another participant proposes the idea of adding a penalty for complex components in an optimization approach, although this may not yield a true solution.
  • A later reply indicates that after correcting some errors in their code, the participant no longer encounters complex values for x, suggesting a potential resolution to their issue without further need to enforce the real condition explicitly.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to ensure x remains real, with no consensus reached on a definitive method. Some participants offer suggestions while others highlight the limitations and challenges of the proposed solutions.

Contextual Notes

Participants acknowledge the complexity of the problem, particularly in distinguishing between linear and non-linear contexts, and the implications of using different Mathematica functions. There are also references to specific errors in code that may have contributed to the initial issue.

tau1777
Messages
29
Reaction score
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
Could you post the code please? It's awfully hard to tell why this would happen without seeing it.
 
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.
 
Try with the Solve[] fcn instead using Reals as the domain.

Solve[expr,var,Reals]
 
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.
 
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.
 
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.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K