Re: Solve vs. NSolve
- To: mathgroup at smc.vnet.net
- Subject: [mg92834] Re: Solve vs. NSolve
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Tue, 14 Oct 2008 04:59:49 -0400 (EDT)
On 10/13/08 at 6:21 AM, sigmundv at gmail.com (SigmundV) wrote: >Consider >f[a_, b_, c_, k_, t_] := >With[{\[Alpha] = a k, \[Beta] = b k}, (x - \[Alpha] Cos[t])^2/a^2 + >(y - (\[Beta] Sin[t] + c) - c)^2/b^2 - 1 == 0]; df[a_, b_, c_, k_, >t_] := D[f[a, b, c, k, t], t]; >and execute >{x, y} /. Simplify@PowerExpand@Simplify@Solve[{f[1, 2, 1/2, 4/5, t], >df[1, 2, 1/2, 4/5, t]}, {x, y}] // Chop // N >and >{x, y} /. Simplify@PowerExpand@Simplify@NSolve[{f[1, 2, 1/2, 4/5, >t], df[1, 2, 1/2, 4/5, t]}, {x, y}] // Chop >Simplify@PowerExpand@Chop@Simplify[% /. Cos[2 t] -> (1 - 2 >Sin[t]^2)] >respectively. >As you see, Solve and NSolve yield two different solutions, with the >solution from Solve being the correct one, as can be verified by >plugging in to the equations -- the solution from NSolve does not >satisfy the second equation, but only the first. Can anyone explain >this behaviour to me? The issue isn't NSolve vs Solve since, In[5]:= Solve[{f[1, 2, 1/2, 4/5, t], df[1, 2, 1/2, 4/5, t]}, {x, y}] // FullSimplify Out[5]= {{x -> (4*Cos[t])/5 - Cot[t]*Sqrt[Sin[t]^2], y -> (8*Sin[t])/5 - 2*Sqrt[Sin[t]^2] + 1}, {x -> (4*Cos[t])/5 + Cot[t]*Sqrt[Sin[t]^2], y -> (8*Sin[t])/5 + 2*Sqrt[Sin[t]^2] + 1}} and In[8]:= NSolve[{f[1, 2, 1/2, 4/5, t], df[1, 2, 1/2, 4/5, t]}, {x, y}] // FullSimplify // Chop Out[8]= {{x -> 0.8*Cos[t] - 1.*Cot[t]*Sqrt[Sin[t]^2], y -> 1.6*Sin[t] - 2.*Sqrt[0.49999999999999994 - 0.49999999999999994*Cos[2*t]] + 1.}, {x -> 0.8*Cos[t] + 1.*Cot[t]*Sqrt[Sin[t]^2], y -> 1.6*Sin[t] + 2.*Sqrt[0.49999999999999994 - 0.49999999999999994*Cos[2*t]] + 1.}} The issue is the transformations you do to these solutions. In particular, note In[14]:= Sqrt[Sin[t]^2] == Sin[t] /. t -> -.5 Out[14]= False That is PowerExpand needs to be used with a lot of care since the transformation it does is not valid for all possible values. =46or the case where you used Solve, PowerExpand transforms 2*Sqrt[Sin[t]^2] to 2 Sin[t]. In the case where you used NSolve, the mathematically equivalent expression 2.*Sqrt[0.5 - 0.5*Cos[2*t]] is unaffected by PowerExpand. The result is two expressions that are not equal although they started as equal.