MathGroup Archive 2014

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Numerical solution from Module

  • To: mathgroup at smc.vnet.net
  • Subject: [mg132716] Re: Numerical solution from Module
  • From: Kevin <kjm at KevinMcCann.com>
  • Date: Mon, 12 May 2014 00:43:43 -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
  • References: <lkhrbi$e0k$1@smc.vnet.net>

I would put f inside the module as well. Here is my version:

Clear[soln, a, b, c]
soln[a_, b_, c_] := Module[{xsoln, f, x},
   f[x_] = a*x^2 + b*x + c;
   xsoln = Solve[f[x] == 0, x]; x /. xsoln
   ]

Try it out:

soln[1,2,3]

output: {-1-I Sqrt[2],-1+I Sqrt[2]}


On 5/9/2014 2:07 AM, Rob Y. H. Chai wrote:
> Hello every one,
>
>
>
> Here is a simple question. Say I define a function
>
>
>
> In[14]:= f[x_] := a*x^2 + b*x + c
>
>
>
> Then I use Module to frame the solution of f[x] ==0
>
>
>
> In[15]:= soln[a_, b_, c_] := Module[{}, xsoln = Solve[f[x] == 0 , x]; x /.
> xsoln]
>
>
>
> When I enter numerical values for parameters a, b, and c in the module, f[x]
> never sees these numerical values, and returns a symbolic solution.
>
>
>
> In[11]:= soln[1, -3, 2]
>
>
>
> Out[11]= {(-b - Sqrt[b^2 - 4 a c])/(2 a), (-b + Sqrt[b^2 - 4 a c])/(2 a)}
>
>
>
> But I want the module to return a numerical solution as:
>
> {{x -> 1}, {x -> 2}}
>
>
>
> My question is: without bringing f[x] explicitly into the Module function,
> and without redefining f as f[a_,b_,c_][x] := a*x^2+b*x+c, how can I get the
> module to return a numerical solution?
>
>
>
> Thanks - Rob Chai
>
>



  • Prev by Date: Re: DSolve test
  • Next by Date: Re: limits on symbol eigenvalues?
  • Previous by thread: Re: Numerical solution from Module
  • Next by thread: Re: Numerical solution from Module