Re: Function calling another function that uses NSolve - can't get this
- To: mathgroup at smc.vnet.net
- Subject: [mg123674] Re: Function calling another function that uses NSolve - can't get this
- From: Ray Koopman <koopman at sfu.ca>
- Date: Thu, 15 Dec 2011 04:54:20 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <jca04g$a2h$1@smc.vnet.net>
On Dec 14, 3:09 am, jdm <james.d.mclaugh... 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 It helps if you make all the arguments exact. secondaltb[KP_, Ps_, C_, M_, wp_:20] := With[{z = N[-Sqrt[2]*InverseErfc[2*Ps], wp]}, NSolve[Sqrt[2*M]*b + z*Sqrt[4*z^2 + 4*Sqrt[2*M]*b + M*2027226163*^-9] == KP*C - 2*z^2, b, WorkingPrecision -> wp] ] sb = b /. secondaltb[2^64, 95/100, 2^(-5538/100), 2295][[1]] b /. secondaltb[2^64, 95/100, 2^(-5538/100), 2295, 30][[1]] 3.8915440624386002540 3.89154406243860025402669707577