Re: Function calling another function that uses NSolve -
- To: mathgroup at smc.vnet.net
- Subject: [mg123672] Re: Function calling another function that uses NSolve -
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Thu, 15 Dec 2011 04:53:58 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <201112141102.GAA10211@smc.vnet.net>
- Reply-to: drmajorbob at yahoo.com
There are two general solutions: Clear[secondaltb, eqn, kp, ps, c, m] eqn[kp_, ps_, c_, m_][b_] = Sqrt[2*m]*b + InverseCDF[NormalDistribution[0, 1], ps]* Sqrt[4*(InverseCDF[NormalDistribution[0, 1], ps]^2) + 4*Sqrt[2*m]*b + Rationalize[2.027226163, 0]*m] == kp*c - 2*(InverseCDF[NormalDistribution[0, 1], ps]^2); error[kp_, ps_, c_, m_][b_] = eqn[kp, ps, c, m][b] /. Equal -> Subtract; secondaltb[kp_, ps_, c_, m_] = Module[{b}, b /. Solve[eqn[kp, ps, c, m][b], b] ] {(1/(6729636 m))(3364818 Sqrt[2] c kp Sqrt[m] - Sqrt[1121606] Sqrt[ 161511264 c kp m InverseErfc[2 ps]^2 + 81854965 m^2 InverseErfc[2 ps]^2]), (1/( 6729636 m))(3364818 Sqrt[2] c kp Sqrt[m] + Sqrt[1121606] Sqrt[ 161511264 c kp m InverseErfc[2 ps]^2 + 81854965 m^2 InverseErfc[2 ps]^2])} For the arguments in your example, one is spurious: error[2^64, 95/100, 2^(-5538/100), 2295] /@ secondaltb[2^64, 95/100, 2^(-5538/100), 2295] // N {7.95808*10^-13, 270.402} NSolve will avoid superfluous symbolic solutions, but it is slower if the function must be called often (when, as in this case, Solve can deal with the equations). Solve will also avoid spurious solutions, but this is also slower than necessary: Clear[secondaltb] secondaltb[kp_, ps_, c_, m_] := Module[{b, sb}, sb = b /. First@Solve[eqn[kp, ps, c, m][b], b] ] secondaltb[2^64, 95/100, 2^(-5538/100), 2295] // N 3.89154 This has the advantage of using Solve only once (when secondaltb is defined) and NSolve never, while still avoiding spurious solutions: Clear[secondaltb, eqn, kp, ps, c, m, f] eqn[kp_, ps_, c_, m_][b_] = Sqrt[2*m]*b + InverseCDF[NormalDistribution[0, 1], ps]* Sqrt[4*(InverseCDF[NormalDistribution[0, 1], ps]^2) + 4*Sqrt[2*m]*b + Rationalize[2.027226163, 0]*m] == kp*c - 2*(InverseCDF[NormalDistribution[0, 1], ps]^2); error[kp_, ps_, c_, m_][b_] = eqn[kp, ps, c, m][b] /. Equal -> Subtract; secondaltb[kp_, ps_, c_, m_] = Module[{b}, b /. Solve[eqn[kp, ps, c, m][b], b] ]; f[kp_, ps_, c_, m_] := Module[{b, sb}, sb = secondaltb[kp, ps, c, m]; N[First@SortBy[sb, Abs@N@error[kp, ps, c, m]@# &], 20] ] f[2^64, 95/100, 2^(-5538/100), 2295] 3.8915440624386003240 Bobby On Wed, 14 Dec 2011 05:02:28 -0600, jdm <james.d.mclaughlin at gmail.com> wrote: > I was trying to convert a function (advantagesecondalttheoretic below) > from another system to Mathematica. Probably because the function it in > turn > called used NSolve, I couldn't seem to get it to work while it called > this function - it seemed that where secondaltb output {b->whatever > value}, there was no way for advantagesecondalttheoretic to access > this value. I ended up having to call the secondaltb function > separately, and then manually input the result from it to a new > version of advantagesecondalttheoretic - the one below: > > This is, naturally, not much help if I want to plot any graphs! > > Could someone please take a look at the below code and tell me how I > can get advantagesecondalttheoretic to call secondaltb and use the > value of b that results from it? Thanks! > > secondaltb[KP_, Ps_, C_, M_] := > NSolve[Sqrt[2*M]*b + > InverseCDF[NormalDistribution[0, 1], Ps]* > Sqrt[4*(InverseCDF[NormalDistribution[0, 1], Ps]^2) + > 4*Sqrt[2*M]*b + 2.027226163*M] == > KP*C - 2*(InverseCDF[NormalDistribution[0, 1], Ps]^2), b, > WorkingPrecision -> 20] > > lnscale = N[1/(2*Log[2]), 20] > > sb = secondaltb[2^64, 0.95, 2^(-55.38), 2295] > > {{b -> 3.8915440624385913563}} > > advantagesecondalttheoretic[b_] := (lnscale*(b^2)) + Log2[b] + > Log2[Sqrt[2*Pi]] > > N[advantagesecondalttheoretic[3.8915440624385913563], 20] > > 14.210260698586953991 > -- DrMajorBob at yahoo.com
- References:
- Function calling another function that uses NSolve - can't get this
- From: jdm <james.d.mclaughlin@gmail.com>
- Function calling another function that uses NSolve - can't get this