Discussion:
Numerical solution from Module
(too old to reply)
Rob Y. H. Chai
2014-05-09 06:07:46 UTC
Permalink
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
Bob Hanlon
2014-05-12 04:42:20 UTC
Permalink
It is a scoping issue which is why the approach that uses explicit
parameters in f works. Alternatively, deconflict the symbols


Clear[a, b, c, f, soln];


f[x_] = (a*x + b)*x + c;


soln[aa_, bb_, cc_] = Module[{x},
x /. Solve[f[x] == 0, x] /.
{a -> aa, b -> bb, c -> cc}];



soln[a, b, c]


{(-b - Sqrt[b^2 - 4*a*c])/(2*a),
(-b + Sqrt[b^2 - 4*a*c])/(2*a)}



soln[1, -3, 2]


{1, 2}



Bob Hanlon
Post by Rob Y. H. Chai
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)}
{{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
Kevin
2014-05-12 04:44:02 UTC
Permalink
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]}
Post by Rob Y. H. Chai
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)}
{{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
Sseziwa Mukasa
2014-05-12 04:45:43 UTC
Permalink
The arguments of soln are not the a, b, and c in f. A couple of solutions:

soln[aval_, bval_, cval_] := Module[{}, xsoln = Solve[f[x] == 0 , x]; x /.
xsoln/.{a->aval,b->bval,c->cval}]

f[x_,a_,b_,c_]:= a*x^2 + b*x + c

soln[a_,b_,c_]:=x/.Solve[f[x,a,b,c]==0,x]
Post by Rob Y. H. Chai
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)}
{{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
Loading...