Re: Problems with Solve
- To: mathgroup at smc.vnet.net
- Subject: [mg132466] Re: Problems with Solve
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Sun, 23 Mar 2014 04:59:58 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-outx@smc.vnet.net
- Delivered-to: mathgroup-newsendx@smc.vnet.net
On 3/22/14 at 12:06 AM, sy81 at sussex.ac.uk (Samuel Mark Young) wrote: >Hello everyone, I'm trying to use the solutions of Solve from >solving a cubic equation - however, it keeps returning complex >answers when there are real solutions. For example: >Solve[z + 5 (z^2 - 1) + 1 z^3 == 1, z] >This equation has 3 real solutions. However, the answers returned >when I ask mathematica for a decimal answer are complex (which I >need to do later on when an integration needs solving numerically): >{{z -> 0.925423 + 0. I}, {z -> -4.47735 + >2.22045*10^-16 I}, {z -> -1.44807 - 4.44089*10^-16 I}} Since the equation you provided Solve has exact coefficients you must have done Solve[z + 5 (z^2 - 1) + 1 z^3 == 1, z] // N to get the result you posted >I'm guessing this is to do with the finite precision that is used in >the calculations as the imaginary components are very small, but am >unsure how to deal with them and they shouldn't be there. Any >suggestions? Use Chop to get rid of small values, i.e., In[8]:= Solve[z + 5 (z^2 - 1) + 1 z^3 == 1, z] // N // Chop Out[8]= {{z->0.925423},{z->-4.47735},{z->-1.44807}} Or perhaps even better when you want machine precision answers use NSolve In[9]:= NSolve[z + 5 (z^2 - 1) + 1 z^3 == 1, z] Out[9]= {{z->-4.47735},{z->-1.44807},{z->0.925423}} >The second problem I am having is that I need to solve for s in a >function B[s] == 10^-5, where B is some (complicated) function of s. >The form of the function depends on s - and this is handled by If[] >commands in the function B. For example, the s dependance might be: >B[s]:=If[s<0.5,Erfc[-x],Erfc[-x]+Erfc[y]-Erfc[z]] >B[s] is a smooth function of s. >The problem seems to arise because, before it has found a solution for >s, it can't decide which form of the function to use - and so just >returns an error message You can ensure your function won't evaluate with symbolic arguments by defining it as: B[s_?NumericQ]: Another thing to consider : It is generally not good to use a single capital letter for things you define in Mathematica. Doing so can lead to conflicts since there are with built-in definitions for several capital letters. However, I suspect none of the above will work. The problem is s is not a really parameter of what is being computed. Instead it is being used as a switch. If you were to do =46indRoot[b[s]-10^5, {s, ... B[s] cannot return numeric values unless x, y and z have defined numeric values. And if they do have defined numeric values, B[s] will return a constant value for all s < .5 and another constant value when s >= .5. I would guess you really want to do something like b[s_,res_?NumericQ]:=If[s<.5, FindRoot[Erfc[x]-res,{x ....