Re: Function calling another function that uses NSolve - can't get this
- To: mathgroup at smc.vnet.net
- Subject: [mg123665] Re: Function calling another function that uses NSolve - can't get this
- From: Bob Hanlon <hanlonr357 at gmail.com>
- Date: Thu, 15 Dec 2011 04:52:42 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
Clear[secondaltb, advantagesecondalttheoretic] Since secondaltb is defined using numeric techniques it should be restricted to numeric arguments. While this doesn't cause a problem in this specific case, it will avoid problems if secondaltb is itself used in a numeric technique. Use ReplaceAll ( /. ) to return a value rather than a rule. Use Module to avoid conflict with any global definitions for b. Don't use machine precision numbers in computations for which you are looking for higher precision. secondaltb[KP_?NumericQ, Ps_?NumericQ, C_?NumericQ, M_?NumericQ] := Module[{b}, b /. NSolve[Sqrt[2*M]*b + InverseCDF[NormalDistribution[0, 1], Ps]* Sqrt[4*(InverseCDF[NormalDistribution[0, 1], Ps]^2) + 4*Sqrt[2*M]*b + 2027226163*^-9*M] == KP*C - 2*(InverseCDF[NormalDistribution[0, 1], Ps]^2), b, WorkingPrecision -> 20][[1]]] advantagesecondalttheoretic[b_] = Simplify[(1/(2*Log[2])*(b^2)) + Log2[b] + Log2[Sqrt[2*Pi]]]; advantagesecondalttheoretic[secondaltb[2^64, 95/100, 2^(-5538/100), 2295]] 14.210260698587007244 This can also be done symbolically until the final conversion to a real number. Clear[secondaltb2] secondaltb2[KP_, Ps_, C_, M_] = Module[{b}, Simplify[b /. Solve[Sqrt[2*M]*b + InverseCDF[NormalDistribution[0, 1], Ps]* Sqrt[4*(InverseCDF[NormalDistribution[0, 1], Ps]^2) + 4*Sqrt[2*M]*b + 2027226163*^-9*M] == KP*C - 2*(InverseCDF[NormalDistribution[0, 1], Ps]^2), b][[1]]]]; advantagesecondalttheoretic[secondaltb2[2^64, 95/100, 2^(-5538/100), 2295]]// N[#, 20] & 14.210260698587007244 Bob Hanlon On Wed, Dec 14, 2011 at 6:02 AM, 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 > -- Bob Hanlon